Greasemonkey notes
Updated: Sep 30, 2018
Links
- Greasemonkey 4 notes for Firefox v57+
- Greasemonkey wiki
- Greasemonkey script forums
- Greasyfork forum
- Google forum about Grease Monkey.
Sources for scripts
- Find user scripts. Various script hosts.
Tutorials
- On Greasespot.
- From Haya Geek. 2013.
Voat stuff
- Banned users: https://voat.co/v/GreatAwakening/about/log/banned
- Removed submissions: https://voat.co/v/GreatAwakening/about/log/submission
- Removed comments: https://voat.co/v/GreatAwakening/about/log/comment
- About: https://voat.co/v/GreatAwakening/about
Basics
Using variables and GM functions
Source: http://greasemonkey.mozdev.org/authoring.html, and https://wiki.greasespot.net/Greasemonkey_Manual%3aAPI
- GM_setValue(name, value) = set a value to variable 'name'. Supports only strings, booleans, and integers. No decimal/floating point numbers.
- GM_getValue(name, default) = get a value from variable 'name'.
- GM_log(message, level)
- GM_openInTab(url)
- GM_addStyle(css)
- GM_registerMenuCommand = Make menu item, GM_registerMenuCommand( "Hello world!", hello, "e", "control", "h" );
Show info
alert("My popup message"); // Show popup window
alert('My message has "quotes"'); // Msg contains double quotes.
consoler.log("Log this to console"); // Does not make a popup window
Make a new script
- Click Greasemonkey icon. Click New User Script.
- The user script will be given a random name but you can't see the new script name yet. Click the Greasemonkey icon again. Click the random script name that was created, click Edit. A basic script looks like this:
// ==UserScript==
// These @ directives MUST be in comments.
// @name Hello world
// @version 1
// @grant none
// @namespace http://hayageek.com
// @include is to use on hayageek.com only
// @include hayageek.com
//Script is included for the domain and sub-domains
// @include http://.google.com/
// ==/UserScript==
alert("Hello world");
Modify whole page style
document.body.style.fontFamily = "Verdana, sans-serif";
Modify page, get Youtube info and show it
if(window.location.href.indexOf("v=") > 0) //If it is a valid video
{
var userName="";
var title="";
var userVideos="";
var views="";
if(document.getElementById("eow-title"))
title = document.getElementById("eow-title").textContent;
var as = document.getElementsByTagName("a");
for(var i=0;i<as.length;i++)
{
var cls =as[i].getAttribute("class");
if(cls)
{
GM_log(cls+"<br>");
if(cls.indexOf("yt-user-name") >= 0)
{
userName = as[i].textContent;
}
else
if(cls.indexOf("yt-user-videos") >= 0)
{
userVideos = as[i].textContent;
}
}
}
if(document.getElementById("watch7-views-info"))
{
var viewsObj = document.getElementById("watch7-views-info");
var spans =viewsObj.getElementsByTagName("span");
for(var i=0;i<spans.length;i++)
{
var cls =spans[i].getAttribute("class");
if(cls.indexOf("watch-view-count") >= 0)
{
views = spans[i].textContent;
}
}
}
var div=document.createElement("div");
div.setAttribute("style",";border:1px solid red;padding:10px 10px 10px 100px;");
div.innerHTML ="<h1>"+title+"</h1><br>";
div.innerHTML +="<b>Uploaded By:</b>"+userName+"<br>";
div.innerHTML +="<b>Uploaded Videos: </b>"+userVideos+"<br>";
div.innerHTML +="<b>Total Views: </b>"+views+"<br>";
document.body.insertBefore(div,document.body.firstChild);
//hide sidebar
var sidebar = document.getElementById("watch7-sidebar");
if(sidebar)
{
sidebar.style.display="none";
}
}
Change all words in page
document.body.innerHTML = document.body.innerHTML.replace(/hello/g, 'hi');
Create element and insert it
var div=document.createElement("div");
div.setAttribute("style",";border:1px solid red;padding:10px 10px 10px 100px;");
div.innerHTML ="<h1>"+title+"</h1><br>";
div.innerHTML +="<b>Uploaded By:</b>"+userName+"<br>";
div.innerHTML +="<b>Uploaded Videos: </b>"+userVideos+"<br>";
div.innerHTML +="<b>Total Views: </b>"+views+"<br>";
document.body.insertBefore(div,document.body.firstChild);
Highlight words
function highlightWord(word) {
var xpath = "//text()[contains(., '" + word + "')]";
var texts = document.evaluate(xpath, document.body, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for (n = 0; n < texts.snapshotLength; n++) {
var textNode = texts.snapshotItem(n);
var p = textNode.parentNode;
var a = [];
var frag = document.createDocumentFragment();
textNode.nodeValue.split(word).forEach(function(text, i) {
var node;
if (i) {
node = document.createElement('span');
node.style.backgroundColor = 'lightgreen';
node.appendChild(document.createTextNode(word));
frag.appendChild(node);
}
if (text.length) {
frag.appendChild(document.createTextNode(text));
}
return a;
});
p.replaceChild(frag, textNode);
}
}
highlightWord('Basel');
highlightWord('Bern');