如何同步后台和内容脚本之间的消息传递?

发布于 10-19 16:49 字数 1120 浏览 4 评论 0原文

目前,我的背景和内容脚本之间的沟通存在问题。问题是它们可以通信,但似乎是异步的。

让我向你展示我的代码。

yourToolbar.js

chrome.extension.sendRequest({getStatut : "none"}, function(response) 
{
    console.log('yourToolbar : ' + response.statut);
    localStorage['activated'] = response.statut;
});

if (localStorage['activated'] == "show") 
{
        // injection
}

background.js

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) 
{   
    if(request.getStatut)
    {
        console.log('Background : ' + localStorage['activated']);
        sendResponse({statut : localStorage['activated']});
    }
    else if(request.modifyToolbar)
{
    if (request.modifyToolbar == "hideToolbar") 
    {
        localStorage['activated'] = "hide";
    }       
}
});

真正的问题是,当我隐藏工具栏时,工具栏只会在再刷新 1 次后才会消失。所以看来通信不是最新的...当我在控制台上打印“已激活”的值时,它向我显示了良好的值,但显然 yourtoolbar.js 上的条件测试了“已激活”的先前

值有主意吗?

编辑:一个奇怪的想法是,如果我在条件之前添加一个警报,它就会完美地工作......

alert(ocalStorage['activated']);
        if (ocalStorage['activated']== "show") 
        {

I currently have an issue with the communication bewteen my background and my contentscript. The problem is that they can communicate but it seems to be asynchronous.

Let me show you my code.

yourToolbar.js

chrome.extension.sendRequest({getStatut : "none"}, function(response) 
{
    console.log('yourToolbar : ' + response.statut);
    localStorage['activated'] = response.statut;
});

if (localStorage['activated'] == "show") 
{
        // injection
}

background.js

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) 
{   
    if(request.getStatut)
    {
        console.log('Background : ' + localStorage['activated']);
        sendResponse({statut : localStorage['activated']});
    }
    else if(request.modifyToolbar)
{
    if (request.modifyToolbar == "hideToolbar") 
    {
        localStorage['activated'] = "hide";
    }       
}
});

The real problem is that when i hide the toolbar, the toolbar only disapear after 1 more refresh. So it's seems that the communication is not up to date... When i print on the console the value of "activated" it show me the good value, but apparently the condition on yourtoolbar.js test the previous value of "activated"

Someone have an idea ?

EDIT : One strange thinks, is that if i add an alert just before the condition it work perfectly...

alert(ocalStorage['activated']);
        if (ocalStorage['activated']== "show") 
        {

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热鲨2024-10-26 16:49:22

Chrome 中的所有消息传递和所有(?)API 方法都是异步的。

所以你的toolbar.js应该是这样的:

chrome.extension.sendRequest({getStatut : "none"}, function(response) 
{
    console.log('yourToolbar : ' + response.statut);
    localStorage['activated'] = response.statut;

    //inside a callback
    if (localStorage['activated'] == "show") 
    {
            // injection
    } 
});

All messaging in Chrome and all(?) API methods are asynchronous.

So your toolbar.js should look like:

chrome.extension.sendRequest({getStatut : "none"}, function(response) 
{
    console.log('yourToolbar : ' + response.statut);
    localStorage['activated'] = response.statut;

    //inside a callback
    if (localStorage['activated'] == "show") 
    {
            // injection
    } 
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文