-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
126 lines (106 loc) · 3.04 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*global chrome:true */
chrome.extension.onMessage.addListener(function(msg, _, sendResponse) {
if (msg.msg === 'popupAddPage') {
addPage(msg, sendResponse);
} else if (msg.msg === 'popupCheckDiffs') {
console.log("checkDiffs not implemented");
sendResponse('Not implemented');
}
return true;
});
function addPage(msg, sendResponse) {
var url = msg.url;
console.log("GET " + url);
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState === this.DONE) {
var resultMsg = '';
console.log("XHR: ", xhr);
/*
resultMsg = "Page saved " + (getPrev(url) ? "again..." : "!");
store(url, xhr.responseText);
//diffStored(url);
saveSynced(url, msg.title);
sendResponse(resultMsg);
*/
// Check that this page has been added
chromeGetPrev(url, function (item) {
resultMsg = "Page saved " + (item ? "again..." : "!");
// Save url's content to chrome.storage.local
chromeStore(url, xhr.responseText, function () {
// Add url pageList at chrome.storage.sync
saveSynced(url, msg.title, function () {
// Finally, response the 'addPage' message
sendResponse(resultMsg);
});
});
});
}
};
xhr.send();
}
function saveSynced(url, title, callback) {
// Save this entry to storage.sync
var lengthByte = 100;
var hash = "ABCDEFG";
var checkIntervalSecond = 600;
var changed = false;
var item = {};
item[url] = {
title: title,
dateAdd: Date.now(),
lengthByte: lengthByte,
hash: hash,
checkIntervalSecond: checkIntervalSecond,
changed: changed
};
chrome.storage.sync.set(item, function () {
callback();
});
}
function prevKey(url) { return url + "$$$PREV"; }
function getPrev(url) { return localStorage.getItem(prevKey(url)); }
function getCurr(url) { return localStorage.getItem(url); }
function store(url, responseText) {
var prev = getCurr(url);
localStorage.setItem(prevKey(url), prev);
localStorage.setItem(url, responseText);
}
function diffStored(url) {
var prev = getPrev(url);
var cur = getCurr(url);
if (prev == cur) {
console.log("SAME SAME!");
return false;
}
var prevDiv = document.createElement("div");
prevDiv.innerHTML = getPrev(url);
var currDiv = document.createElement("div");
currDiv.innerHTML = getCurr(url);
diffDOMConsole(prevDiv, currDiv);
}
function chromeGetPrev(url, callback) {
chrome.storage.local.get(prevKey(url), function (value) {
callback(value);
});
}
function chromeGetCurr(url, callback) {
chrome.storage.local.get(url, function (value) {
callback(value);
});
}
function chromeStore(url, responseText, callback) {
chromeGetCurr(url, function (curr) {
var item = {};
item[prevKey(url)] = curr[url] || '';
console.log(curr);
chrome.storage.local.set(item, function () {
var i = {};
i[url] = responseText;
chrome.storage.local.set(i, function () {
callback();
});
});
});
}