Javascript 只运行一次的Chrome扩展

Javascript 只运行一次的Chrome扩展,javascript,json,google-chrome,google-chrome-extension,Javascript,Json,Google Chrome,Google Chrome Extension,我正在尝试做一个扩展,当我访问facebook时激活它。直到这一部分,一切都很好,但是它开始无限期地运行我的代码 我想要的是只在我第一次访问Facebook时运行content.js脚本,然后再也不运行它 我目前的方法是这样的,但不起作用: background.js chrome.extension.onRequest.addListener(function(request, sender, sendResponse) { if (request.storage) { if (t

我正在尝试做一个扩展,当我访问facebook时激活它。直到这一部分,一切都很好,但是它开始无限期地运行我的代码

我想要的是只在我第一次访问Facebook时运行content.js脚本,然后再也不运行它

我目前的方法是这样的,但不起作用:

background.js

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
  if (request.storage) {
    if (typeof request.value != 'undefined') {
      localStorage[request.storage] = request.value;
    }
    sendResponse({storage: localStorage[request.storage]});
  } else {
    sendResponse({});
  }
});
content.js

var status;
chrome.extension.sendRequest({storage: 'status'}, function(response) {
  status = response.storage;
});

if(status != '1')
{
   chrome.extension.sendRequest({storage: 'status', value: '1'});
   //  do my stuff here but only 1 time
}
manifest.json

   "content_scripts": [ {
      "js": [ "js/jquery.js", "content.js" ],
      "matches": [ "https://facebook.com/" ]
   } ],
基本上,我的想法是在访问Facebook时运行content.js,然后在脚本完成后,添加一个状态变量,然后下次检查状态是否为1,这意味着代码以前已经运行过

我怎样才能做到这一点

谢谢你。

1)你做得很好

var状态;
sendRequest({storage:'status'},函数(响应){

status=response.storage;//P.S.我的回答和关闭是重复的,因为我的其他评论对于评论部分来说有点太大了。我使用了这个,我将尝试用您的建议进行修改,thanks@lincher我在那里加了一条评论。你还记得你是怎么找到那个链接的吗?我在谷歌上搜索了“chrome extension set storage from content”嘿@xan如果这是一个经典的js dup为什么要回答呢?@ZigMandel我在对这个问题的评论中提到了这一点。这里有太多的错误。如果你认为我是一个农业代表,我可以将答案转换为CW。
var status;
chrome.extension.sendRequest({storage: 'status'}, function(response) {
  status = response.storage; // <--+
});                          //    | This code didn't execute yet
                             //    |
if(status != '1') // --------------+
{
   chrome.extension.sendRequest({storage: 'status', value: '1'});
   //  do my stuff here but only 1 time
}