Discussioni » Sviluppo

UserScript Hooks and Environment

§
Pubblicato: 19/08/2016

UserScript Hooks and Environment

Hey guys, I have some familiarity with JS, but still new to Userscripts in general. I had a couple general questions relating to running userscripts conditionally, and some best practices.

It might be easiest to explain with the example I'm working on now. This is some work browser software I'm working on. Starting from the 'work order' page, I would like to use maybe a bookmarklet to find the link that connects to the equipment's record, opens it in a new tab/window, and then when that page loads, a userscript catches it and automatically clicks a link to the equipment's history.

For the most part, this is all extremely basic stuff. The question I have is how to hook that userscript only when the bookmarklet opens the link. When I normally click the equipment record link, I do not want the userscript to activate.

The simplest way I can think of to do this, is basically when the bookmarklet clicks the link, it adds some garbage data to the end of the URL; http://www.blah.com/moo/records/25?garbagedata=true. And basically have the Userscript only trigger when the url has the garbage data in it.

I think this will work fine for my application, though I haven't tested it. But this post is more of a general question: what are the best practices for something like this? Is there a better, smarter way to do this? Besides the rare chance of my garbagedata overwriting some variable and sending bad data, are there any glaring issues with this approach?


Is there any way to have like my own Javascript namespace that doesn't refresh or restart when the url changes, but stays consistent from the moment I open the browser until I close it, and all my scripts can access it? I'm assuming that can be very dangerous, but is it possible? And what are the dangers to be concerned with if it is? Or how about a dummy webpage that I use that holds onto my scripts between page urls that it switches between. If that's possible, would the different pages' javascripts mess with each other? Sorry if I'm lacking some basic understanding here for my assumptions.

And on a similar note, from what little I've read into, it seems like Greasemonkey and Tampermonkey have a ton of safety mechanisms built in that cause a lot of hassle when making your own scripts work together, or accessing the webpage's variables, etc. How many of these are for the sole purpose of protecting people who download scripts from other people over the internet not knowing or understanding what they do? I only plan on using my own scripts, period. And I may prefer that some of those mechanisms not be present as long as that's the case.

I mostly script with Tampermonkey on Chrome, but I also use Greasemonkey on Firefox.

woxxomMod
§
Pubblicato: 19/08/2016

Just invent a good unique parameter name because bookmarklets don't have access to anything outside the current window/document object by design.

§
Pubblicato: 20/08/2016
Is there any way to have like my own Javascript namespace that doesn't refresh or restart when the url changes, but stays consistent from the moment I open the browser until I close it, and all my scripts can access it?

If you include * then your script will load on every page and you can share Greasemonkey storage across sites. Not sure what you are trying to accomplish, so I'm not sure whether that answers your requirements.

Pubblica risposta

Accedi per pubblicare una risposta.