使用 Greasemonkey 添加我自己的 onclick 函数
我想向页面添加一个带有 onclick 事件的按钮。它最终会打开一个覆盖 div。
但是,我什至无法运行警报。
我使用了几种技术,例如 unsafeWindow
,并将脚本标记添加到正文,但仍然不起作用。
这是我现在正在使用的代码:
function main() {
var script_source = '<script type="text/javascript">function doenwedan(){alert("hebben we gedaan!");}</script>';
var link_knop = '<br /><br /><br /><br /><button onclick="doenwedan()" value="gaan we doen">gaan we doen</button>';
var orginele_pagina = document.getElementById('ds_body').innerHTML;
document.getElementById('ds_body').innerHTML =orginele_pagina + link_knop + script_source ;
}
main();
我怎样才能让它工作?
我需要这样的技术,因为当调用覆盖 div 时,将需要新的函数,所以如果要注入它们,那么......
I want to add a button, with an onclick event, to the page. It will eventually open an overlay div.
But, I can't get even an alert running.
I've used a couple of techniques, like unsafeWindow
, and adding the script tags to the body, but still doesn't work.
This is the code that I'm using right now:
function main() {
var script_source = '<script type="text/javascript">function doenwedan(){alert("hebben we gedaan!");}</script>';
var link_knop = '<br /><br /><br /><br /><button onclick="doenwedan()" value="gaan we doen">gaan we doen</button>';
var orginele_pagina = document.getElementById('ds_body').innerHTML;
document.getElementById('ds_body').innerHTML =orginele_pagina + link_knop + script_source ;
}
main();
How can I get this working?
I need a technique like this, because when the overlay div is called, there will be new functions needed, so if got to inject them then...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
几件事:
innerHTML
。它破坏了事情并导致尝试正则表达式 HTML 的诱惑。把它们放在一起,你的脚本将变成这样:
Several things:
innerHTML
if you don't have to. It busts things and leads to the temptation of trying to regex HTML.Putting it all together, your script would become something like: