读写chrome.storage.local

ops/2024/11/1 10:32:40/

修改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);
}

http://www.ppmy.cn/ops/130109.html

相关文章

总分441数一149专137东南大学820信号数电考研经验电子信息与通信工程电路原920专业基础综合,真题,大纲,参考书。

一. 写在前面的话 本人是23年考生,本科就读于西电电子信息工程,以441分总分(数学一149,英语83,专业课820(原920信号和数电专业基础综合)137,政治73)考上东南信院电路与系…

appium+mumu模拟器+python 嚼碎菜鸟教程

1、android sdk 下载安装 下载地址:https://www.androiddevtools.cn/index.html# 选择版本:android sdk【sdk tools:installer_r24.4.1-windows.exe】 参考步骤:https://blog.csdn.net/2401_83004375/article/details/139300339 2、jdk 安装…

官方工具重装Windows 11当前版本 /绕过硬件检查/免U盘

官方工具重装Windows 11当前版本 /绕过硬件检查/免U盘 官方工具重装Windows 11当前版本 /绕过硬件检查/免U盘_win11安装跳过检测-CSDN博客

2024.10|AI/大模型在机器人/自动驾驶/智能驾舱领域的最新应用和深度洞察

AI/大模型-机器人 1. 大语言模型(LLM)在机器人领域的应用 多模态大语言模型(LLM)正逐步被应用于机器人控制和操作任务中。例如,ManipLLM是一种结合多模态输入的大语言模型,能够实现复杂的物体操作任务。这…

智慧养老/社区养老/家政预约/老年护理 小程序

1.支持内部外部护工人员登录,接单、更改预约日期、联系客户、收入提现等 2.记录从下单到派单、服务、评价的全流程,实时查看服务进度 3.护工人员到达现场后根据情况报价补差价或尾款 4.服务订单可指定某人员接单,也可护工人员自由 5.支持…

StringBuilder为何比String节省效率

StringBuilder为何比String节省效率 通常说StringBuilder比String节省效率一般是指在对字符串进行一定的操作,比如拼接、反转等,那么究竟为什么节省效率呢,本篇将从字符串拼接的原理来讲述 字符串拼接的两种实现 在Java中用加号对字符串进行…

大数据计算里的Broadcast Hash Join/Shuffle Hash Join/Sort Merge Join

文章目录 Broadcast Hash Join场景 Shuffle Hash Join场景 Sort Merge Join场景 Broadcast Hash Join 场景 大表和小小表,直接把B表加载到内存,然后读块1内容和内存中数据匹配 Shuffle Hash Join 场景 大表和小表JOIN ,小表分块后能加载…

C++智能指针的实现

本篇文章详细探讨下如何使用裸指针实现智能指针。 补充内容 由于本篇文章主要是探讨怎么实现三种智能指针,但是在编码过程中,博主可能会使用些有些同学不了解的特性,为了保证大家思绪不被打断,博主先把这些小特性介绍出来,大家选择性参考。 1、什么是RAII? RAII(Reso…