Is there a way to launch userscripts directly from browser (Firefox)? - firefox

I would like to run some userscripts to backup data.
The same scripts could be used on a variety of websites.
Scripts are basically the same, only some configuration changes from one script to another (what to scrape on the page, and where to save it).
Scripts will not alter the page in any way, just read it then call some REST API with the data
I do not want these scripts to be loaded on every web page, as they won't alter them, and depending on the page there is only one I'd like to run anyway.
So is there a way to launch these scripts directly from the browser (Firefox) ? From a bookmark or something ? Basically I want to click a button and have the script run like if I was running it from the js console.
Best I found for now is the "#run-at context-menu" directive of Tampermonkey. My script appears in a TamperMonkey context menu and is run on click.
However this menu is loaded on every page (#run-at context-menu ignores the #include directive and is applied everywhere), I'm not sure of the performance consequences, and it might be inconvenient if I end up with lots of scripts.
I guess I could also modify my scripts to be run on specific pages so that they add a floating button or something. But I'd rather not modify the pages, and it would be inconvenient on pages where I'd like to have several possible scripts.
I remember using a Greasemonkey script that added options on the context menu a long while ago. Would that be possible with Tampermonkey, implementing an equivalent of the #run-at context-menu myself that would only work on some pages ?
A more native way would be best, but I'm open to options.

Basically I want to click a button and have the script run like if I was running it from the js console.
From above, I assume the userscript does not use any GM API or use #require since those are not available in JS console.
If that is the case, FireMonkey has a feature to inject script (or CSS) from the toolbar popup.
After saving a script and disabling it (so it doesn't run automatically) or enabling on sites that the script MUST run, users can select the script in the toolbar icon popup and click the button to inject the script in the active tab.
For temporary scripts, FireMonkey also has a Scratchpad that you can paste JS (or CSS) into and run on any webpage by clicking the button. FireMonkey remembers the last pasted data so that it can be reused.
Note: Except TM that alters the CSP of websites, other managers are bound by the webpage CSP.

Turns out there's a function for that: https://wiki.greasespot.net/GM.registerMenuCommand
Your script just needs to declare a function, call GM.registerMenuCommand("label", your_function), and it becomes available on click in the GM/TM menu.
Contrary to the other answer this means you can use #require.
Contrary to the #run-at context-menu this means you can use #match to only apply it to the intended websites
Notes:
GM_registerMenuCommand in TamperMonkey
Needs the #grant to use the function

Related

Output outside of page

I'm trying to output some very detailed summary information about the properties of nodes in the DOM tree. Several years ago, I wrote a browser extension that created a new window and wrote the output there; that extension is no longer compatible with modern versions of Firefox, so I'm trying to port it into a Greasemonkey script instead of writing a whole new extension. Unfortunately, the cross-origin scripting policy seems to prevent me from actually writing anything to the window I've created. I have two questions:
1. Is there a better way to output too much text for an alert box, so that some or all can be copied, without modifying the original page's DOM tree?
2. If not, how do I create a window and output text to it from a Greasemonkey script?
I'm running the current version of Greasemonkey, by the way.
Is there a better way to output too much text for an alert box, so that some or all can be copied, without modifying the original page's DOM tree?
You can use shadow DOM to create a popup box that is isolated from the original CSS & JS.
If not, how do I create a window and output text to it from a Greasemonkey script?
AFA user-scripts, a new window is a new page. It will be a lot more work if you need to pass data to that window from a user-script. Therefore option 1 would be recommended.

Use selenium IDE to change Firefox Preferences for SDK Addon testing

I am using Selenium IDE to test some behavior in my FireFox SDK Add-On. For example, I load a page and determine that the content script is executing at intended. In my plugin, I use simple-prefs to set some user defined preferences.
For example, I would like to load a page and then ensure that if a preference is changed, that the content script received the update and made the necessary changes to the page based on the new setting.
when I try to navigate with Selenium-IDE to the plugin configuration page chrome://mozapps/content/extensions/extensions.xul?type=extensions / about:addons. I am able to use Selenium to select an entry (<richlistitem />), but I cannot click any of the buttons within the entry because they are not part of the XUL dom. I have tried using Selenium to send enter, tab, clicks, double clicks to the appropriate <richlistitem /> but there is no way of interacting with the "inner part" of the item.
I have also tried going down the path of using selenium to modify entries via about:config, however, the area with all of the entires is just an XUL <treechildren /> and you have no way of targeting individual entries.
Is there a convenient way to change addon setting as part of an automated workflow with selenium-ide?
It seems you should use Selenium Web driver , by driver you can set preference

Opening a bar on top of the page with firefox sdk

I am making a firefox pluggin and I want to open a "topbar" on a few websites. Realy, it would be a few informations about the curent page a link back to my own website. What would be te best way to do that ?
My first idea was to use content script, but that seems to be a very bad practice. I also read about panels, here are my questions :
How can I add my pannel just under the adressbar ?
How can I only open in it on the website I need ?
thx.
Using content script is completely fine.
It is modern, simple, less-code, more compatible way
to add top-panel to some web pages.
Also, code of content script is not injected to the web page, it just uses the dom and context; page script has no access (if you do not provide it explicit) to content script.
The only possible disadvantage is that panel would not look like native part of the browser.
If I convienced you to use content script:
The module you really need in your plugin page-mod
Using Add-on Builder you make have your plugin in a day

how to enable Safari Extensions when using a web view

I am using a Web view in my application, instead of open a Safari browser instance, so I noticed that Safari extensions doesn't work. Is there a possibility to enable this feature when using a custom web view in a Cocoa Application?
The reason by which I need to use Safari extensions is to inject javascript to whatever web page is loaded at one moment, so if is there another approach to do it without using extensions, welcome any suggestions or samples.
There's no way to use Safari extensions in a web view.
If your script isn't too big, how about formatting it as a "javascript:" bookmarklet and setting the web view's location to it?
[Edit: Stuff below added in response to questioner's request for "a bit more about that technique".]
Say you want to change the background color of the page to yellow and all the text to red. The javascript to do that would be something like:
document.body.style.backgroundColor = "yellow";
document.body.style.color = "red !important";
To turn the script into a bookmarklet, you just:
Wrap it in an anonymous function,
remove all line breaks,
(optionally) remove any unnecessary spaces,
url-encode it,
and prefix the whole thing with "javascript:".
So, the example would become:
javascript:(function(){document.body.style.backgroundColor%3D%22yellow%22%3B%0Adocument.body.style.color%3D%22red%20!important%22%3B%0A}());
Then you can set the webview's window.location to that string to "run" the bookmarklet.
Here is a page with an automatic script to bookmarklet converter that seems to work.

Functionality like "about:whatever" to display arbitrary data in Firefox

I'm writing a Greasemonkey script that has a fair few user settings (just using GM_getValue and GM_setValue).
What I'd like to be able to do is create a settings page for the script, and add that to the #include-d sites. So, for example, it'd run on:
#include http://www.greasemonkeyedsite.com/*
#include about:myScriptConfig
Then the script would check the URL of the site it's being called for. If it's the about: one it'd create and display a settings page, otherwise it'd just run the script as usual.
I came up with this under the impression that you could type about:(anything) and it'd show up fine, with just the text following the about: as the page content. I remember this working last time I checked it, but that was years ago.
Seems to be that you can't just display arbitrary data by use of about:x any more, though. Firefox just displays a "The URL is not valid and cannot be loaded" error.
I know about the data: URI protocol, but it's not suitable as entering it manually into the address bar doesn't lead to its own page.
Is there some equivalent behaviour? Or am I going to have to just have a "settings" button on the top corner of greasemonkeyedsite.com that hides and shows a settings div?
If you have a permanent web site, you could make a URL there that becomes the Greasemonkey script's settings page. That could even be a convenient URL that allows the user to download the script if he does not already have it installed, and you can, that way, also offer the user a software update when a new version of your script is released. (Just have the Greasemonkey script check some "current version" part of the settings page.)
As mentioned by jnpcl, it is possible to create a chrome:// URI within the browser, but as I understand it, that requires a full-fledged Firefox add-on rather than just a Greasemonkey script.
You could use a designated URL on the affected site if you do not have a permanent web site, like http://www.greasemonekyedsite.com/myGreasemonkeySettingsPage. Your script could then strip out the parts of their 404 page it does not need, and then it could insert its list of settings within.

Resources