2

让我解释一下这个问题。我正在开发一个 chrome 扩展程序,但我遇到了一些功能。我需要在浏览器中获取 url 并从我的服务器中检索一些内容来更新 badgeText。

contentscript1.js

chrome.extension.sendMessage({method:"sendUrlInBrowser",urlinbrowser: location.href}, function(response) { });

contentscript2.js

chrome.extension.sendRequest({method: "getInfosUser"}, function(response) {
var url = location.href;
var id_user = response.id_user;
var default_friend_view_user = response.default_friend_view_user;

$.post("http://host/getNoteCount",
    {
        "url_note" : url

    },
    function (data)
    {
        var obj = $.parseJSON(data);
        var note_count = obj[0].note_count;
        chrome.extension.sendMessage({method:"sendNoteCount",note_count: not_count}, function(response) {

         });
    }
); });

背景.js

var urlinbrowser = "";var note_count = "";chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {if(request.method== 'sendUrlInBrowser') urlinbrowser = request.urlinbrowser;else if(request.method== 'sendNoteCount') 
{
    note_count = request.note_count;
    console.log(note_count);
    chrome.browserAction.setBadgeText({text:""+note_count});
} });chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request.method == "getInfosUser")
{
    var storage = chrome.storage.local;

    var items_to_retrieve = ['id_user','default_friend_view_user'];

    storage.get(items_to_retrieve, function(items) {
      var id_user = items.id_user;
      var default_friend_view_user = items.default_friend_view_user;

        if(items.id_user.replace(' ','')!='undefined') 
        {
            sendResponse({id_user: id_user,default_friend_view_user: default_friend_view_user});
        }
    });

 }
else
  sendResponse({}); // snub them.});

主要问题实际上是我需要将跨站点请求的结果发送回后台,所以它必须监听这两条消息(url + result)而且我绝对不能:(我很确定我不必使用 connect() 但如果它可以工作...而且我什至不确定我是否需要 2 个内容脚本。有人知道吗?谢谢...

4

0 回答 0