关闭弹出窗口后Chrome.storage.local不保存

时间:2017-02-28 17:55:07

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

我是创建Chrome扩展程序的新手。到目前为止,一切顺利,因为它与Web开发相同。但是,我似乎无法让chrome.storage.local 100%工作。截至目前,如果我设置一个密钥(带有其值)并将其保存在存储中,则扩展仅在用户保持弹出窗口打开时才保存。当弹出窗口关闭并且用户再次打开扩展名时,密钥就会消失。

当你将它与cookie和会话进行比较时,它实际上是相同的故事,我希望它作为一个cookie运行(所以即使浏览器关闭也会保存数据),但我的结果是一个会话(当弹出窗口关闭时,数据消失了。)

我的清单文件:

{
    "manifest_version": 2,

    "name": "Rooster Extension",
    "version": "0.5.2",
    "version_name": "Open Beta",
    "description": "description placeholder",

    "browser_action": {
        "default_popup": "popup.html",
        "default_title": "Title placeholder"
    },

    "icons": {
        "16": "icon16.png",
        "48": "icon48.png",
        "128": "icon128.png"
    },

    "web_accessible_resources": [
        "fonts/*.*"
    ],

    "content_scripts": [
        {
            "matches": ["http://nowhere/*"],
            "css": ["css/font-awesome.min.css","css/style.css"],
            "js": ["js/jquery.min.js", "js/custom.js"]
        }
    ],

    "permissions": [
        "storage"
    ]
}

Custom.js文件(我使用chrome API的部分):

function getClass(){
    chrome.storage.local.get("class", function(data){
        var class = data["class"];
        var class = class.toUpperCase();
    });
}

try{
    getClass();
    if(class== null){
        $("#main-page").hide();
        $("#hello-page").fadeIn();//Redirect user to the welcome page where he/she has to typ in his/her classname
    }
}catch(e) {
    console.log(e);
    $("[id*='-page']").hide();//Redirect user to the selected page
    $("#hello-page").fadeIn();
}

$("#searchclass").click(function(){
        var searchclass= $("#search_input_class").val();
        if(!searchclass|| searchclass=== " " || searchclass.length !== 4){return;}
        searchclass= searchclass.toUpperCase();
        $(".class").text("Search: "+searchclass);

        $("#roosterdata").attr("src", "http://example/rooster/getRooster.php?class="+zoekklas);

        $("#search-page").hide();
        $("#main-page").fadeIn();
    });

我认为问题是由我的manifest.json文件引起的。我写这个文件可能做得不好,因为我甚至不得不保持"匹配"在文件中,否则Chrome在更新我的扩展程序时会出错。

附注:我还不是Chrome扩展开发者,我只需要在Google上搜索,如果我可以用预付信用卡支付25美元(Android开发者)和5美元(镀铬扩展开发者)费用,因为我没有信用卡。

0 个答案:

没有答案
相关问题