Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic method to Add html content to the page with Greasemonkey?

Is there a Greasemonkey method to append basic HTML content to the end of a page right after the <body> tag, or right before it ends?

I found before/after methods but I need to know names of elements which may change page to page..

like image 439
bushdiver Avatar asked Jan 10 '13 02:01

bushdiver


2 Answers

The quick and dirty way: Please only use innerHTML for brand-new content.

var newHTML         = document.createElement ('div');
newHTML.innerHTML   = '             \
    <div id="gmSomeID">             \
        <p>Some paragraph</p>       \
        etc.                        \
    </div>                          \
';

document.body.appendChild (newHTML);


A complete script showing the somewhat better jQuery way (and with new, ECMAScript 6, multiline string):

// ==UserScript==
// @name     YOUR_SCRIPT_NAME
// @include  http://YOUR_SERVER.COM/YOUR_PATH/*
// @require  http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @grant    GM_addStyle
// ==/UserScript==
//--- The @grant directive is used to restore the proper sandbox.

$("body").append ( `
    <div id="gmSomeID">
        <p>Some paragraph</p>
        etc.
    </div>
` );


Both methods will place the new content like this:

<!-- NEW STUFF INSERTED HERE -->
</body>

Which is a good place for it.

Even though the HTML is at the end of the page, you can use CSS to display it anywhere with something like:

GM_addStyle ( "                         \
    #gmSomeID {                         \
        position:       fixed;          \
        top:            0px;            \
        left:           0px;            \
    }                                   \
" );
like image 79
Brock Adams Avatar answered Nov 16 '22 06:11

Brock Adams


If you don't want to have to muck around with having to escape your multi line html - you can put your HTML in local files, and load it using GM_getResourceText. Make sure you have enabled your Greasemonkey/Tampermonkey to use local files.

eg:

// ==UserScript==
// @name         Tamper Test
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        file:///C:/david/sandbox/tampermonkey/tamper.html
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js
// @resource html      file:///C:/david/sandbox/tampermonkey/foo.html
// @resource style     file:///C:/david/sandbox/tampermonkey/style.css
// @grant        GM_addStyle
// @grant  GM_getResourceText
// ==/UserScript==


(function() {
    'use strict';

    $("body").append('<div id = "dwj-tamper">new content from tamper script</div>');  

    GM_addStyle(GM_getResourceText("style"));    
    $("body").append(GM_getResourceText("html"));

})();

This solution is fine if the tamperscript is for yourself only. You can similarly save the resource online. For example:

// @resource pastebin http://pastebin.com/raw/9WfbN24i

//...

 $("body").append(GM_getResourceText("pastebin"));

also works

enter image description here

like image 6
dwjohnston Avatar answered Nov 16 '22 04:11

dwjohnston