[英]How to pass variables to the popup.html which has loaded already?
I want to create an extension that 我想创建一个扩展
I managed to get step 1 done by the following scripts. 我设法通过以下脚本完成了步骤1。
grabcomanddefinecompet.js grabcomanddefinecompet.js
document.addEventListener('DOMContentLoaded', function (){
document.getElementById('btn4').addEventListener('click', injection);
});
injection = function (){
chrome.tabs.query
(
{
lastFocusedWindow: true,
url: "https://www.linkedin.com/sales/profile/*"
},
function(tabs){
for(i=0; i<tabs.length; i++)
{
chrome.tabs.executeScript(tabs[i].id, {file: "myScript2.js"})
}
}
)
}
myScript2.js myScript2.js
getcompanynames = function(){
chrome.runtime.sendMessage(document.getElementsByClassName('link')[0].innerHTML);
}
getcompanynames();
bg.js (This is Background Script) bg.js(这是后台脚本)
var companynames = [];
chrome.runtime.onMessage.addListener(function(response, sender, sendResponse){
companynames.push(response);
console.log(companynames)
})
As you can see, right now when I click the button, 如您所见,当我单击按钮时,
Everything is good now. 现在一切都很好。 But I just don't know how to put these company names back to popup.html.
但是我只是不知道如何将这些公司名称放回popup.html。 The problem is popup.html has already loaded.
问题是popup.html已经加载。 Do I need Angular JS to solve this problem?
我是否需要Angular JS才能解决此问题? Also, is my process too complicated?
另外,我的过程太复杂了吗?
Thanks! 谢谢!
Assuming btn4
is in the popup page (since chrome.tabs
is not allowed in content scripts), you could move all your logic in bg.js
to popup page, because popup page also lives in the extension context and it has access to all extension apis. 假设
btn4
在弹出页面中(因为内容脚本中不允许使用chrome.tabs
),您可以将bg.js
所有逻辑bg.js
弹出页面,因为弹出页面也位于扩展上下文中,并且可以访问所有扩展蜜蜂。
In general, for message passing between popup page and background page, you could use the following ways. 通常,对于在弹出页面和背景页面之间传递消息,可以使用以下方式。
chrome.storage
, you could save value to storage in background/popup page and retrieve it in popup/background page. chrome.storage
,您可以将值保存到后台/弹出页面中的存储中,然后在弹出/后台页面中检索它。 chrome.runtime.getBackgroundPage
, you could directly call global methods/variables declared in background page from popup page via this method. chrome.runtime.getBackgroundPage
,您可以通过此方法直接从弹出页面调用在后台页面中声明的全局方法/变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.