Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a "config" or "options" page for a Greasemonkey script

Tags:

I've written a simple Greasemonkey script, and I'm trying to create a "config" page for this script (like the one that is used for Google Chrome extensions. ) Would there be any way to create a config page for a userscript, like the "options" pages for Google Chrome extensions? There isn't any way to include an .html page as part of a Greasemonkey script (as far as I know), so I'm looking for other options.

// ==UserScript==
// @name       Redirector
// @namespace  http://use.i.E.your.homepage/
// @version    0.1
// @description  enter something useful
// @match      http://*/*
// @copyright  2012+, You
// @run-at document-start
// ==/UserScript==

redirectToPage("http://www.youtube.com/", "http://www.google.com");

function redirectToPage(page1, page2){
if(window.location.href.indexOf(page1) != -1){
    window.location.href = page2;
}
}
like image 209
Anderson Green Avatar asked Jan 29 '13 23:01

Anderson Green


People also ask

How do I make a Greasemonkey script?

In your Firefox window, once you've downloaded and installed Greasemonkey, there'll be a little monkey face in the right-hand corner of the status bar. Right-click on that and you'll get a menu that includes the option "New User Script". Click that and you'll get a dialog looking a bit like the box on the right.

Is Greasemonkey the same as Tampermonkey?

Tampermonkey is used to run so-called userscripts (sometimes also called Greasemonkey scripts) on websites. Userscripts are small computer programs that change the layout of a page, add or remove new functionality and content, or automate actions.

How do I write a script for Tampermonkey?

Tampermonkey has it's own built-in editor. Just hit the Tampermonkey button and select Dashboard. To get a new script, hit the little + tab in the upper right. You'll get a nice template with an IIFE (Immediately Invoked Function Expression) that you should put all your code in to avoid global namespace pollution.

How do I run a Greasemonkey script in Firefox?

Installing the Greasemonkey Extension. Click on the Firefox drop-down menu at the top left of the browser and select Add-ons. Type Greasemonkey into the add-ons search box at the top right of the browser. Find Greasemonkey in the list and click on Install.


2 Answers

There are a few libraries which provide config pages for userscripts:

1) GM_config

Example GM_config dialog

2) MonkeyConfig

Example MonkeyConfig dialog

3) GM_registerMenuCommand Submenu JS Module


The usage varies per library, but typically you grant the permissions they need such as GM_getValue and GM_setValue, and require the library via the @require directive, e.g.:

// ==UserScript==
// @name          My Userscript
// @description   An example userscript with a config page
// @version       0.0.1
// @require       https://www.example.com/lib/myconfig.js
// @grant         GM_getValue
// @grant         GM_setValue
// @grant         GM_addStyle
// @grant         GM_registerMenuCommand
// ==/UserScript==

const config = new MyConfig({ ... })

You then register a menu command which opens the config page/dialog, e.g.:

GM_registerMenuCommand('Configure My Userscript!', () => {
    config.open()
})

In the case of MonkeyConfig, it can register the command for you:

const config = new MonkeyConfig({
    title: 'Configure My Userscript!',
    menuCommand: true,
    // ...
})

For advanced uses, the configurator may allow listeners to be registered for the close/cancel/save events, as well as providing control over the CSS, and other options. Detailed instructions can be found on the GM_config wiki and the MonkeyConfig homepage.

like image 162
chocolateboy Avatar answered Oct 05 '22 23:10

chocolateboy


If you are using it for chrome, then it isn't Greasemonkey but Tampermonkey.

You may consider using GM_getResourceText, paste your html to pastebin.com (or similar) and add the link as one of @resource to the metadata block. At least, I know it works to Greasemonkey.

For example:

// @resource configHtml http://pastebin.com/raw.php?i=2RjKfwJQ

// ... some DOM node that you will append to the current page
node.innerHTML = GM_getResourceText("configHtml");
like image 33
w35l3y Avatar answered Oct 05 '22 23:10

w35l3y