Simple Alternative to Greasemonkey
browser, google-chrome, greasemonkey, javascript
Solution
There 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.
Problem
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.