简体   繁体   English

如何将变量传递到已经加载的popup.html?

[英]How to pass variables to the popup.html which has loaded already?

I want to create an extension that 我想创建一个扩展

  1. When I click a button (Grab Companies and Define Competitors) on the html, it goes to the tabs that match my criteria and grabs some variables in each tab 当我单击html上的按钮(抢先公司和定义竞争对手)时,它会转到符合我的条件的选项卡,并在每个选项卡中获取一些变量
  2. Then it should paste the variables back on the popup.html 然后,应将变量粘贴回popup.html上

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, 如您所见,当我单击按钮时,

  1. it goes to pages that match " https://www.linkedin.com/sales/profile/ *" and injects "myScript2.js" 它会转到与“ https://www.linkedin.com/sales/profile/ *”匹配的页面,并注入“ myScript2.js”
  2. myScript2.js grabs the 1st variable of "link" element and sends back to bg.js myScript2.js捕获“链接”元素的第一个变量,然后发送回bg.js
  3. bg.js logs all company names in the console bg.js在控制台中记录所有公司名称

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. 通常,对于在弹出页面和背景页面之间传递消息,可以使用以下方式。

  1. Message Passing , just use same ways as you used for communicating between content scripts and background page. 消息传递 ,只需使用与内容脚本和后台页面之间进行通信所使用的相同方法即可。
  2. chrome.storage , you could save value to storage in background/popup page and retrieve it in popup/background page. chrome.storage ,您可以将值保存到后台/弹出页面中的存储中,然后在弹出/后台页面中检索它。
  3. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM