How to create toolbar button for Chrome user Tampermonkey script?

I wrote a custom text that I would like to run when I call it (not every time the corresponding web page is loaded). Ideally, I would like to create a toolbar button to run this script. How can I do that?

PS: I need it to run in the same context with web page scripts and be able to call functions built into it.

+3


source to share


1 answer


I don't know exactly which panel you're talking about, but you can add a menu command to the Tampermonkey action menu .

Since your script should work on any page, you need to @include all pages, which can slow down pages with a lot of frames a little.

This script will execute the main function (with a warning statement) only when a menu command is clicked.

// ==UserScript==
// @name       Run only on click
// @namespace  http://tampermonkey.net/
// @version    0.1
// @description  Run only on click
// @include    /https?:\/\/*/
// @copyright  2012+, You
// @grant      unsafeWindow
// @grant      GM_registerMenuCommand
// ==/UserScript==

GM_registerMenuCommand('Run this now', function() { 
    alert("Put script main function here");
}, 'r');

      



Page functions can be accessed in two ways:

function main () {
  window.function_at_the_page();
}

var script = document.createElement('script');
script.appendChild(document.createTextNode('('+ main +')();'));
(document.body || document.head || document.documentElement).appendChild(script);

      

or simply:

unsafeWindow.function_at_the_page();

      

+4


source







All Articles