I love the concept of GM, but in practice unless you use it all the time and are an absolute JS god it is impossible to use (maybe I just suck?).
It would be so useful to have a little extension that took a few lines of JS and ran them after page load for a certain site. But that is not what GM does. With GM you have to deal with multiple frames and those layers upon layers of annoying security issues and scope. Even when you just ignore proper procedure and use unsafewindow or one of the other hacks it often still does not work.
It is so easy to come up with JS that you can run in the browser console that will do what you want, but this never works when transferred to a userscript. is there any settings in greasemonkey that I can change or a different extension entirely that is geared towards ease of use?
Note: I use Chrome, so bonus points for a solution that works for that particular browser.
Summery: I want a way to automatically run scripts with identical scope/permissions as the console on specific pages.
I love the concept of GM, but in practice unless you use it all the time and are an absolute JS god it is impossible to use (maybe I just suck?).
It would be so useful to have a little extension that took a few lines of JS and ran them after page load for a certain site. But that is not what GM does. With GM you have to deal with multiple frames and those layers upon layers of annoying security issues and scope. Even when you just ignore proper procedure and use unsafewindow or one of the other hacks it often still does not work.
It is so easy to come up with JS that you can run in the browser console that will do what you want, but this never works when transferred to a userscript. is there any settings in greasemonkey that I can change or a different extension entirely that is geared towards ease of use?
Note: I use Chrome, so bonus points for a solution that works for that particular browser.
Summery: I want a way to automatically run scripts with identical scope/permissions as the console on specific pages.
Share Improve this question edited Jun 9, 2012 at 12:56 Jonathon asked Jun 9, 2012 at 4:34 JonathonJonathon 2,6995 gold badges29 silver badges50 bronze badges 8- 4 Bookmarklets. Also see bookmarklets.com, plus many others, including a Stackapps site. – Jared Farrish Commented Jun 9, 2012 at 4:35
- Ah, so that is what those are. These would be useful in many situations, but what is so great about userscripts is that they automatically runs on the desired pages. – Jonathon Commented Jun 9, 2012 at 4:41
- Yeah, the jsFiddle “tidy up” bookmarklet is particularly useful for me. – Jared Farrish Commented Jun 9, 2012 at 4:44
- @JaredFarrish - What's the difference between that "Tidy up" button? – Derek 朕會功夫 Commented Jun 9, 2012 at 4:47
- 1 There is an extension for chrome that does just that but I can't remember the name of it. Just search for JavaScript userscripts or something like that and you'll find it. (Not the sumo one). I'll check back tomorrow with a link. – Joseph Marikle Commented Jun 9, 2012 at 5:07
1 Answer
Reset to default 15There is no simpler alternative to Firefox's Greasemonkey or to Chrome's userscripts that runs user JS automatically. You could write your own extension/add-on, but there wouldn't be much point to it.
If you don't care about the awesome extra power that GM and userscripts provide and always want to just "(take) a few lines of JS and (run) them after page load for a certain site" -- ignoring iframes, then just use the following code as a base-template for all of your scripts:
// ==UserScript==
// @name _Base template for simple, cross-browser, JS injection.
// @match *://YOUR_SERVER.COM/YOUR_PATH/*
// @run-at document-start
// ==/UserScript==
if (window.top != window.self) //-- Don't run on frames or iframes.
return;
function scriptMain () {
// PUT ALL OF YOUR CODE HERE, INCLUDING ANY FUNCTIONS YOU CREATE.
console.log ("Hello World!");
}
window.addEventListener ("load", scriptMainLoader, false);
function scriptMainLoader () {
addJS_Node (null, null, scriptMain);
}
function addJS_Node (text, s_URL, funcToRun) {
var D = document;
var scriptNode = D.createElement ('script');
scriptNode.type = "text/javascript";
if (text) scriptNode.textContent = text;
if (s_URL) scriptNode.src = s_URL;
if (funcToRun) scriptNode.textContent = '(' + funcToRun.toString() + ')()';
var targ = D.getElementsByTagName ('head')[0] || D.body || D.documentElement;
targ.appendChild (scriptNode);
}
Note that the @run-at document-start
is required (for Chrome) but your code will still fire at document load.