使用Greasemonkey将html内容添加到页面的基本方法? [英] Basic method to Add html content to the page with Greasemonkey?

查看:120
本文介绍了使用Greasemonkey将html内容添加到页面的基本方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有Greasemonkey方法将基本HTML内容追加到< body> 标记之后的页面末尾,或者在它结束之前?



我在方法之前/之后找到了,但我需要知道元素的名称,这些元素可能会改变页面到页面。 解决方案

快速和肮脏的方式:请仅为品牌 使用 innerHTML 内容。

  var newHTML = document.createElement('div'); 
newHTML.innerHTML ='\
< div id =gmSomeID> \
< p>某段< / p> \
等。\
< / div> \
';

document.body.appendChild(newHTML);









一个完整的脚本,显示了更好的jQuery方法(以及新的ECMAScript 6,多行字符串):

  // == 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 ==
// - - @grant指令用于恢复正确的沙箱。
$ b $(body)。append(`
< div id =gmSomeID>
< p>某段< / p>
等等
< / div>
`);









两种方法都会放置这样的新内容:


$ b

 <! - 新插入的内容这里 - > 
< / body>

这是个好地方。



即使HTML位于页面的 end 处,您可以使用CSS将其展示在任何地方,例如:

  GM_addStyle(\ 
#gmS​​omeID {\
position:fixed; \
top:0px; \
left:0px; \
} \
);


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..

解决方案

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;            \
    }                                   \
" );

这篇关于使用Greasemonkey将html内容添加到页面的基本方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆