【问题标题】:Function not accessing Chrome storage values in extension函数未访问扩展中的 Chrome 存储值
【发布时间】:2017-05-01 05:05:46
【问题描述】:

我正在开发一个 chrome 扩展程序,它将给定页面上的 pries 转换为用户赚取该金额所需的工作时间。该扩展通过弹出窗口接收用户工资信息,然后将其设置在 chrome 存储中。这部分工作正常。

我遇到的问题是访问该值并使用它来运行计算。 JS 很好地解析了这些值并调用了转换函数,但在计算中只使用占位符每小时值 1,而不是从 chrome 存储中检索它。相反,它似乎在所有解析之后访问了 chrome 存储(我根据控制台日志的运行时间来计算)。

您知道是什么原因造成的吗?我已经尝试将整个解析移动到它自己的函数中,以便在 $(document).ready(function) 的末尾调用,但它仍然不会从 chrome 存储中获取值。

在 content.js 中:

function convert(price){
    var hourly = 1;
    chrome.storage.sync.get('hourly', function(result){
        hourly = result.hourly;
        console.log(hourly);
    });
    return price / hourly;
}

var elements = document.getElementsByTagName('*');

for (var i = 0; i < elements.length; i++) {
var element = elements[i];

    for (var j = 0; j < element.childNodes.length; j++) {
        var node = element.childNodes[j];

        if (node.nodeType === 3) {
            var text = node.nodeValue;
            if (text[0] == '$'){
                if (/\s/.test(text)){
                    var price = text.substring(0, text.indexOf(' '));
                    var priceNum = text.substring(1, text.indexOf(' '));
                    var time = convert(parseFloat(priceNum));
                    var timeStr = time.toFixed(2);
                    if (text.length > 5){
                        var remainder = text.substring(4);
                        var nextPrice = remainder.indexOf('$');
                        if (nextPrice != -1){
                            var priceTwo = remainder.substring(nextPrice);
                        }
                        var priceTwo = null;
                    }
                }
                else {
                    var price = text.substring(0);
                    var priceNum = text.substring(1);
                    var time = convert(parseFloat(priceNum));
                    var timeStr = time.toFixed(2);
                }
            }
            var replacedText1 = text.replace(price, timeStr);
            if (replacedText1 !== text) {
                element.replaceChild(document.createTextNode(replacedText1 + ' hours'), node);
            }
        }
    }
}

【问题讨论】:

标签: javascript google-chrome google-chrome-extension google-chrome-storage


【解决方案1】:

chrome.storage.sync.get 通过回调异步返回结果。你可以通过将chrome.storage.sync.get包裹在Promise中得到结果,然后在then函数中接收解析出来的数据:

function convert(price){
    var hourly = 1;
    return new Promise(function(resolve, reject) {
        chrome.storage.sync.get('hourly', function(result){
            hourly = result.hourly;
            resolve(price / hourly);
        });
    });
}

var elements = document.getElementsByTagName('*');

for (var i = 0; i < elements.length; i++) {
var element = elements[i];

    for (var j = 0; j < element.childNodes.length; j++) {
        var node = element.childNodes[j];

        if (node.nodeType === 3) {
            var text = node.nodeValue;
            if (text[0] == '$'){
                if (/\s/.test(text)){
                    var price = text.substring(0, text.indexOf(' '));
                    var priceNum = text.substring(1, text.indexOf(' '));
                    if (text.length > 5){
                        var remainder = text.substring(4);
                        var nextPrice = remainder.indexOf('$');
                        if (nextPrice != -1){
                            var priceTwo = remainder.substring(nextPrice);
                        }
                        var priceTwo = null;
                    }
                }
                else {
                    var price = text.substring(0);
                    var priceNum = text.substring(1);
                }
            }
            convert(parseFloat(priceNum)).then(function(time) {
                var timeStr = time.toFixed(2);
                var replacedText1 = text.replace(price, timeStr);
                if (replacedText1 !== text) {
                    element.replaceChild(document.createTextNode(replacedText1 + ' hours'), node);
                }
            });
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-05
    • 1970-01-01
    • 2021-09-15
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    相关资源
    最近更新 更多