开发者

Communicating with content scripts without requesting "tabs" permission

开发者 https://www.devze.com 2023-03-23 05:49 出处:网络
When developing a Chrome extension, my background script needs to communicate with the content scripts in the tabs loaded with a particular site.

When developing a Chrome extension, my background script needs to communicate with the content scripts in the tabs loaded with a particular site. Is there a way to communicate without using chrome.tabs.sendRequest?

This function requires the "ta开发者_C百科bs" permission which shows up as "this extension has access to your browsing history," which scares off users.


Sorry, there is no other way around.

UPDATE

Actually there is a way. Instead of pushing data from a background page to a content script you can pull data from a content script, and this doesn't require any permissions:

content script:

chrome.extension.sendRequest({cmd: "getData"}, function(response) {
    console.log("data:", response);
});

background page:

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    if(request.cmd == "getData") {
        sendResponse({param1: "value1", param2: "value2"});
    }
});


Remember even if you could communicate with background page without using chrome.tabs.sendRequest (actually it is almost impossible), you still need the tabs permission in order to inject a content script.

Read more: http://code.google.com/chrome/extensions/content_scripts.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号