简体   繁体   English

chrome.tabs.create不起作用

[英]chrome.tabs.create not working

Because my content script cannot use all the chrome API tools, I am sending a message from my content script to my background script. 由于我的内容脚本无法使用所有chrome API工具,因此我正在从内容脚本向后台脚本发送一条消息。 When received, the background script is supposed to open a new tab containing an html file I had made. 收到背景脚本后,应该打开一个新选项卡,其中包含我创建的html文件。

This is sending the message from the content script... 这是从内容脚本发送消息...

chrome.runtime.sendMessage({permission: true}, function(response) {
        console.log(response.access);
});

This is the code to receive the message in my background script... 这是在我的后台脚本中接收消息的代码。

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if (request.permission == true) {
        chrome.tabs.create({'url': chrome.extension.getURL('./src/permission.html')}, function(tab) {
            sendResponse({access: "yes"});
        });  
    }
});

The message is received, I have already tested that. 收到消息,我已经测试过了。 But when I add the following code... 但是当我添加以下代码时...

chrome.tabs.create({'url': chrome.extension.getURL('./src/permission.html')}, function(tab) {
...etc

I get an error saying response not received. 我收到一条错误消息,说未收到回复。 Meaning something must have broke inside my chrome.tabs.create . 意思是我的chrome.tabs.create内部一定发生了chrome.tabs.create Why is it breaking? 为什么会破裂?

The permission.html path is relative to the background script. permission.html路径是相对于后台脚本的。

What I want is for a new tab to be created when the message is received. 我想要的是在收到消息时创建一个新选项卡。

I'm not sure if this has any affect, but the content scripts and the background scripts communicate asyncronously so for you to use the sendResponse callback, you'll need to return true; 我不确定这是否有影响,但是内容脚本和后台脚本是异步通信的,因此要使用sendResponse回调,您需要return true; at the end of your onMessage anonymous function. 在onMessage匿名函数的末尾。

Chrome onMessage return true Chrome onMessage返回true

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
    if (request.permission == true) {
        chrome.tabs.create({'url': chrome.extension.getURL('./src/permission.html')}, function(tab) {
            sendResponse({access: "yes"});
        });  
    }
    return true; //to tell the content script to look out for sendResponse
});

Again, I'm not sure if this will solve your problem, but regardless, your response using sendResponse will not work without returning true at the end of the listener 同样,我不确定这是否可以解决您的问题,但是无论如何,如果没有在侦听器末尾返回true,则使用sendResponse的响应将无法工作

Also, BTW, chome.extension.getURL() does not need a dot-slash so chrome.extension.getURL('src/permission.html') should be enough. 此外,顺便说一句, chome.extension.getURL()不需要点斜杠,因此chrome.extension.getURL('src/permission.html')应该足够。

Have you tried just running 你试过跑步吗

chrome.tabs.create({'url': chrome.extension.getURL('src/permission.html')});

(with or without the dot-slash) to see if the tab opens up? (带或不带点斜线)以查看标签页是否打开?

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

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