简体   繁体   English

将页面保存到 chrome.storage.sync

[英]Save page to chrome.storage.sync

I'm making a chrome extenstion to save and load an webpage page.我正在制作一个 chrome 扩展来保存和加载网页。 (Eg. You want to store a page before it gets changed, or you've modified the page yourself and want to save it). (例如,您想在页面被更改之前存储它,或者您自己修改了该页面并想要保存它)。

What I've done right now is I am running code in the activetab to save the innerHTML of the page in localstorage with the key of the page URL.我现在所做的是我在 activetab 中运行代码,以使用页面 URL 的键将页面的 innerHTML 保存在 localstorage 中。

'use strict';


let saveButton = document.getElementById('saveButton');

saveButton.onclick = function(element) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  chrome.tabs.executeScript(
      tabs[0].id,
      {code: 'localStorage[window.location.href] = document.documentElement.innerHTML;'});
});
};


let loadButton = document.getElementById('loadButton');

loadButton.onclick = function(element) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  chrome.tabs.executeScript(
      tabs[0].id,
      {code: 'document.documentElement.innerHTML = localStorage[window.location.href];'});
});
};

This works okay, but I want it to be chrome.storage.sync so that it will be synced to the user's Google Account.这工作正常,但我希望它是chrome.storage.sync以便它将同步到用户的 Google 帐户。 (and also this would not interfere with the page if it uses its url as a key) (如果它使用它的 url 作为键,这也不会干扰页面)

How can I accomplish this?我怎样才能做到这一点?

I've tried to use chrome.storage.sync.set inside of the tab, but that doesn't work (I'm assuming it has to somehow be done in the popup.js我尝试在选项卡内使用chrome.storage.sync.set ,但这不起作用(我假设它必须以某种方式在 popup.js 中完成

Thanks.谢谢。

This isn't an appropriate way to do this.这不是执行此操作的适当方法。 chrome.storage.sync has major restrictions. chrome.storage.sync有很大的限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM