我一直在尝试编写一个chrome扩展程序,该扩展程序可以跟踪所有用户在其浏览器窗口中滚动并将此数据发送到chrome.storage.sync
我一直没有成功查看chrome.storage中记录的任何数据,并且不确定这是因为我在下面包含的滚动.js脚本,还是我没有正确定向到.storage。
我无法从此处发布的其他答案中找到解决方案,因此任何帮助将不胜感激!
manifest.json
{
"manifest_version": 2,
"name": "Caressing the Silver Rectangle",
"description": "Measures Jesse Bowling's distance scrolled in pixels on Google Chrome",
"version": "1.0",
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": [
"scroller.js"
],
"run_at": "document_start"
}
],
"background": [
{ "scripts": ["https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"]
}
],
"browser_action": {
"default_icon":"icon.png",
"default_title": "Caressing the Silver Rectangle",
"default_popup": "popup.html"
},
"permissions": [
"activeTab",
"<all_urls>",
"tabs",
"storage"
]
}
滚动条.js
/*jslint devel: true */
var totalScroll;
var lastKnownScrollPos = 0;
window.addEventListener("scroll", function () {
"use strict";
console.log(lastKnownScrollPos);
totalScroll += Math.abs(window.scrollY - lastKnownScrollPos);
lastKnownScrollPos = window.scrollY;
chrome.storage.local.sync({ scroll: totalScroll });
});
没有方法.sync
.
要将数据放入存储中,您应该使用:
chrome.storage.local.set({scroll: totalScroll})
考虑一下,您需要哪种类型的存储:
- local
存储用于将数据本地保存在 Chrome 实例内
- sync
存储有助于在浏览器之间同步数据
chrome.storage.sync
和chrome.storage.local
实现StorageArea
接口。这里描述了它的方法