相关疑难解决方法(0)

1126
推荐指数
17
解决办法
84万
查看次数

为什么我的变量在函数内部修改后没有改变? - 异步代码引用

鉴于以下示例,为什么outerScopeVar在所有情况下都未定义?

var outerScopeVar;

var img = document.createElement('img');
img.onload = function() {
    outerScopeVar = this.width;
};
img.src = 'lolcat.png';
alert(outerScopeVar);
Run Code Online (Sandbox Code Playgroud)

var outerScopeVar;
setTimeout(function() {
    outerScopeVar = 'Hello Asynchronous World!';
}, 0);
alert(outerScopeVar);
Run Code Online (Sandbox Code Playgroud)

// Example using some jQuery
var outerScopeVar;
$.post('loldog', function(response) {
    outerScopeVar = response;
});
alert(outerScopeVar);
Run Code Online (Sandbox Code Playgroud)

// Node.js example
var outerScopeVar;
fs.readFile('./catdog.html', function(err, data) {
    outerScopeVar = data;
});
console.log(outerScopeVar);
Run Code Online (Sandbox Code Playgroud)

// with promises
var outerScopeVar;
myPromise.then(function (response) {
    outerScopeVar = response;
});
console.log(outerScopeVar);
Run Code Online (Sandbox Code Playgroud)

// geolocation API
var outerScopeVar; …
Run Code Online (Sandbox Code Playgroud)

javascript asynchronous

675
推荐指数
6
解决办法
18万
查看次数

无功能地返回Chrome存储API值

在过去的两天里,我一直在使用chrome异步存储.如果你有一个功能,它的工作"很好".(如下):

chrome.storage.sync.get({"disableautoplay": true}, function(e){
console.log(e.disableautoplay);
});
Run Code Online (Sandbox Code Playgroud)

我的问题是我无法使用我正在做的功能.我想把它归还,就像LocalStorage一样.就像是:

var a = chrome.storage.sync.get({"disableautoplay": true});
Run Code Online (Sandbox Code Playgroud)

要么

var a = chrome.storage.sync.get({"disableautoplay": true}, function(e){
    return e.disableautoplay;
});
Run Code Online (Sandbox Code Playgroud)

我已经尝试了一百万种组合,甚至设置一个公共变量并设置:

var a;
window.onload = function(){
    chrome.storage.sync.get({"disableautoplay": true}, function(e){
    a = e.disableautoplay;
});
}
Run Code Online (Sandbox Code Playgroud)

什么都行不通.它都返回undefined,除非引用它的代码在get函数内部,这对我来说没用.我只是希望能够将值作为变量返回.

这甚至可能吗?

编辑:这个问题不重复,请允许我解释原因:

1:没有其他帖子专门询问这个问题(我花了两天时间先查看,以防万一).

2:我的问题仍未得到解答.是的,Chrome Storage是异步的,是的,它不会返回值.那就是问题所在.我将在下面详细说明......

我需要能够在chrome.storage.sync.get函数之外获取存储值.我无法使用localStorage,因为它是特定于URL的,并且无法从chrome扩展的browser_action页面和background.js访问相同的值.我无法使用一个脚本存储值并使用另一个脚本访问它.他们分开对待.

所以我唯一的解决方案是使用Chrome存储.必须有某种方法来获取存储项的值并在get函数之外引用它.我需要在if语句中检查它.

就像localStorage可以做的那样

if(localStorage.getItem("disableautoplay") == true);
Run Code Online (Sandbox Code Playgroud)

必须有一些方法可以做一些事情

if(chrome.storage.sync.get("disableautoplay") == true);
Run Code Online (Sandbox Code Playgroud)

我意识到这不会那么简单,但这是我解释它的最佳方式.

我看到的每个帖子都说这样做:

chrome.storage.sync.get({"disableautoplay": true, function(i){
    console.log(i.disableautoplay);
    //But the info is worthless to me inside this function.
});
//I need it outside this function.
Run Code Online (Sandbox Code Playgroud)

javascript function return-value google-chrome-extension google-chrome-storage

14
推荐指数
4
解决办法
1万
查看次数

chrome.storage.local.get如何设置值?

var storage = chrome.storage.local;
var css = "old";
storage.set({'css': 'new'});
storage.get('css', function(items) {
    if (items.css) {
        css = items.css;
        console.log(css);
    }
});
console.log(css);
Run Code Online (Sandbox Code Playgroud)

我正在尝试升级我的chrome扩展以适应清单版本2.
当我运行上面的代码时,我得到第一个console.log返回"new",第二个返回"old".如果我想将css设置为新值,我怎样才能得到两个"new"?

javascript local-storage google-chrome-extension

6
推荐指数
1
解决办法
5544
查看次数

关于注入脚本 + 本地存储的 Chrome 扩展

我对我的第一个“将所有内容放在一起”的 Chrome 扩展程序感到困惑,我将描述我正在尝试做的事情,然后我是如何通过一些脚本摘录来解决这个问题的:

  1. 我有一个 options.html 页面和一个 options.js 脚本,它允许用户在文本字段中设置一个 url——这使用 localStorage 存储。
function load_options() {
   var repl_adurl = localStorage["repl_adurl"];
   default_img.src = repl_adurl;
   tf_default_ad.value = repl_adurl;
}

function save_options() {
   var tf_ad = document.getElementById("tf_default_ad");
   localStorage["repl_adurl"] = tf_ad.value;
}

document.addEventListener('DOMContentLoaded', function () {
   document.querySelector('button').addEventListener('click', save_options);
});

document.addEventListener('DOMContentLoaded', load_options );
Run Code Online (Sandbox Code Playgroud)
  1. 我的 contentscript 将脚本“myscript”注入页面(因此它可以访问页面 html 中的 img 元素)
var s = document.createElement('script');
s.src = chrome.extension.getURL("myscript.js");
console.log( s.src );
(document.head||document.documentElement).appendChild(s);
s.parentNode.removeChild(s);
Run Code Online (Sandbox Code Playgroud)
  1. myscript.js应该以某种方式获取本地存储数据并决定如何操作图像元素。

我从 html 源中抓取图像没有任何问题,但我似乎无法访问 localStorage 数据。我意识到它必须与具有不同环境的两个脚本有关,但我不确定如何克服这个问题——据我所知,我需要从 contentscript.js 注入 myscript.js,因为 contentscript.js 没有可以访问 html 源代码。 …

javascript google-chrome-extension content-script

1
推荐指数
1
解决办法
3411
查看次数

获取当前标签并将其传递到Chrome扩展程序中的变量

我正在尝试创建一个返回当前标签页面的函数:

function tabURL() {
var url="";
chrome.tabs.getSelected(null, function(tab) {url = tab.url;});
return url;
}
Run Code Online (Sandbox Code Playgroud)

我用的时候:

chrome.tabs.getSelected(null, function(tab) {alert(tab.url);});
Run Code Online (Sandbox Code Playgroud)

Chrome会显示网址,但如果我在Chrome控制台中使用我的功能,则该功能会返回"".

有没有办法将tab.url传递给变量然后返回此变量?

google-chrome google-chrome-extension

0
推荐指数
1
解决办法
4006
查看次数