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

前端 未结 2 1661
太阳男子
太阳男子 2020-12-13 10:20

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

I found befo

相关标签:
2条回答
  • 2020-12-13 11:06

    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;            \
        }                                   \
    " );
    
    0 讨论(0)
  • 2020-12-13 11:09

    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

    0 讨论(0)
提交回复
热议问题