简体   繁体   English

如何将价值从popup.js传递到background.js

[英]How to deliver value from popup.js to background.js

I hope to deliver the value from popup.js to background.js so that I can open the website as what i expect. 我希望将值从popup.js传递到background.js,以便我可以按预期打开网站。 I use the localStorage variable as my json's value. 我使用localStorage变量作为json的值。 But when found that the value I have delieverd to background.js in the argument input of the function openTab(input) is always the string "localStorage.input" itself. 但是当发现我在函数openTab(input)的参数输入依赖于background.js的值始终是字符串“ localStorage.input”本身。 How can I solve it? 我该如何解决?

popup.js popup.js

window.onload=function()
{
    localStorage.input=document.getElementById("search").value;

    document.getElementById("submit").onclick=function()
    {
        chrome.extension.sendMessage({command:"start",input:localStorage.input});
    }
}

background.js background.js

chrome.runtime.onMessage.addListener(
    function(request,sender,sendResponse)
    {
        switch(request.command)
        {
            case "start":
                openTab(request.input);
                break;
        }

        return true;
    }
);

var openTab=function(input)
{
    chrome.windows.create
    (
        {
            url:"http://www.baidu.com/s?wd="+input,
        }

    );
};

try this out 试试这个

var lStore = localStorage.input || '';
window.onload=function()
{
    var search = document.getElementById("search");
    search.value = lStore

    document.getElementById("submit").onclick=function()
    {
        // var input = search.value; //try this as well
        var input = lStore;
        chrome.extension.sendMessage({command:"start",input:input});
    }
}

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

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