ZK Zhao
ZK Zhao

Reputation: 21523

How to use userscripts in a site?(How does Greasemonkey work)

Userscripts are used by add-ons like Greasemonkey. But how can I utilize these scripts in my site?

For example, I found a script could eliminate ads in videos, and I want to put this script in my site, so people in my site wont't see those ads

I've tried just to save and put it in <script> in the head, but something just went wrong. (TypeError: document.body is null)

I also tried to put it just before </body>, no error reported this time, but something still doesn't work.

And I'm wondering how Greasemonkey works? Does it just insert scripts in the webpage? How does it load scripts into the pages?

Upvotes: 0

Views: 1116

Answers (1)

jakub.g
jakub.g

Reputation: 41268

Greasemonkey and Userscripts.org are meant to provide kind of lightweight extensions for the browser, not for the webpages. I.e. you install them as the user inside your browser.

Some of the trivial scripts might be incorporated into webpages, but in general, Greasemonkey has some "superpowers" to do things that ordinary JavaScript can't do due to security constraints. For instance, issuing (and reading response of) cross-domain XMLHttpRequest, interactions between frames from different origin etc.

Normally Greasemonkey scripts are executed when DOMContentLoaded is fired by the browser (this can be changed using @run-at directive to execute the script before anything is loaded).

If the script has @grant none in its GM meta section, then it could be perhaps incorporated into your site, but it depends on what it exactly does.

Bear in mind also that Greasemonkey is a Firefox extension (there's equivalent extension for Chrome and also some limited native support from Chrome and Opera). Many userscripts will likely fail in IE8.

Upvotes: 3

Related Questions