Greasemonkey script stop working when require this js - greasemonkey

My script stop working when I use #require with this js library.
It works in Tampermonkey.
This is the minimal code.
// ==UserScript==
// #name test
// #version 1
// #namespace test
// #require https://cdnjs.cloudflare.com/ajax/libs/masonry/3.2.3/masonry.pkgd.min.js
// #run-at document-end
// #grant none
// #match *
// ==/UserScript==
alert('999');
ps: why did someone vote down this question? what's wrong?

I don't know why but I've found a solution: insert a script tag into the head tag.

Related

looking for a way (with grease/tampermonkey) to replace part of url(s) on page

So I frequent a webpage which uses ouo.io to try and make some advertisement money on links. Lucky me that that particular redirector-service doesnt alter or hide the original url in any way, so I was thinking that perhaps it would be possible (with a tamper/greasemonkey script I guess) to remove the ouo part from the urls automatically
The urls are like this
https:// ouo.io/qs/pRqHNWPK?s=https://destination.url/
(The space between the protocol and the ouo part was intentional because without it this link trips the URL-shortener warning thus not letting me post)
Anybody who has a tampermonkey script that would be adaptable for this situation? I tried messing around and writing something myself but that didn't work (or do anything at all lmao).
Here is a sample code to show you how it can be done ...
(remove extra space added before ouo)
Redirecting based on location.href of the new page
// ==UserScript==
// #name Remove Redirect
// #match https:// ouo.io/qs/*
// #grant none
// #version 1.0
// run-at document-start
// ==/UserScript==
const redirect = location.search.match(/http.+/); // find redirect URL
if (redirect) {
window.stop(); // stop loading the page
location.href = decodeURIComponent(redirect[0]);
}
You can also change the links on the pages. It is much more resource intensive since it has to run on every page thus not recommended. Here is an example ...
// ==UserScript==
// #name Remove Redirect Link
// #match *://*/*
// #grant none
// #version 1.0
// ==/UserScript==
document.querySelectorAll('a[href^="https:// ouo.io/qs/"]').forEach(item => {
const redirect = item.search.match(/http.+/); // find redirect URL
if (redirect) {
item.href = decodeURIComponent(redirect[0]);
}
}):

Gitlab, how to change tabulation size on code review

While reviewing the code with gitlab merge request web page, tabulation is 8 spaces.
It makes the code difficult to read. How could I move it to 1 or 2 spaces ?
It seems to be impossible to achieve that by available GitLab settings, but this can be hacked using Greasemonkey/Tampermonkey browser extension:
// ==UserScript==
// #name Custom tab-width
// #version 1
// #grant none
// #include https://your.gitlab.server.com/*/diffs*
// ==/UserScript==
var style = document.createElement("style");
style.type = "text/css";
style.innerHTML = "span.line {tab-size:2; -moz-tab-size:2;}";
document.head.appendChild(style);
or by any other stylish-like extensions which allow you to attach any custom css rules to web pages:
span.line {tab-size:2; -moz-tab-size:2;}

Greasemonkey: leaving user configuration intact while updating

I've written a user script for Greasemonkey that requires some user configuration. To specify how they want the script to behave, a user needs to set a couple variables.
Right now, the script is set up like this:
// ==UserScript==
// #name My script
// #description A simplified example
// #include http://www.example.com/
// #version 0.0.1
// #updateURL https://www.example.com/myscript.meta.js
// ==/UserScript==
// Configuration
var config1 = "on";
var config2 = "off";
// Programs
[various functions that refer to the configuration variables]
I'd like to be able to update the script using Greasemonkey's automatic updates, while leaving the user's configuration lines intact. Basically, I don't want to force every user to redo their configuration after each update.
Is there a good method for updating a Greasemonkey userscript while leaving some configuration intact?
You may wish to utilise the greasemonkey functions GM_getValue() and GM_setValue(), which will store values that stay that way until they're changed again. The script can set values based on user needs, and get the value where required.
Specific GM functions require a special grant, in the metadata for the userscript:
// #grant GM_getValue
// #grant GM_setValue
Your code with the GM value functions may look like this:
var value = 18;
GM_setValue('dataName', value);
if (GM_getValue('dataName') == 18) ...
When you make updates to the script, instead of having the values set by GM_setValue overwritten, you could first check to see if they have been written:
var value = 18;
if (typeof GM_getValue('dataName') === 'undefined')
GM_setValue('dataName', value);
To enable users to control these settings, you could inject an HTML interface that shows the values of the database entries, and allows them to be set. This approach would then make such settings immune to script updates (as long as the script doesn't overwrite the database values).
Also, this question may also be a beneficial read.

Scriptish GM_getResourceText doesn't work

This works on Greasemonkey, but I don't know why it doesn't work on Scriptish:
// ==UserScript==
// #name myscript
// #namespace xxx
// #include http://*
// #version 1
// #grant GM_getResourceText
// #resource resourceName http://xxx.xxx/style.css
// ==/UserScript==
alert(GM_getResourceText('resourceName'))
The css file is actually downloaded, and console log says [myscript] not defined.
This has come up since Firefox update to version 30. Try to "enable #grant" in the settings of Scriptish. If you don't find this setting in your Scriptish options dialog, go to URL about:config and find the key extensions.scriptish.enableScriptGrant. If that doesn't work, go to scriptish download page and list all available versions. Version 0.1.12 works well with Firefox 30. Official latest version is 0.1.11.

In a Greasemonkey script, how can I target a page by its elements?

I would like to change a page using a Greasemonkey script, but the page is the result of a CGI POST call and thus the URL looks like this:
http://www.example.com/cgi-bin/foo.pl
So obviously I cannot use the #include in the Greasemonkey script, because that URL fits a lot of pages the app can generate.
I could just look for some elements on the page within the script before doing stuff:
if (document.getElementById('someIdentifier')) {
// do changes in here
}
But I was wondering if there is a better, maybe built-in way to do this.
Greasemonkey (and most userscript engines) has no built-in way to target a page by its elements. Your script needs to poll or use mechanisms like Mutation Observers to check for the desired elements.
A handy way to do this is to use the waitForKeyElements() utility, and you still want to use the #include, #exclude, and/or #match directives to narrow down the page firings as much as possible.
Here is a complete Greasemonkey/Tampermonkey script illustrating the process:
// ==UserScript==
// #name _Do something after select posts
// #include http://www.example.com/cgi-bin/foo.pl*
// #require http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js
// #require https://gist.github.com/raw/2625891/waitForKeyElements.js
// #grant GM_addStyle
// ==/UserScript==
/*- The #grant directive is needed to work around a design change
introduced in GM 1.0. It restores the sandbox.
*/
function processTargetElement (jNode) {
//***** YOUR CODE HERE *****
jNode.css ("background", "pink");
}
waitForKeyElements ("#someIdentifier", processTargetElement);

Resources