简体   繁体   English

Chrome扩展程序-如何将变量从JS传递到popup.html?

[英]Chrome Extension - How to pass variables from JS to popup.html?

I have some variables in the following JS: 我在以下JS中有一些变量:

document.addEventListener('DOMContentLoaded', function (){
    document.getElementById('btn4').addEventListener('click', getbg);
});

getbg = function()

{

    chrome.runtime.getBackgroundPage(
        function (bg) {
        var allcompanynames = bg.companynames;
        alert(allcompanynames)})

}

As you can see, the variable is "allcompanynames". 如您所见,变量是“ allcompanynames”。

However, how do I pass them and show it on the popup.html page? 但是,如何传递它们并将其显示在popup.html页面上?

I have tried 我努力了

<script type="text/javascript" src="companynames.js"></script>
<p id="allcompanynames"></p>

no luck. 没运气。 What's wrong? 怎么了?

document.addEventListener('DOMContentLoaded', function (){
document.getElementById('btn4').addEventListener('click', getbg);
});

getbg = function()

{

chrome.runtime.getBackgroundPage(
    function (bg) {
    var allcompanynames = bg.companynames;
    alert(allcompanynames)})
    document.getElementById("allcompanynames").innerHTML(allcompanynames)

}

I'm guessing you should add that last line after displaying the pop up to add the content into the page. 我猜您应该在显示弹出窗口以将内容添加到页面后添加最后一行。

Write your code in this way 用这种方式编写代码

var background = chrome.extension.getBackgroundPage(); 
     var allcompanynames = background.companynames;
alert(allcompanynames)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 chrome 扩展将来自 popup.html 的输入值传递到当前页面 - How to pass input value from popup.html to current page with a chrome extension 如何在Chrome扩展程序中通过鼠标调整POPUP.html页面的大小 - How to resizing POPUP.html page from mouse in chrome extension 如何在 chrome 扩展中从 background.js 引用 popup.html DOM? - How to reference popup.html DOM from background.js in chrome extension? Chrome扩展程序:从弹出式html传递变量 - Chrome extension: pass variables from popup html 在popup.html和content.js之间的Chrome扩展消息传递 - Chrome extension messaging between popup.html and content.js 如何使用Chrome扩展程序popup.html从新的弹出窗口中定位DOM元素 - How to target DOM elements from new popup window, using chrome extension popup.html Chrome扩展程序sendMessage从popup.html到content.js不起作用 - Chrome Extension sendMessage from popup.html to content.js not working Chrome扩展程序将Background.js中的值保存到存储中并在popup.html上显示 - Chrome Extension Saving Values from Background.js into Storage and displaying on popup.html 防止 Chrome 扩展程序的 popup.html 自行打开 - Preventing a Chrome extension's popup.html from opening itself 在content.js(Chrome扩展程序)中发生操作时,如何检查popup.html页面? - How to check popup.html page when action occurs within content.js (Chrome Extension)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM