bulrush

GreaseMonkey

Sep 26th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!

Greasemonkey notes

Updated: Sep 30, 2018

Links

  1. Greasemonkey 4 notes for Firefox v57+
  2. Greasemonkey wiki
  3. Greasemonkey script forums
    1. Greasyfork forum
    2. Google forum about Grease Monkey.

Sources for scripts

  1. Find user scripts. Various script hosts.
    1. Greasyfork
    2. OpenuserJS
    3. Userscripts-mirror

Tutorials

  1. On Greasespot.
  2. From Haya Geek. 2013.

Voat stuff

  1. Banned users: https://voat.co/v/GreatAwakening/about/log/banned
  2. Removed submissions: https://voat.co/v/GreatAwakening/about/log/submission
  3. Removed comments: https://voat.co/v/GreatAwakening/about/log/comment
  4. 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

  1. GM_setValue(name, value) = set a value to variable 'name'. Supports only strings, booleans, and integers. No decimal/floating point numbers.
  2. GM_getValue(name, default) = get a value from variable 'name'.
  3. GM_log(message, level)
  4. GM_openInTab(url)
  5. GM_addStyle(css)
  6. 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

  1. Click Greasemonkey icon. Click New User Script.
  2. 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');

Add Comment
Please, Sign In to add comment