I don't know exactly what toolbar you're talking about, but it's possible to add a menu command to Tampermonkey's action menu.
Since your script should be able to run at any page you need to @include all pages what might slow down pages with a lot of iframes a little bit.
This script will execute the main function (with the alert statement) only if the menu command was 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's main function here"); }, 'r');
Accessing the pages functions is possible by 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 just:
unsafeWindow.function_at_the_page();