useful piece of code for GreaseKit 1.4

in Script development
Subscribe to useful piece of code for GreaseKit 1.4 12 posts, 6 voices



greut Scriptwright

Since GreaseKit 1.4 removed all the GM_* methods for security reasons, most of the GM scripts aren't working anymore.

http://pastie.caboo.se/161302

if(typeof GM_xmlhttpRequest === "undefined") {
	GM_xmlhttpRequest = function(/* object */ details) {
		details.method = details.method.toUpperCase() || "GET";
		
		if(!details.url) {
			throw("GM_xmlhttpRequest requires an URL.");
			return;
		}
		
		// build XMLHttpRequest object
		var oXhr, aAjaxes = [];
		if(typeof ActiveXObject !== "undefined") {
			var oCls = ActiveXObject;
			aAjaxes[aAjaxes.length] = {cls:oCls, arg:"Microsoft.XMLHTTP"};
			aAjaxes[aAjaxes.length] = {cls:oCls, arg:"Msxml2.XMLHTTP"};
			aAjaxes[aAjaxes.length] = {cls:oCls, arg:"Msxml2.XMLHTTP.3.0"};
		}
		if(typeof XMLHttpRequest !== "undefined")
			 aAjaxes[aAjaxes.length] = {cls:XMLHttpRequest, arg:undefined};
	
		for(var i=aAjaxes.length; i--; )
			try{
				oXhr = new aAjaxes[i].cls(aAjaxes[i].arg);
				if(oXhr) break;
			} catch(e) {}
		
		// run it
		if(oXhr) {
			if("onreadystatechange" in details)
				oXhr.onreadystatechange = function() { details.onreadystatechange(oXhr) };
			if("onload" in details)
				oXhr.onload = function() { details.onload(oXhr) };
			if("onerror" in details)
				oXhr.onerror = function() { details.onerror(oXhr) };
			
			oXhr.open(details.method, details.url, true);
			
			if("headers" in details)
				for(var header in details.headers)
					oXhr.setRequestHeader(header, details.headers[header]);
			
			if("data" in details)
				oXhr.send(details.data);
			else
				oXhr.send();
		} else
			throw ("This Browser is not supported, please upgrade.")
	}
}

if(typeof GM_addStyle === "undefined") {
	function GM_addStyle(/* String */ styles) {
		var oStyle = document.createElement("style");
		oStyle.setAttribute("type", "text\/css");
		oStyle.appendChild(document.createTextNode(styles));
		document.getElementsByTagName("head")[0].appendChild(oStyle);
	}
}

if(typeof GM_log === "undefined") {
	function GM_log(log) {
		if(console)
			console.log(log);
		else
			alert(log);
	}
}

some handy functions.

You still have to deal with unsafeWindow yourself though.

 
psyched Scriptwright

W00t??
Why would anyone remove the thing that makes Greasemonkey/Greasekit adorable the most??

 
Descriptor Scriptwright

Heh, a better GM_addStyle function than what Greasemonkey uses. However mine's better than that.

 
psyched Scriptwright

How is that better?
GM_addStyle() css strings have priority before all embedded stylesheets and such, pretty much like adding !important behind every rule, as far as I know..
But let me know why it's better ;)

 
Descriptor Scriptwright

psyched, uhm, what makes you think that?
Greasemonkey uses this: http://diveintogreasemonkey.org/patterns/add-cs...
It's in miscapis.js

 
greut Scriptwright

The goal isn't to be better, only to enable a GM script to run with GreaseKit as well. GM_* API has a lot of very powerful features that cannot be emulated that easily. BTW, GreaseKit has a branch in their SVN repository that contains the GM_* API, it's only a matter of time and good will before someone put it back.

 
Descriptor Scriptwright

@greut, I believe (but don't know for sure) that it has to be better in order to enable a GM script to run with GreaseKit. As in ECMAScript. But maybe I'm wrong, as I don't understand why that code above includes typeof ActiveXObject.

 
dob Scriptwright

Also you won't be able to fake cross domain xhrs...

 
nathaniel_hi... Scriptwright

I'm using 1.5, the GM_ functions still aren't back. Pretty useless like that. Does anyone know when they'll be brought back? Also your xmlhttprequest functions does not emulate events like onload, does it? Maybe you could rip something from prototype/mootools or so, so that this functionality isn't lost? I'd try myself, but the only thing I installed GreaseKit for, doesn't work, so I'm pretty let down and don't feel like wasting more time.

Can you tell how to work around unsafeWindow.so.getVariable ?

 
dob Scriptwright

var scripts = document.getElementsByTagName("script");
scripts.forEach(function(s) {
	eval(s.textContent);
});

This is a horrible solution, but maybe you get the idea.

 
phoque User

Your GM_xmlhttpRequest doesn't seem to work with my script... :-/

 
phoque User

More details: I'm getting "Refused to set unsafe header Connection" on Safari. Maybe that means that the function is actually working... but I can't find any proper documentation on that message.

Cross
Presentational HTML allowed.
Use <code> for inline code and <pre> for code blocks. Use &lt; and &gt; for literal < and >.
We help break paragraphs and link your links.
or cancel