Javascript Chrome扩展:Can';t从chrome.local变量中检索值

Javascript Chrome扩展:Can';t从chrome.local变量中检索值,javascript,jquery,google-chrome,google-chrome-extension,Javascript,Jquery,Google Chrome,Google Chrome Extension,我正在pop.js中设置chrome.local变量,但无法在content.js popup.js chrome.storage.local.set('TITLE',title); var title = null; $(".class").each(function () { chrome.storage.local.get('TITLE', function (result) { titl

我正在
pop.js
中设置
chrome.local
变量,但无法在
content.js

popup.js

chrome.storage.local.set('TITLE',title);
var title = null;
    $(".class").each(function () {
          chrome.storage.local.get('TITLE', function (result) {
                                    title = result.TITLE;
                    console.log('inside'+title);//prints value
                                });


    }

  console.log(title);//returns null;
content.js

chrome.storage.local.set('TITLE',title);
var title = null;
    $(".class").each(function () {
          chrome.storage.local.get('TITLE', function (result) {
                                    title = result.TITLE;
                    console.log('inside'+title);//prints value
                                });


    }

  console.log(title);//returns null;
它将
title
返回为
null

Chrome控制台输出为:

outside: null
content.js:42 insideJava: A Beginner's Guide, Sixth Edition

chrome.storage.local.get
是一个异步调用,这意味着当您调用
console.log(title)时
,尚未执行chrome.storage.local.get的回调,则
title
保持
空值。您应该将您的逻辑放入回调中。

您确定
title
变量在popup.js中有值吗?也许你也可以试试
console.log
ing
result
,在content.js中?@BryndenBielefeld我检查过它甚至没有进入
get
代码块。进一步解释addedIt听起来问题来自代码中的其他地方。我看你已经更新了你的问题。如果content.js中声明了
title
,我认为它超出了范围。你在哪里定义了它?我刚刚重新阅读了你的评论,你说“它甚至没有进入
get
块”。也许页面上没有任何元素可以使用您提供给jQuery的CSS选择器进行选择。@BryndenBielefeld我正在进一步检查,代码在此之前已经被破坏了,我猜请,Haibara。使用规范副本。@Xan,对不起,我确实想标记它,但由于网络问题,我无法找到id。我明天会这样做,或者周一我已经这样做了-你应该有一个最常用的副本列表。@Xan,明白了,我过去常在频繁类别中找到该副本。。