Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.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
Google chrome extension 如何启动contentscript_Google Chrome Extension - Fatal编程技术网

Google chrome extension 如何启动contentscript

Google chrome extension 如何启动contentscript,google-chrome-extension,Google Chrome Extension,我有一个简单的问题 我有这三个文件 popup.html: <script> function buttonClicked(button) { chrome.extension.sendRequest({command:button.id}); } </script> <input style="width:100%" type="button" value="Click me" id="click" onclick

我有一个简单的问题 我有这三个文件 popup.html:

<script>
    function buttonClicked(button) 
    {
        chrome.extension.sendRequest({command:button.id});
    }
</script>
    <input style="width:100%" type="button" value="Click me" id="click" onclick="buttonClicked(this)"/><br/>

功能按钮点击(按钮)
{
sendRequest({command:button.id});
}

background.html:

<script>
    function processRequest(request, sender, sendResponse) 
    {
        alert('hi');
        sendResponse({});
    }
    chrome.extension.onRequest.addListener(processRequest);
</script>

函数processRequest(请求、发送方、sendResponse)
{
警报(“hi”);
sendResponse({});
}
chrome.extension.onRequest.addListener(processRequest);
和contentscript.js

s = document.getElementsByClassName('st');
if (s[0].innerText != '') { 
st = new Array(); 
for (i = 0;i<s.length;i++) {
    st[i] = s[i].innerText;
}
chrome.extension.sendMessage({"message" : st}, function(response) {});
}
s=document.getElementsByClassName('st');
如果(s[0]。innerText!=''{
st=新数组();

对于(i=0;i我已经三次试图解释解决方案以及为什么在注释中使用它。由于您不理解它,我将参考您的代码再次解释:

弹出窗口
  • 将内联脚本移动到外部文件,例如
    popup.js
  • 删除内联事件侦听器,并将其移动到
    popup.js
popup.js
popup.html
背景页。 有关可用选项的更详细说明,请参阅本部分的中间部分

  • 修改清单文件,使用
    “后台”:{“脚本”:[“background.js”]}
  • background.html
    重命名为
    background.js
    ,并从文件中删除

多亏了manifest版本2和CSP。解决方案:将JavaScript移动到外部文件。如果manifest 2导致此错误,则可能重复。我可以只使用版本1吗?!manifest v1支持正在逐步取消。为什么不使用我提供的解决方案?这是让扩展正常工作的推荐方法。这并不能真正解决问题e我的问题是,当我点击弹出页面中的按钮时,我想要执行contentscript,因为当我从背景页面或弹出页面执行它时,我无法真正进入当前选项卡元素。*我猜你不理解我的其他注释和答案。解决方法是将内联JavaScript移动到e外部文件,并使用
引用该外部文件。希望您现在理解它。如果不是,这里有一个类似的答案,但针对弹出窗口:所以我已经按照您所说的做了,它有点工作,但实际上没有工作,我的意思是脚本工作…但不是我想要的方式,我想要popup.js访问页面元素!不是popup、 html元素…就像内容脚本一样,当页面完全加载时,我可以访问当前页面元素Sok,我想我找到了一个解决方案,就是使用executeScript并在executeScript.function hello()中执行contentScript.js{chrome.tabs.executeScript(null,{code:“s=document.getElementsByClassName('st');“+”st=s[0].innerText;“+”chrome.extension.sendMessage({\'message\':st});“}”);但是除了executeScript之外还有其他方法吗?@yassine_hell是的,您可以通过清单文件(定义消息侦听器)注入内容脚本。然后,使用发送消息,并使用
sendResponse
回调返回JSON可序列化数据(没有节点,但有字符串!)。但您当前的解决方案看起来还可以,因为它只在必要时使用内容脚本。好的,谢谢您的帮助,很抱歉占用您的时间…刚刚开始使用google extensions:s。。。
function buttonClicked(button) {
    chrome.extension.sendRequest({command: button.id});
}
document.getElementById('click').addEventListener('click', function() {
    buttonClicked(this);
});
<script src="popup.js"></script>
<input style="width:100%" type="button" value="Click me" id="click"><br>
document.addEventListener('DOMContentLoaded', function() {
    document.getElementByUd('click').addEventListener( ... );
});