【发布时间】:2020-08-11 04:10:22
【问题描述】:
我正在制作一个 chrome 扩展来保存和加载网页。 (例如,您想在页面被更改之前存储它,或者您自己修改了该页面并想要保存它)。
我现在所做的是在 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];'});
});
};
这没问题,但我希望它是 chrome.storage.sync,以便将其同步到用户的 Google 帐户。
(如果它使用它的 url 作为键,这也不会干扰页面)
我怎样才能做到这一点?
我尝试在选项卡内使用chrome.storage.sync.set,但这不起作用(我假设它必须以某种方式在 popup.js 中完成
谢谢。
【问题讨论】:
-
我担心
chrome.storage.sync不适合您的问题,因为它有severe restrictions -
@hindmost 哦,这似乎很糟糕。所以保持现状是最好的选择?
-
是的,虽然我会说这是你的唯一选择)
-
@hindmost 我可以在此处将评论标记为答案吗?
-
不。但是您可以发布自己的答案。我不会介意的。
标签: javascript html dom google-chrome-extension