How to use a greasemonkey function - javascript

I'm new to greasemonkey and I've a script (which I'm serving) that uses GM_xmlhttpRequest. I was wondering what I've to reference in order to use this function. Like I said, I'm serving this script so I've no idea if the user has GreaseMonkey installed.
Thanks

You can not "serve" script that uses GM_xmlhttpRequest(). GM_xmlhttpRequest() will run in the protected Greasemonkey sandbox (or the protected Chrome userscript sandbox) only.
You can host a proper user script and ask that the user install it, but the user is under no obligation to do so.
If you want your page to have some king of cross-site AJAX capability, you cannot use GM_xmlhttpRequest() in its JS. You'll have to use one of the standard workarounds.

If you installed scripts appropriately (like from userscripts.org), the GreaseMonkey should be able to detect them. You can see what scripts are currently being used by right-clicking on the GreaseMonkey icon on the lower right-hand side of your screen. Some scripts work automatically, and some scripts require the browser to be restarted first. When Greasemonkey detects a page that a script applies to, it automatically runs the script. No need for you to do anything.

Related

Is it a good use case for eval in Chrome extension?

I am developing a Chrome extension that absolutely always needs to run with the newest code.
Now, this is a problem that I am not quite sure how to solve while not going for eval() alike functionality.
I designed it to fetch the newest script from server over HTTPS, then execute it using new Function()
It's absolutely most important to have extension run using newest code for every user and updates managed by Google don't solve that problem cause they are usually delayed or require user to update it manually.
CSP only allows for scripts executed from domain specified by me, but I am also using unsafe-eval although I can encode these scripts using hash for more security.
Scripts require access to page DOM and chrome.* API. I can't just specify them as script src in popup, because that's not the point.
Are there better solutions to this problem?

Is there any way to run a userscript without using Greasemonkey or Tampermonkey?

Is there any way to run a user script without running it through Greasemonkey/Tampermonkey?
I know its a simple question but I can't find any information on this...or if it's even possible.
Can the user script be edited so it doesn't rely on Greasemonkey?
It's up to the browser you are using to enable support for running arbitrary javascript on page load.
Chrome has limited userscript support, but is nowhere near as easy or powerful as Tampermonkey.
Tampermonkey and Greasemonkey (and other browser extensions) were created to address the userscript need.
You do have options for running javascript after a page has loaded though. You can use the developer tools console or you can create a Bookmarklet.
On firefox:
You can use the Scriptish addon (an alternative to greasemonkey for automatically running userscripts).
There is the Developer Scratchpad for editing, saving, loading and running scripts (and userscripts) manually. Also, can be applied on either the current document or browser chrome. Accessed via shift-F4 or Developer > Scratchpad.
And then there is also GCLI (Graphical Command Line Interpreter) which you can write routines for via a mozcmd file and is run manually. You'll need to modify the userscript a bit to access window, document objects. Accessed via shift-F2 or Developer > Developer Toolbar.
This one is a bit more involved, but if you want to go the addon route, you can write an addon using the Addon SDK, and use the page-mod API which injects a userscript into a page automatically.
In Google Chrome or Firefox you can open the DevTools (e.g. by right-clicking the page and selecting Inspect) and then paste the code in the Console tab and press Enter to run it.
In Google Chrome DevTools you can also create a js snippet and run it on demand whenever you need it.
You can also use Chrome DevTools Snippets:
Right-click anywhere on the page and select Inspect.
Select the Sources tab — you will see a number of subtabs: Page, Filesystem, Override, Content scripts and finally Snippets (you might need to click >> in order to see it).
Click the plus button ➕ to add the js code, and use right-click > Run to execute the code.
NOTE: If the original script uses extra js libraries (jquery, etc) and the libs are not already preloaded on the page, then you'll need to load them yourself (see this).
Unlike the code you run from the Console, you can set breakpoints in your JS snippets.

Can I write a private greasemonkey script?

I want to write a greasemonkey script for my client to automate his tasks, however I don't want to share the script with others (as obviously, my client would not be too happy with this!)
How can I do write a greasemonkey script without sharing it with others?
The question is not clear. But if you mean, "Can I stop my client from seeing the Greasemonkey script's code and possibly sharing it?"
Then the answer is "NO". Anyone using Firefox can see/edit the script's code via the Greasemonkey menu or the User Scripts tab in the Add-on manager.
This is by design. The producers of Greasemonkey (and most script authors) do not want any possibility of hidden or encrypted scripts being foisted on unsuspecting users.
~~~
Additionally, the script source can be seen by anyone with access to the client-machine's file system.
If you mean:
How can I run/install the script in my browser without uploading it to the userscripts site?
That's easy...
Save your script to to a file, EG MyScript.user.js. Keep track of where you saved it, and make sure it's not in a system temp folder.
In Firefox, choose Open File (CtrlO).
Navigate to where you'd saved your script and open it.
Greasemonkey will pop up a dialog asking if you want to install the script. Press Install.
See "Installing Scripts" in the documentation.
Make site with a link like this
Install special script xx
and save your script at
http://yourserver.com/yourscript.user.js
Grease Monkey and Tamper Monkey will see a javascript file and if it ends with user.js it will ask to install it. Give the url with the install link to your client. You can protect your site with a password.

Detecting if java is installed and enabled with javascript

I need to be able to determine if a user has Java installed and enabled through JavaScript. The 1st requirement is easily done, I'm using the deployment toolkit script:
http://download.oracle.com/javase/6/docs/technotes/guides/jweb/deployment_advice.html
However, in the situation where a user has java installed, but they have disabled it (NOT disabled support in the browser, but disabled via the java control panel in Windows), the installed versions still show up in calls to deployJava.getJREs(). Is there any way to detect versions of Java that are both installed AND enabled using JavaScript?
See Detect if an applet is ready at Real's HowTo. AFAIU that should only be true once the applet is loaded and started.
There are also ways to call applet methods directly from JavaScript, if the first technique fails, you might look to that in order to ensure the applet is both loaded and working.
Another approach is to set a meta-refresh redirect element in the HTML, then have the applet call a JS to cancel the redirect if it loads successfully. The idea would be to set the href of the redirect to point to a page with words to the effect 'Sorry, our applet failed to load within NN seconds. Click -here- for support.'

How can I test JavaScript without placing it on the website?

I'm talking about something like GreaseMonkey but that would accept the script just as it would be on the website. Adding external scripts to Greasemonkey has been a pain for me so far.
So, I have a client who wanted me to write a specific script for him. Because the script reads the URL of the page visited by a user I can only test it on the website but i don't have access to the source code of the website. I'd like to make sure I deliver to the client a 100% working script so I would love to test it first.
How can I do that? Any plugins that would just allow me to copy the script and would run it every time I load a page of the website?
Obviously, if you can, you want to set up a copy of the page on which the script operates, on a local web server where you can play around with things.
If that isn't possible for whatever reason, you can inject your script directly into their site when you're looking at it with your browser using a bookmarklet. The code to do it is roughly:
var script = document.createElement('script');
script.src = "...the path to your script file, ideally on a local web server rather than a file:// path...";
document.body.appendChild(script);
Once you've tweaked the above (pretty much just supplying the src value), you can turn it into a bookmark via the Crunchinator. Once you have your bookmarket, just visit the site you're developing this for and click your bookmarklet, and your script will be added to the page (just for you, obviously, and just for that visit to the page).
Then your develop/test cycle becomes:
Modify the script file (for instance, to fix a bug)
Open their site
Click your bookmarklet to add your script file to the page
Using something like GreaseMonkey can lead to unexpected results since GM runs outside of the Browsers Sandbox and GM scripts always run after everything else has loaded.
My solution for this would probably be:
Setup a local WebServer
Use "Save page..." to get the page contents, then put them on your localhost
Now add your script to the page etc. and make it work
That gives you A) A flexible development environment and B) more "realworld" results, hell you can even edit you hosts file to use the same URL that your client's page has (of course you need to re-edit the file if you want to visit the original page) and C) you can test in IE etc. too.

Categories

Resources