本文介绍了尽管从chrome本地存储中获取一个未设置的变量,但chrome.runtime.lastError即将以'UNDEFINED'出现,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图检查key,value对是否已经在chrome.storage.local中设置。为此,我借助了类似的问题。在这里,我试图从存储中获得的变量并没有被我设置。当我尝试使用 lastError 捕获错误时。它是未定义的,而它是由于在读取以前没有设置的变量时发生错误而定义和设置的(按照我的搜索结果)。请帮助我在读取尚未设置的变量时检测错误。

I am trying to check if the key, value pair has been set in the chrome.storage.local. for that i took the help of a similar kind of problem here. Here the variable i'm trying to get from storage has not been set by me. When i try to catch the error using lastError. It is coming as undefined, whereas it was suppose to defined and set due to error in reading a variable that has not been set previously ( as per what i googled). Please help me to detect the error while reading a variable that has not been set.

manifest.json

manifest.json

{
    "name": "TestExtension",
    "version": "1.0",
    "manifest_version": 2,
    "background": {
        "scripts": ["jquery.js", "background.js"]
    },

    "description": "Test the extension features here",
    "icons": {
        "16": "images/16x16.png",
        "48": "images/48x48.png",
        "128": "images/128x128.png"
    },

    "page_action": {
        "default_icon": {
            "19": "images/19x19.png",
            "38": "images/38x38.png"
        },
        "default_title": "Test Plugin default title",
        "default_popup": "popup.html"
    },

    "permissions": ["tabs", "storage", "http://*/*", "https://*/*"]
}

background.js

function showTheExtension(tabId, changeInfo, tab) {
    console.log("Extension loaded");
    chrome.pageAction.show(tabId);
} // ---EOF checkForValidUrl---
chrome.tabs.onUpdated.addListener(showTheExtension);

popup.js

$('document').ready(function() {
    chrome.storage.local.get('testVar1', function(result) {
        if (chrome.runtime.lastError) {
            alert('Var not set')
        }
        console.log(result);
        console.log(chrome.runtime.lastError.message);
    });
});

popup.html

<!DOCTYPE html>
<html>
<head>
    <title>Page Title</title>
</head>
<body>
   <h3>Welcome to the test project for designing chrome plugins</h3>

<script type="application/x-javascript" src="./jquery.js"></script>
<script type="application/x-javascript" src="./popup.js"></script>
</script>
</body>
</html>


推荐答案

不存在该值不是API错误,因此没有设置。

Non-existence of the value is not an API error, hence chrome.runtime.lastError is not set.

如果您想知道变量是否设置,请检查结果:

If you want to know whether the variable is set or not, check the results:

chrome.storage.local.get('testVar1', function(result) {
    if (result.testVar1 === undefined) {
        alert('Var not set');
        return;
    }
    // Rest of code...
});

这篇关于尽管从chrome本地存储中获取一个未设置的变量,但chrome.runtime.lastError即将以'UNDEFINED'出现,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 02:22