修改chrome-extensions-samples\api-samples\storage\stylizr,可以从GitHub下载。读取最好在回调函数里面读取,比较方便。
// Store CSS data in the "local" storage area.
const storage = chrome.storage.local;// Get at the DOM controls used in the sample.
const resetButton = document.querySelector('button.reset');
const submitButton = document.querySelector('button.submit');
const textarea = document.querySelector('textarea');// Load any CSS that may have previously been saved.
loadChanges();submitButton.addEventListener('click', saveChanges);
resetButton.addEventListener('click', reset);const SCREEN_CAPTURE_FORCE= 'screenCaptureForce';
const SUSPEND_IN_PLACE_OF_DISCARD= 'suspendInPlaceOfDiscard';
const SCREEN_CAPTURE= 'screenCapture';
const UNSUSPEND_ON_FOCUS= 'gsUnsuspendOnFocus';
const SUSPEND_TIME= 'gsTimeToSuspend';
const IGNORE_WHEN_OFFLINE= 'onlineCheck';
const IGNORE_WHEN_CHARGING= 'batteryCheck';
const IGNORE_PINNED= 'gsDontSuspendPinned';
const IGNORE_FORMS= 'gsDontSuspendForms';
const IGNORE_AUDIO= 'gsDontSuspendAudio';
const IGNORE_ACTIVE_TABS= 'gsDontSuspendActiveTabs';
const IGNORE_CACHE= 'gsIgnoreCache';
const ADD_CONTEXT= 'gsAddContextMenu';
const SYNC_SETTINGS= 'gsSyncSettings';
const NO_NAG= 'gsNoNag';
const THEME= 'gsTheme';
const WHITELIST= 'gsWhitelist';const DISCARD_AFTER_SUSPEND= 'discardAfterSuspend';
const DISCARD_IN_PLACE_OF_SUSPEND= 'discardInPlaceOfSuspend';
const USE_ALT_SCREEN_CAPTURE_LIB= 'useAlternateScreenCaptureLib';const APP_VERSION= 'gsVersion';
const LAST_NOTICE= 'gsNotice';
const LAST_EXTENSION_RECOVERY= 'gsExtensionRecovery';const SM_SESSION_METRICS= 'gsSessionMetrics';
const SM_TIMESTAMP= 'sessionTimestamp';
const SM_SUSPENDED_TAB_COUNT= 'suspendedTabCount';
const SM_TOTAL_TAB_COUNT= 'totalTabCount';function getSettingsDefaults() {const defaults = {};defaults[SCREEN_CAPTURE] = '0';defaults[SCREEN_CAPTURE_FORCE] = false;defaults[SUSPEND_IN_PLACE_OF_DISCARD] = false;defaults[DISCARD_IN_PLACE_OF_SUSPEND] = false;defaults[USE_ALT_SCREEN_CAPTURE_LIB] = false;defaults[DISCARD_AFTER_SUSPEND] = false;defaults[IGNORE_WHEN_OFFLINE] = false;defaults[IGNORE_WHEN_CHARGING] = false;defaults[UNSUSPEND_ON_FOCUS] = false;defaults[IGNORE_PINNED] = true;defaults[IGNORE_FORMS] = true;defaults[IGNORE_AUDIO] = true;defaults[IGNORE_ACTIVE_TABS] = true;defaults[IGNORE_CACHE] = false;defaults[ADD_CONTEXT] = true;defaults[SYNC_SETTINGS] = false;defaults[SUSPEND_TIME] = '60';defaults[NO_NAG] = false;defaults[WHITELIST] = '';defaults[THEME] = 'light';return defaults;}async function saveChanges() {let st = '{"screenCapture":"0","screenCaptureForce":false,"suspendInPlaceOfDiscard":false,"discardInPlaceOfSuspend":false,"useAlternateScreenCaptureLib":false,"discardAfterSuspend":false,"onlineCheck":false,"batteryCheck":false,"gsUnsuspendOnFocus":false,"gsDontSuspendPinned":true,"gsDontSuspendForms":true,"gsDontSuspendAudio":true,"gsDontSuspendActiveTabs":true,"gsIgnoreCache":false,"gsAddContextMenu":true,"gsSyncSettings":false,"gsTimeToSuspend":"60","gsNoNag":false,"gsWhitelist":"","gsTheme":"light"}'// Get the current CSS snippet from the form.console.log(JSON.stringify(getSettingsDefaults()));let gss=JSON.stringify(getSettingsDefaults());chrome.storage.local.set({ key: JSON.stringify(getSettingsDefaults()) }).then(() => {console.log("Value is set");});chrome.storage.local.get(["key"]).then((result) => {console.log("Value is " + result.key);});chrome.storage.local.get(["gsSettings"]).then((result) => {console.log("gsSettings Value is " + result.gsSettings);textarea.value = result.gsSettings +'\nst: '+st;});// textarea.value = JSON.stringify(getSettingsDefaults());chrome.storage.local.set({ gsSettings: JSON.stringify(getSettingsDefaults()) });// textarea.value = JSON.parse(chrome.storage.local.get('gsSettings'));let result=chrome.storage.local.get(["gsSettings"]);console.log(result);const cssCode = textarea.value;// Check that there's some code there.if (!cssCode) {message('Error: No CSS specified');return;}// Save it using the Chrome extension storage API.await storage.set({ css: cssCode });// textarea.value = getSettingsDefaults();// textarea.value = JSON.stringify(getSettingsDefaults());message('Settings saved');
}function loadChanges() {storage.get('css', function (items) {// To avoid checking items.css we could specify storage.get({css: ''}) to// return a default value of '' if there is no css value yet.if (items.css) {// textarea.value = items.css;textarea.value = chrome.storage.local.get("gsSettings");// textarea.value = JSON.parse(chrome.storage.local.get("gsSettings"));message('Loaded saved CSS.');}});
}async function reset() {// Remove the saved value from storage. storage.clear would achieve the same// thing.await storage.remove('css');message('Reset stored CSS');// Refresh the text area.textarea.value = '';textarea.value = JSON.stringify(getSettingsDefaults());
}let messageClearTimer;
function message(msg) {clearTimeout(messageClearTimer);const message = document.querySelector('.message');message.innerText = msg;messageClearTimer = setTimeout(function () {message.innerText = '';}, 3000);
}