15

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.

8
  • 4
    Bookmarklets. Also see bookmarklets.com, plus many others, including a Stackapps site. Jun 9 '12 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
    Jun 9 '12 at 4:41
  • Yeah, the jsFiddle “tidy up” bookmarklet is particularly useful for me. Jun 9 '12 at 4:44
  • @JaredFarrish - What's the difference between that "Tidy up" button? Jun 9 '12 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. Jun 9 '12 at 5:07
13

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.

3
  • Yes, and like I said even with the workarounds (unsafe..., injection, and address bar use) in practical situations I find nothing ever works. This was no different for my particular situation now: "$Acb.close('Ad160x600_0', InboxPage.onSkyscraperClose);" on hotmail.com (auto clicks the close ad link to give you more screen room). works perfect in the console every time.
    – Jonathon
    Jun 9 '12 at 12:41
  • 1
    Your "simpler" alternative would fail the same way then. On that page, there is apparently an AJAX delay before the function you want is available. If it "works from the console", it's because you run it several seconds after the page has loaded. The solution is either to use a timer to wait an additional couple of seconds (or however long it takes), or to poll for the target code as in the second part of this answer. AJAX delays are always highly dependent on a page-by-page details. There is no simpler approach. Jun 9 '12 at 17:31
  • I just spent an hour trying to make a simple script work. you are a Godsend Feb 9 '15 at 19:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.