[Greasemonkey] adding function calling to <A> element

Matej Cepl ceplm at seznam.cz
Thu Mar 2 20:13:57 EST 2006


Hi,

I tried this with GM on Firefox 1.5 and it doesn't work (after
pressing Alt+L nothing happens and there is no message in
JavaScript Console; line broken for this message only):

// RBMAnotherLocation
//
// ==UserScript==
// @name          RMBAnotherLocation
// @namespace     http://www.ceplovi.cz/matej/progs/scripts
// @description   ask for another location and go there
// @include       http://www.example.com/somewhere/...
// ==/UserScript==

window.openNewLocation = function(event) {
	alert("OK");
	//var newLocation = \
	//prompt("Enter number of the new location","");
	event.preventDefault();
	//return newLocation;
}

var ATags = document.getElementsByTagName("a");
var hrefArray = window.location.href.split("/");
hrefArray[hrefArray.length-1] = "gallery";
var imageHREF = hrefArray.join("/");

for (var i = 0; i < ATags.length; i++)
{
	tempElem = ATags[i];
	if (tempElem.getAttribute("href") == "someURL") {
		tempElem.accesskey = "L";
		// onClick doesn't work with GM,
		// I have to use addEventListener
		//tempElem.onClick = \
		// "function() {openNewLocation();return false}";
		tempElem.addEventListener('click', \
			window.openNewLocation, true);
	}
};

//
// ChangeLog
// 2006-03-02 - 0.1 - MC - first attempt
//

Thanks a lot,

Matej

-- 
GPG Finger: 89EF 4BC6 288A BF43 1BAB  25C3 E09F EF25 D964 84AC
http://www.ceplovi.cz/matej/blog/
23 Marion St. #3, (617) 876-1259, ICQ 132822213



More information about the Greasemonkey mailing list