当前位置: 首页 > article >正文

读写chrome.storage.local

修改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.kler.cn/a/374484.html

相关文章:

  • Flutter动画学习二
  • 前端下载文件的几种方式使用Blob下载文件
  • UG NX二次开发(C#)-机电概念设计-UIStyler中selection块选择信号等对象的过滤器设置
  • Mybatis分页插件的使用问题记录
  • Oracle Database 21c Express Edition数据库 和 Sqlplus客户端安装配置
  • taiwindcss
  • 股票基础交易规则——涨跌幅限制、价格笼子?
  • Java:阿里云联络中心“双呼A”功能系统接入
  • vscode | 开发神器vscode快捷键删除和恢复
  • 【VM实战】VMware迁移到VirtualBox
  • 无人机之感知避让技术篇
  • 【网络】什么是 ICMP (Internet Control Message Protocol)?
  • 双指针习题篇(上)
  • 基于SpringBoot的健身房系统的设计与实现(源码+定制+开发)
  • 如何在1个账号上,1个客户6个人同时服务
  • 鸿蒙网络编程系列41-仓颉版HttpRequest模拟登录示例
  • C++——酒店管理系统
  • 二百七十二、Kettle——ClickHouse中增量导入数据重复性统计表数据(1天1次)
  • Python中的PDF处理工具:PyPDF2和ReportLab使用指南
  • 慢sql优化和Explain解析
  • MySQL数据表导入到clickhouse数据库中
  • linux命令行的艺术
  • Spring Boot + Vue:打造高效图书借阅管理平台
  • 第三百零四节 Log4j教程 - Log4j配置
  • 微积分复习笔记 Calculus Volume 1 - 4.3 Maxima and Minima
  • 导出列表数据到Excel并下载