2

設定の変更をcontent_script.jsに通知する適切な方法は何ですか?

設定をlocalStorageに書き込むとき、コンテンツスクリプトにsendMessage / sendRequestを実行して、それに応じて適用できるようにします。

options.js>background.js>content_script.jsを実行する必要があります

考え?

options.js

$('#add-filter-button')
    .button()
    .click(function(event) {
        event.preventDefault();
        var userFilters = new Array();
        var newFilter = $('#add-filter').val();
        if (newFilter !== undefined && newFilter.length > 0) {
            $('#filters-box').append(new Option(newFilter, newFilter));
            $('#add-filter').val('');
            $('#filters-box option').each(function() {
                userFilters.push( $(this).text() );
            });
            localStorage['userFilters'] = JSON.stringify(userFilters);
            chrome.storage.sync.set({'userFilters': userFilters});
        }
    });

content.js

chrome.storage.onChanged.addListener(function(changes, namespace) {
    for (key in changes) {
        var storageChange = changes[key];
        console.log('Storage key "%s" in namespace "%s" changed. ' +
            'Old value was "%s", new value is "%s".',
            key,
            namespace,
            storageChange.oldValue,
            storageChange.newValue);
        }
});
4

1 に答える 1

3

https://developer.chrome.com/extensions/storage.htmlを使用しますchrome.storage.local

イベントがありますonChanged

例えば:

settings.js

chrome.storage.sync.set({'value': theValue});

content.js

chrome.storage.onChanged.addListener(function(changes, namespace) {
  for (key in changes) {
    var storageChange = changes[key];
    console.log('Storage key "%s" in namespace "%s" changed. ' +
                'Old value was "%s", new value is "%s".',
                key,
                namespace,
                storageChange.oldValue,
                storageChange.newValue);
  }
});
于 2013-02-08T05:30:48.673 に答える