Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple Alternative to Greasemonkey

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.

like image 457
Jonathon Avatar asked Jun 09 '12 04:06

Jonathon


People also ask

Is Tampermonkey and Greasemonkey same?

Tampermonkey is one of the most popular browser extensions with over 10 million users. Tampermonkey is used to run so-called userscripts (sometimes also called Greasemonkey scripts) on websites.

What can you do with Greasemonkey?

Greasemonkey can be used for customizing page appearance, adding new functions to web pages (for example, embedding price comparisons within shopping sites), fixing rendering bugs, combining data from multiple web pages, and numerous other purposes.

What is Tampermonkey used for?

Tampermonkey is a donationware userscript manager that is available as a browser extension. This software enables the user to add and use userscripts, which are JavaScript programs that can be used to modify web pages.

Does Greasemonkey work on Chrome?

Install Greasemonkey Scripts in Chrome You don't need to download an extension to enable Greasemonkey scripts like you do in Firefox; they work just like a standard extension in Chrome.


1 Answers

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.

like image 94
Brock Adams Avatar answered Oct 21 '22 11:10

Brock Adams