Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Chrome扩展:从弹出html传递变量_Javascript_Jquery_Google Chrome_Google Chrome Extension - Fatal编程技术网

Javascript Chrome扩展:从弹出html传递变量

Javascript Chrome扩展:从弹出html传递变量,javascript,jquery,google-chrome,google-chrome-extension,Javascript,Jquery,Google Chrome,Google Chrome Extension,我正试图为chrome做一个非常简单的扩展,但我一直在从弹出式html传递变量 这是我目前掌握的代码: 舱单: { "background": { "scripts": [ "background.js" ] }, "browser_action": { "default_icon": "img/test.png", "default_popup": "popup.html", "default_title": "Auto Cl

我正试图为chrome做一个非常简单的扩展,但我一直在从弹出式html传递变量

这是我目前掌握的代码:


舱单:

{
   "background": {
      "scripts": [ "background.js" ]
   },
   "browser_action": {
      "default_icon": "img/test.png",
      "default_popup": "popup.html",
      "default_title": "Auto Clicker"
   },
   "description": "Auto click",

   "manifest_version": 2,
   "name": "Auto Clicker",
   "permissions": [ "activeTab" ],
   "version": "0.0.1"
}

background.js

chrome.extension.onMessage.addListener(
    function(request, sender, sendResponse) {
        switch (request.directive) {

             case  "popup-click-1":
            // execute the content script
            chrome.tabs.executeScript(null, { // defaults to the current tab

                file: "myscript.js", // script to inject into page and run in sandbox
                allFrames: true // This injects script into iframes in the page and doesn't work before 4.0.266.0.
            });
            sendResponse({}); // sending back empty response to sender
            break;

        }
    }
);

myscript.js

function foo(){
    document.getElementById('submit-button').click();
}

setInterval(function(){
    foo()}, 20000)

foo();

popup.js

 function clickHandler(e) {
        chrome.extension.sendMessage({directive: "popup-click-1"}, function(response) {
            this.close(); // close the popup when the background finishes processing request
        });
    }


 document.addEventListener('DOMContentLoaded', function () {
        document.getElementById('submit').addEventListener('click', clickHandler);

    })

popup.html

<html>
<head>
<title>test</title>
 <script src="popup.js"></script>
</head>
<body>
test

<form>
<input id="1" type = "text">
</br>
</form>
<button id='submit'> etst </button>
</body>
</html>

测试
测试

etst

到目前为止,我在单击submit按钮时运行foo(),该按钮每20秒运行一次。 我想要实现的是在弹出的html中添加一个数字,然后在myscript.js中使用该数字来设置setInterval函数的时间

下面是一个案例场景:


我打开页面,按下分机按钮。有一个带有表单的弹出窗口。我投了30000,然后击中了萨姆比特。这样,foo()将每30秒运行一次

不需要背景脚本

将脚本注入
popup.js
并传递值:

function clickHandler(e) {
    chrome.tabs.executeScript({
        code: "var timeout=" + document.getElementById('1').value,
        allFrames: true
    }, function(result) {
        chrome.tabs.executeScript({file: "myscript.js", allFrames: true}, function(result) {
        });
    });
}
myscript.js
将使用注入的
timeout
变量:

.............
setInterval(foo, timeout);
.............

1.问题是什么还不清楚。2.我不认为在这种情况下需要背景脚本(实际上我怀疑你的方案是否有效)@wOxxOm 1)我想得到用户在popup.html表单中输入的变量,并用该数字替换数字20000,在myscript.js 2)代码工作得很好,我试了很多次。当你点击popup.html上的submit按钮时,#submit按钮每20秒就会被点击一次。好吧,到目前为止,我已经尝试了很多次,当我使用以下代码时,你的代码工作得很好:代码:“var timeout=1”;然后我转到myscript.js,使用一个警报(超时)并获得值[1]。问题是,当我使用document.getElementById时,它不起作用。。。。有什么想法吗?注:我仍然在搜索和测试,如果我有更多的信息,我会更新编辑:你是最好的。你的代码工作100%。事实证明,输入(在popup.html)没有id。用于检查变量和表达式的实际值