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
:
-在扩展弹出窗口中使用的脚本。。。e、 g:如果我要点击扩展弹出窗口中的一个按钮,我可以使用extension.js
来处理它。js`不影响您正在查看的页面,它只影响弹出框extension.js
-此“”是影响当前网页的内容,在本例中是JavaScript。但是这个脚本不能影响弹出窗口,它只能影响你告诉它的网页content\u script.js
好的,问题基本上是标题。如何在
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.
}
});