Source for "Google Reader - Read by Mouse"

By ryanbot
Has 1 other script.


// ==UserScript==
// @name           Google Reader - Read by Mouse
// @namespace      http://userscripts.org/people/26481
// @description    Adds a button that toggles Google Reader in and out of a "mouse-only" mode that allows for easy and customizable reading via the mouse buttons (next item, previous item, open in tab/star/share/tag).
// @source         http://userscripts.org/scripts/show/8843
// @version        0.8
// @date           2008-02-14
// @creator        Ryan Williams <ryanbot at gmail>
// @include        http://www.google.com/reader/*
// @include        https://www.google.com/reader/*
// ==/UserScript==

// Changelog
// ---------
// v0.8     14-Feb-2008 -  Fixed an issue with links not working while in Mouse Mode
// v0.7     13-Feb-2008 - Added experimental Add Tag feature
// v0.6     03-Feb-2008 - Fixed an issue with the autoupdate.
// v0.5     13-May-2007  -  Clicks on links work like normal (by-pass the Next/Prev/Open in Tab features)
// v0.4     08-May-2007  -  Now hides context menu when Mouse Mode is on.
// v0.3     01-May-2007   - Added a keboard shortcut to toggle mouse mode on and off (ctrl-z)
// v0.2     30-Apr-2007 - Added auto-updating and fixed some bugs
// v0.1.7   28-Apr-2007 - The middle click settings are now saved to GM
// v0.1.6   28-Apr-2007 - Added a droplist of settings for the middle click button
// v0.1.5   27-Apr-2007 - Replaced keyboard shortcut with toggle button on screen.
// v0.1     26-Apr-2007 - initial release

// Thanks to Mihai Parparita for the simulateClick function (http://googlereader.blogspot.com/2005/11/warning-geekery-ahead.html)
// Took the autoupdate code from: http://userscripts.org/forums/1/topics/48

// If you have suggestions or want to make changes to this script, please leave a comment or email me at ryanbot at gmail.

// Autoupdate...
function autoUpdateFromUserscriptsDotOrg(SCRIPT) {
    // Note: Version numbers must be in x.y float format
    try {
    if (!GM_getValue) return;
         // avoid a flood of dialogs e.g. when opening a browser with multiple tabs set to homepage and a script with * includes
        var DoS_PREVENTION_TIME = 2 * 60 * 1000;
        var isSomeoneChecking = GM_getValue('CHECKING', null);
        var now = new Date().getTime();
        GM_setValue('CHECKING', now.toString());
        if (isSomeoneChecking && (now - isSomeoneChecking) < DoS_PREVENTION_TIME) return;
        var lastChecked = GM_getValue('LAST_CHECKED', null);

        var ONE_DAY = 24 * 60 * 60 * 1000;
        if (lastChecked && (now - lastChecked) < ONE_DAY) return;
        GM_xmlhttpRequest(
        {
            method: 'GET',
            url: SCRIPT.checkURL, // don't increase the 'installed' count just for checking
            onload: function(result)
            {
                if (!result.responseText.match(/@version\s+([\d.]+)/)) return;
                var theOtherVersion = parseFloat(RegExp.$1);
                if (theOtherVersion <= parseFloat(SCRIPT.version)) return;
                if (window.confirm('A new version (' + theOtherVersion + ') of greasemonkey script "' + SCRIPT.name + '" is available.\nYour installed version is ' + SCRIPT.version + ' .\n\nUpdate now?\n'))
                {
                    GM_openInTab(SCRIPT.url);
                }
            }
        });
        GM_setValue('LAST_CHECKED', now.toString());
    }    catch (ex){}
}

// Check for an update
autoUpdateFromUserscriptsDotOrg(
{    
    name: 'Google Reader - Read by Mouse',
    checkURL: 'http://userscripts.org/scripts/review/8843?format=txt',	
    url: 'http://userscripts.org/scripts/source/8843.user.js',
    version: '0.8'
});

var systemOn = 'false';

//Get the element our new button goes by
var nearNewButton = document.getElementById('sub-tree-subscriptions');

// Add the middle click settings droplist and the Mouse Control toggle button
if (nearNewButton)
{
    var mouseCtrlButton = document.createElement("div");
    mouseCtrlButton.innerHTML = '<table><tr><td style="text-align:left";>Middle click:</td><td></td></tr><tr rowspan="2"><td style="verticle-align:top";><select id="___middleClickSettings" name="midClickSettings"><option id="openInTab" value="openInTab">Opens in Tab<option id="share" value="share">Shares<option id="star" value="star">Stars<option id="addTag" value="addTag">Add a Tag</select></td><td><input type="button" id="___mouseCtrl" value="Mouse Mode Off"></input></tr><tr id="addTagSpan"><td colspan="2" style="text-align:left;">Tag: <input type="textbox" id="txtTag" value=""></td></tr></table>';
    nearNewButton.parentNode.insertBefore(mouseCtrlButton, nearNewButton.nextSibling);
}

var currentSettingMidClick = 'openInTab';
var currentTag = 'test';

if (GM_getValue)
{
    //Get the current middle click setting out of GM
    currentSettingMidClick = GM_getValue('middleClickSetting', 'openInTab');
    
	//Get the current tag setting out of GM
	currentTag = GM_getValue('mouseTag', '');
	
    // set the selected value to the setting
    var midClickOption = document.getElementById(currentSettingMidClick);
    if (!midClickOption) return;
    midClickOption.selected = true;
	
	//Hide or reveal the tag textbox
	if (midClickOption.value == "addTag")
	{
		document.getElementById('addTagSpan').style.visibility = 'visible';
	}
	else
	{
		document.getElementById('addTagSpan').style.visibility = 'collapse';
	}
	
	document.getElementById('txtTag').value = currentTag;
}


//Add listener for key press (toggles Mouse on and off)
document.addEventListener('keydown', function(event) {
    if ( event.ctrlKey && event.which == 90)
        
        var myBtn = document.getElementById('___mouseCtrl');
        
        if (!myBtn) return;
        
        if (systemOn == 'true')
        {
            myBtn.value = 'Mouse Mode Off';
            systemOn = 'false';

        }
        else
        {
            myBtn.value = 'Mouse Mode On';
            systemOn = 'true';

        }
        
}, false);



//Add listener for mouse clicks
document.addEventListener('click', function(event) {
    // On each left click, check to see if the middle click setting has changed.  
    //    if so, then set it in GM
    if (event.button==0)
    {
        //Get the selected option
        var myMiddleSelect = document.getElementById('___middleClickSettings');
        
        if (!myMiddleSelect) return;
        
        var midClickSelValue = myMiddleSelect.options[myMiddleSelect.selectedIndex].value;
        
        if (!midClickSelValue) return;
        
        // If the middle click setting has changed, then set it in GM
        if (currentSettingMidClick != midClickSelValue)
        {
            //alert('pushing setting: ' + midClickSelValue);
            if (GM_setValue)
            {
                GM_setValue('middleClickSetting', midClickSelValue);
                currentSettingMidClick = midClickSelValue;
            }
        }

		//If the tag has changed, then set it in GM
		var strTag = document.getElementById('txtTag').value;
		
		if (currentTag != strTag)
		{
			if (GM_setValue)
			{
				GM_setValue('mouseTag', strTag);
				currentTag = strTag;
			}
		
		}
		
	 	//Hide or reveal the tag textbox
		if (currentSettingMidClick == "addTag")
		{
			document.getElementById('addTagSpan').style.visibility = 'visible';
		}
		else
		{
			document.getElementById('addTagSpan').style.visibility = 'collapse';
		}
    
    }
	
	//Middle click
	if (event.button==1 && systemOn == 'true')
	{
	    //If they click on a link, let the link work like normal
	    if (event.target.nodeName.toLowerCase() == 'a')
	    {
	        return;
	    }
		
		//Action here depends on the selection from the droplist
		var mySettingsDL = document.getElementById("___middleClickSettings");
		switch (mySettingsDL.options [mySettingsDL.selectedIndex].value)
		{
			
			case "addTag":
				tagItem2();
				break;
		}
		
		event.stopPropagation();
		event.preventDefault();
	}

	
}, true);

//Add listener for mousedown
document.addEventListener('mousedown', function(event) {

var myTarget = event.target;
    
if (systemOn == 'true')
{
    //If they click on a link, let the link work like normal
    if (myTarget.nodeName.toLowerCase() == 'a')
    {
        return;
    }
	var clickType=event.button;
	// Left Click
	if (clickType==0)    
	{
		//turn mouse control off if clicking on the mouse control button
		if (myTarget.id == '___mouseCtrl')
		{
			myTarget.value = 'Mouse Mode Off';
			systemOn = 'false';
		}
		else if (myTarget.id == '___middleClickSettings' || myTarget.id == 'txtTag')
		{
			//Always let clicks work here
		}
		else
		{
			simulateClick(document.getElementById ("entries-down"));
			event.stopPropagation ();
			event.preventDefault();
		}
	}
        
	//Right Click
	if (clickType==2)
	{
		simulateClick(document.getElementById("entries-up"));
		event.stopPropagation();
		event.preventDefault();
	}
	
	//Middle click
	if (clickType==1)
	{
		
		//Action here depends on the selection from the droplist
		var mySettingsDL = document.getElementById("___middleClickSettings");
		
		switch (mySettingsDL.options [mySettingsDL.selectedIndex].value)
		{
			case "openInTab":
				openInTab();
				break;
			case "share":
				shareItem();
				break;
			case "star":
				starItem();
				break;
			case "addTag":
				tagItem1();
				break;
		}
		
		event.stopPropagation();
		event.preventDefault();
		
	}
        
        
}
else  //Mouse control is off
{
	//If they clicked on the mouse control button, then turn it on.
	if (myTarget.id == '___mouseCtrl')
	{
		myTarget.value = 'Mouse Mode On';
		systemOn = 'true';
	}
}

}, true);

//Go find the "Open original in tab" element and get the URL for original
function openInTab()
{
    //Find the post link in the current entry
    var currentEntry = document.getElementById('current-entry');
    
    if (!currentEntry)
        return;
    
    var mytable = currentEntry.getElementsByTagName("table")[0];
    if (!mytable) return;
    var mytablebody = mytable.getElementsByTagName("tbody")[0];
    var myrow = mytablebody.getElementsByTagName("tr")[1];
    var myA = myrow.getElementsByTagName("a")[0];
        
    if (!GM_openInTab)
    {
        alert('Please upgrade to the latest version of Greasemonkey.');
        return;
    }
                
    GM_openInTab(myA.getAttribute('href'),'_blank');
}

//Go find the "share item" button and simulate a click on it
function shareItem()
{
    var currentEntry = document.getElementById('current-entry');
    
    if (!currentEntry)
        return;
    
    var mytable = currentEntry.getElementsByTagName("table")[0];
    if (!mytable) return;
    var mytablebody = mytable.getElementsByTagName("tbody")[0];
    var myrow = mytablebody.getElementsByTagName("tr")[2];
    var mycell = myrow.getElementsByTagName("td")[1];
    var mydiv = mycell.getElementsByTagName("div")[0];
    var myspan = mydiv.getElementsByTagName("span")[1];
    
    simulateClick(myspan);    
}

//Go find the "star item" button and simulate a click on it
function starItem()
{
    var currentEntry = document.getElementById('current-entry');
    
    if (!currentEntry)
        return;
    
    var mytable = currentEntry.getElementsByTagName("table")[0];
    if (!mytable) return;
    var mytablebody = mytable.getElementsByTagName("tbody")[0];
    var myrow = mytablebody.getElementsByTagName("tr")[2];
    var mycell = myrow.getElementsByTagName ("td")[1];
    var mydiv = mycell.getElementsByTagName("div")[0];
    var myspan = mydiv.getElementsByTagName("span")[0];
    
    simulateClick(myspan);
    
}

// Do the first part of tagging (click the tag button to reveal the tag control)
function tagItem1()
{
    var currentEntry = document.getElementById('current-entry');
    
    if (!currentEntry)
        return;
    
    var mytable = currentEntry.getElementsByTagName("table")[0];
    if (!mytable) return;
    var mytablebody = mytable.getElementsByTagName("tbody")[0];
    var myrow = mytablebody.getElementsByTagName("tr")[2];
    var mycell = myrow.getElementsByTagName ("td")[1];
    var mydiv = mycell.getElementsByTagName("div")[0];
    var myspan = mydiv.getElementsByTagName("span")[5];
	var myInnerSpan = myspan.getElementsByTagName("span")[0];
    												
    simulateClick(myInnerSpan);

}

// Do the second part of tagging (add the tag and click the save button)
function tagItem2()
{

	var popup = document.getElementById('tags-container-template');

	
	var txt = popup.getElementsByTagName("input")[0];

	txt.value =  txt.value + document.getElementById('txtTag').value;
	var divA = popup.getElementsByTagName("div")[0];
	var divB = divA.getElementsByTagName("div")[0];
	var divC = divB.getElementsByTagName("div")[1];
	var saveButton = divC.getElementsByTagName("table")[0];
	simulateClick(saveButton);
}


function simulateClick(node) {
    var event = node.ownerDocument.createEvent("MouseEvents");
 
    event.initMouseEvent("click",
                         true, // can bubble
                         true, // cancellable
                         node.ownerDocument.defaultView,
                         1, // clicks
                         50, 50, // screen coordinates
                         50, 50, // client coordinates
                         false, false, false, false, // control/alt/shift/meta
                         0, // button,
                         node);
 
    node.dispatchEvent(event);
 }

 // Disable the context menu when Mouse Mode is on.
 document.addEventListener('contextmenu', function(event) {

    if (systemOn == 'true')
    {
        //Let clicks on links open the context menu.
        if (event.target.nodeName.toLowerCase () != 'a')
        {    
            event.stopPropagation();
            event.preventDefault();
        }
    }
}, true);