I stripped down a test case, which triggers a "Greasemonkey access
violation: unsafeWindow cannot call GM_setValue." error in the JS
Console every time it runs, and exhibits some really bizarre behavior
with GM_getValue. Does anyone know how to explain this? (Namely, on GM
0.8.20090123.1 the second GM_getValue("type") returns the injected
textContent of the <script> tag, which is one of the strangest things
I've seen in any program I've ever written.)
Here it is:
// Attempts to set the type config entry to the value of the most recently passed element text
function listener(e) {
GM_setValue("type", e.target.textContent);
}
document.body.addEventListener("DOMNodeInserted", listener, true);
// Fires the DOM event by adding a node
function trigger(type) {
var n = document.createElement("span");
n.style.display = "none";
n.textContent = type.toString();
document.body.appendChild(n);
}
trigger('in GM script context');
alert("Type: " + GM_getValue("type"));
// Inject script, including trigger function, and execute
var nodScript = document.createElement("script");
nodScript.textContent = trigger + "\n\ntrigger('in injected page script');";
alert("nodScript: " + nodScript.textContent);
document.body.removeChild(document.body.appendChild(nodScript));
alert("Type: " + GM_getValue("type"));
function listener(e) {
GM_setValue("type", e.target.textContent);
}
function trigger(type) { var n = document.createElement("span");
n.style.display = "none";
n.textContent = type.toString();
document.body.appendChild(n);
}
function showType() {
alert("Type: " + GM_getValue("type"));
}
document.body.addEventListener("DOMNodeInserted", listener, true);
trigger('in GM script context');
showType();
location.href = "javascript:" + trigger + "\n\ntrigger('in location hack context');"
showType();
cc wrote:
> Still has the problem with GM_functions being disallowed (is
> this a deliberate security choice?)
Digging into the GM source code, it sure looks like it was a deliberate
choice (which I'd rather like to hear the reasoning behind; I can't
myself quite figure it out). It's possible that it was originally
designed to allow this kind of thing but Firefox's stack-walk code
changed under GM_apiLeakCheck; I very much doubt this, though, as it'd
take quite a bit of changes to make this much of a difference. (Right
now, the stack starts in a javascript: URI for the location hack, which
makes perfect sense; therefore, because the root is untrusted, the whole
tree is untrusted.)
So I guess I'll have to fall back to a polling design.... On the upside,
if I get some good code working, maybe I can work it into a generalized
library for this kind of thing. Before I do that, though, anyone know of
a library that already exists for that? ;-)
In reverse-chronological but less-to-more-useful order:
http://groups.google.com/group/greasemonkey-dev/t/3eee8cfb32677bf1
http://groups.google.com/group/greasemonkey-dev/t/68a9fc3101cfdade
http://groups.google.com/group/greasemonkey-dev/t/933ecdb307c4386d
Note that the links here provide a direct and (mostly) straightforward
workaround, though it certainly could be easier to find.
Actually this is documented, if not in a wonderful location:
http://wiki.greasespot.net/0.7.20080121.0_compatibility#Workaround
Sorry you had trouble.