0

私は、manifest.json内のoptions_uiタグで設定されたオプション付きクロム拡張を持っています。私はオプションを保存することができますが、オプションの保存を完了するには、chrome.storage.sync.set関数のオプションを閉じる必要があることに気付きました。オプションのモーダルが閉じていなくても「保存」をクリックすると、オプションを強制的に保存するにはどうすればよいですか?閉じるオプションなしでクロム拡張オプションを保存する

options.js:

function save_options() { 
    var hideAds = document.getElementById('hideAds').checked; 

    chrome.storage.sync.set({ 
     hideAds: hideAds 
    }, function() { 
    // Update status to let user know options were saved 
     var status = document.getElementById('status'); 
    status.textContent = 'Options successfully saved...'; 
    setTimeout(function() { 
     status.textContent = ''; 
    }, 1000); 
    }); 
} 

// Restores checkbox state using the preferences stored in chrome.storage. 
function restore_options() { 
    // Use default values 
    chrome.storage.sync.get({ 
     hideAds: false 
    }, function(items) { 
     document.getElementById('hideAds').checked = items.hideAds; 
    }); 
} 

document.addEventListener('DOMContentLoaded', restore_options); 
document.getElementById('save').addEventListener('click', save_options); 

manifest.jsonを:

{ 
    "name": "Redesign", 
    "version": "1.0", 
    "manifest_version": 2, 
    "options_ui": { 
    "page": "options.html", 
    "chrome_style": true 
    } 
} 

EDIT:その下のbackground.jsコードを追加すると、上のボタンをクリックした後、保存(最新のオプションを取得しません。オプションのモーダルが閉じられていない限り)。下の行のalertは、オプションのモーダルが閉じられた後にのみ、古い保存されたオプションの値...と新しい保存された値を出力します。

あなたが背景ページで chrome.storage.onChangedイベント(あなたが記憶値を取得するため chrome.tabs.onUpdatedに耳を傾ける必要はありませんでしょう)、火災一つ以上の項目が変更に耳を傾けることができ
chrome.tabs.onUpdated.addListener(function(tabId, info, tab) { 
    if (info.status == 'complete') { 
      chrome.storage.sync.get(['hideAds'], function(items) { 

       if(typeof items.hideAds !== 'undefined') { 
        hideAds = items.hideAds; 
        alert(hideAds); 
       } 
      }) 
      doSomething(tab); 
     } 
}); 
+1

モーダルを閉じるまでオプションは保存されませんか? –

+0

上記のbackground.jsコードを追加して、オプションのモーダルウィンドウが閉じられるまで古いオプション値を出力します。 – zeeshan

答えて

0

:あなたは思いますなぜ

chrome.storage.onChanged.addListener(function(changes, areaName) { 
    if(areaName === 'sync') { 
     const hideAdsChange = changes['hideAds']; 
     if(typeof hideAdsChange !== 'undefined') { 
      const newValue = hideAdsChange.newValue; 
      console.log(newValue); 
     } 
    } 
}); 
関連する問題