/    Sign up×
Community /Pin to ProfileBookmark

Greasemonkey @require: can’t get it to work at all

I have this script that automatically creates a document outline table of contents from all the HTML header elements and inserts it at a comment node (or just before the first header if the special comment node is missing). The script requires three external JS files to run properly. Instead of inserting the markup and script required into the HTML docs I want it inserted into, I use Greasemonkey to look for certain characteristics (strings) of the document, then insert the required elements if the characteristics are found.

Greasemonkey does not insert <script> elements with code into rendered HTML documents however. The metadata section takes an ‘@require’ key with a value of the name of a script file to include. I need to do this for my three external JS files so that the “injected” function calls are defined. The FF Error Console (the way to monitor Greasemonkey activity) indicates that the “required” files are not being loaded because the function calls come back “undefined.”

Note that I have another Greasemonkey script which re-styles the document by examining the browser protocol for “file://” or “http://” and then it creates a <link> elements and defines the “href” attribute, depending on the protocol. It works. BUT it does not make use of the @require key. GM is at least working in that regard.

Has anyone else had problems with the @require key of GM? What did you do about it?

Here is the code of my latest GM user-scripted (“injected”) JS file:

[CODE]// ==UserScript==
// @name Document Outline ToC
// @namespace text/javascript
// @require colorContrasting.js
// @require dom1.js
// @require htmlTOCgenerator.js
// @include file:///d:/The%20Complete%20Reference/Proteomics/Mass%20Spectrometry.html
// ==/UserScript==
//alert(“typeof(getElemComputedStyle) => ” + typeof(getElemComputedStyle));
// var node = document.createElement(“script”);
//node.type = “text/javascript”;
// node.src = “file://c|/include/htmlTOCgenerator.js”;
// document.getElementsByTagName(“head”)[0].appendChild(node);
var htmlTOCobj = new htmlTOC(document);
htmlTOCobj.generateTOC();
[/CODE]

MORE INFO:

Note that I commented-out code that actually creates a <script> element with “src” attribute, which is not the way to bring in external JS files using GM.

I put the three script files in the same directory as the HTML doc only for de-bugging purposes.

Normally I keep all (mostly) debugged scripts-in-files in a single “include” directory right off the C: root of a Windows Vista system, and I hard link references to it from just about everywhere. In my dev browser (Firefox 3.5x), I make use of the “file://” protocol and of “http://localhost/“… as I run Apache 2.2 on my notebook. Apache follows Windows-based hard-links to directories by the way, in addition to using its own aliases to track documents out of its document tree.

to post a comment
JavaScript

0Be the first to comment 😎

×

Success!

Help @mavigozler spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...