-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
76 lines (68 loc) · 2.11 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
var target = '*://twitter.com/i/api/graphql/*/UserByScreenName?variables=*';
var indexedDB = (window.indexedDB || window.mozIndexedDB);
var request = indexedDB.open("Note+");
var suid,username,tabid,db;
request.onsuccess = function (event)
{
db = event.target.result;
pageReceiver(event.target.result);
};
request.onupgradeneeded = function (event)
{
let db = event.target.result;
let objStore = db.createObjectStore("users", { keyPath: "uid" });
objStore.createIndex("tag", "tag", { unique: false });
objStore.createIndex("username", "username", { unique: false });
};
function listener(details)
{
let filter = browser.webRequest.filterResponseData(details.requestId);
let decoder = new TextDecoder("utf-8");
let encoder = new TextEncoder();
let data = [];
filter.ondata = event => {
data.push(event.data);
};
filter.onstop = event => {
const mergedUint8Array = new Uint8Array(data.map(typedArray => [...new Uint8Array(typedArray)]).flat());
let obj = JSON.parse(decoder.decode(mergedUint8Array, {stream: true}));
let uid = obj["data"]["user"]["result"]["rest_id"], uname = obj["data"]["user"]["result"]["legacy"]["screen_name"];
suid = uid, username = uname;
filter.write(encoder.encode(JSON.stringify(obj)));
filter.disconnect();
/** Send Message **/
let dbReq = db.transaction(["users"]).objectStore("users").get(uid);
var sdata = {};
dbReq.onsuccess = function(event) {
console.log(sdata);
if(dbReq.result != undefined) {
sdata.comment = dbReq.result.comment;
sdata.tag = dbReq.result.tag;
}
browser.tabs.sendMessage(tabid, {
action: 0,
data: sdata
});
};
};
return {};
}
function pageReceiver(db)
{
browser.runtime.onMessage.addListener(function(req, sender, sendResponse){
switch(req.action) {
case 0:
tabid = sender.tab.id;
browser.webRequest.onBeforeRequest.addListener(
listener,
{urls: [target]},
["blocking"]
);
break;
case 2:
var objectStore = db.transaction("users", "readwrite").objectStore("users");
objectStore.put({'uid': suid, 'username': username, 'comment': req.comment, 'tag': req.tag});
break;
}
});
}