Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.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 Google Chrome Extension-使用content_script.js中Extension.js文件中的数据_Javascript_Jquery_Google Chrome_Google Chrome Extension - Fatal编程技术网

Javascript Google Chrome Extension-使用content_script.js中Extension.js文件中的数据

Javascript Google Chrome Extension-使用content_script.js中Extension.js文件中的数据,javascript,jquery,google-chrome,google-chrome-extension,Javascript,Jquery,Google Chrome,Google Chrome Extension,好的,首先我应该定义一下我所说的extension.js和content\u script.js: extension.js-在扩展弹出窗口中使用的脚本。。。e、 g:如果我要点击扩展弹出窗口中的一个按钮,我可以使用extension.js来处理它。js`不影响您正在查看的页面,它只影响弹出框 content\u script.js-此“”是影响当前网页的内容,在本例中是JavaScript。但是这个脚本不能影响弹出窗口,它只能影响你告诉它的网页 好的,问题基本上是标题。如何在conten

好的,首先我应该定义一下我所说的
extension.js
content\u script.js

  • extension.js
    -在扩展弹出窗口中使用的脚本。。。e、 g:如果我要点击扩展弹出窗口中的一个按钮,我可以使用
    extension.js
    来处理它。js`不影响您正在查看的页面,它只影响弹出框

  • content\u script.js
    -此“”是影响当前网页的内容,在本例中是JavaScript。但是这个脚本不能影响弹出窗口,它只能影响你告诉它的网页


好的,问题基本上是标题。如何在
content\u script.js
中使用
extension.js
中的数据。。。让我举一个例子:

假设我在弹出窗口中有一个表单,它有一些文本字段。。。假设有一个ID为
exmp
,现在,我们可以使用
扩展名.js
获取它,但是我们如何将它馈送到
content\u script.js
,以便它可以修改网页

extension.js

var exmp = document.getElementById("exmp"); // from popup.html
// How would I fetch the "exmp" variable from extension.js?
if (exmp.value == "test text") {
   // Do stuff that modifies the web page.
}


content\u script.js

var exmp = document.getElementById("exmp"); // from popup.html
// How would I fetch the "exmp" variable from extension.js?
if (exmp.value == "test text") {
   // Do stuff that modifies the web page.
}

关于答复:

  • jQuery是可以接受的。但最好使用纯JavaScript的答案。使用两者的答案更好

  • 问题末尾将列出Chrome扩展的来源(如下)


资料来源:


您可以使用以下功能在内容和后台脚本之间进行通信(双向)

window.chrome.runtime.sendMessage() 
chrome.extension.onMessage.addListener(function (msg, sender, sendResponse) {});
关于你的问题,从记忆中你可以把以下内容放在你的背景脚本中

chrome.extension.onMessage.addListener(function (msg, sender, sendResponse) {

  if(msg == 'GetData'){
    sendResponse(SomeData);
  }

});
并在内容脚本中使用以下内容

window.chrome.runtime.sendMessage({'msg':'GetData'},function(response){    
  // How would I fetch the "exmp" variable from extension.js?
  if (response.value == "test text") {
     // Do stuff that modifies the web page.
  }
});