简体   繁体   English

Greasemonkey-按钮已添加到 <div> 我添加了,而不是调用我的oilmonkey函数

[英]Greasemonkey - Button Added to a <div> I added, not calling my greasemonkey functions

So I have a button that I added through GreaseMonkey: 因此,我有一个通过GreaseMonkey添加的按钮:

var input = document.createElement('input');
input.type = 'button';
input.value = 'Off';
input.onclick = turnTheWheelsofTime;

This button worked fine when I added it to the page using this: 当我使用此按钮将其添加到页面时,此按钮可以正常工作:

document.body.appendChild(input);

However, I wanted to position my button in a non-moving box on the page, instead of just appending it to the end (this is my real concern, the following is just how I applied it, so if you know a better way I would be happy to hear) 但是,我想将按钮放置在页面上的固定框中,而不是仅将其附加到末尾(这是我真正关心的问题,下面就是我的应用方式,因此,如果您知道一种更好的方法,会很高兴听到)

I found a solution that used AJAX. 我找到了使用AJAX的解决方案。 I added these to my script: 我将这些添加到脚本中:

// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
 // @grant       GM_addStyle`<br><br>
$("body").append ( '\
    <div id="displayBox">\
    <p id="displayBoxText">Test</p>\
    <button id="gmCloseDlgBtn" type="button">Close popup</button>\
    </div>\
' );

To add the button to this new <div> I created, I got the element and called it box_element, and used this line: 要将按钮添加到我创建的这个新的<div> ,我获得了元素并将其命名为box_element,并使用了以下这一行:

box_element.appendChild(input);

instead of the previous: 而不是以前的:

document.body.appendChild(input);

This works sort-of perfectly. 这工作得很完美。 The box is added to my <div> , I didnt paste this in the question but I used some CSS with GM_addStyle, and it all got applied to my <div> . 该框已添加到我的<div> ,我没有将其粘贴到问题中,但是我将一些CSS与GM_addStyle一起使用,并且所有内容均已应用于我的<div> Everything looks beautiful, but the button doesn't do anything. 一切看起来都很漂亮,但是按钮没有任何作用。

Why? 为什么? I can have it do a console.log('test'); 我可以让它做一个console.log('test'); which works fine, but when I try to use functions from my Greasemonkey script, nothing happens? 哪个工作正常,但是当我尝试使用Greasemonkey脚本中的函数时,什么都没发生?

In case anyone was curious, I figured out the error. 万一有人好奇,我找出错误。 It was actually related to introducing the @grant into my script. 它实际上与将@grant引入我的脚本有关。 My function turnTheWheelsofTime used javascript functions from the page, and apparently using @grant sandboxes everything. 我的函数turnTheWheelsofTime使用了页面中的javascript函数,并且显然使用@grant沙箱将所有内容都使用了。 The sandboxing from the @grant is actually what broke the function @grant的沙箱实际上是破坏功能的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM