0

私は、json 形式のリストで URL をチェックし、新しいタブまたはアドレスにアクセスするたびに比較する必要がある chrome 拡張機能の作成に忙しくしています。一致する場合は、何かを行う必要があります...しかし、現在、関数内から変数にアクセスできません。グローバル変数とローカル変数を使用したものだと思っていましたが、理解できません。私が到達できない変数 は、私が未定義u = request.dom; になるaddListenerの外でそれをチェックするとです。console.log

私のコード:

Background.js (jquery-1.11.3.js、amplify.js をロード):

loadedwebshops = amplify.store('webshops');
(function loadShops() {
    if (typeof loadedwebshops != 'undefined') {
    } else {
        // load from server
        $.ajax({
            url: '../shoplist.json',
            dataType: 'json',
            data: {}
        }).done(function(data, textStatus, jqXHR) {
            // update the cache
            amplify.store('webshops', data, {
                expires: 60000
            });
            loadedwebshops = amplify.store('webshops');
            //populate.Shops(data);
        }).fail(function(jqXHR, exception) {
        }).always(function() {
        });
        // alert("server");
        if (typeof loadedwebshops === 'undefined') {
            location.reload();
        }
    }
    setInterval(loadShops, 60000);
}
)();
chrome.extension.onMessage.addListener(function(request) {
    u = request.dom;
    requestedUrl(u);
});
$(document).ready(function() {
    var webshops = loadedwebshops["webshops"];
    console.log(webshops);
    function requestedUrl(u){
        console.log(u);
    }
});

Main.js:

var d = document.domain;
chrome.extension.sendMessage({
    dom: d
});

マニフェスト:

{
    "background": {
        "page": "background.html"
    },
    "browser_action": {
        "default_icon": "icons/actions/1.png",
        "default_title": "toolbar"
    },
    "content_scripts": [{
        "all_frames": true,
        "js": ["lib/jquery-1.11.3.js", "lib/amplify.js", "lib/main.js"],
        "matches": ["http://*/*", "https://*/*"]
    }],
    "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
    "description": "extension",
    "icons": {
        "128": "icons/icon128.png",
        "16": "icons/icon16.png",
        "48": "icons/icon48.png"
    },
    "manifest_version": 2,
    "name": "toolbar",
    "version": "1.1",
    "permissions": ["http://*/*", "https://*/*", "tabs", "cookies",
        "notifications", "contextMenus", "webNavigation", "webRequest",
        "webRequestBlocking", "unlimitedStorage", "storage"
    ]
}
4

1 に答える 1

0

解決しました...問題は、それが異なる範囲内にあったことでした $(document).ready(function() {});

新しいコードは次のとおりです。

loadedwebshops = amplify.store('webshops');
(function loadShops() {
    if (typeof loadedwebshops != 'undefined') {
    } else {
        // load from server
        $.ajax({
            url: '../shoplist.json',
            dataType: 'json',
            data: {}
        }).done(function(data, textStatus, jqXHR) {
            // update the cache
            amplify.store('webshops', data, {
                expires: 60000
            });
            loadedwebshops = amplify.store('webshops');
            //populate.Shops(data);
        }).fail(function(jqXHR, exception) {
        }).always(function() {
        });
        // alert("server");
        if (typeof loadedwebshops === 'undefined') {
            location.reload();
        }
    }
    setInterval(loadShops, 60000);
}
)();
chrome.extension.onMessage.addListener(function(request) {
    u = request.dom;
    requestedUrl(u);
});

var webshops = loadedwebshops["webshops"];
console.log(webshops);
function requestedUrl(u){
    console.log(u);
}
于 2015-09-18T14:42:00.173 に答える