This repository was archived by the owner on Feb 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcontentScript.js
More file actions
75 lines (63 loc) · 2.14 KB
/
contentScript.js
File metadata and controls
75 lines (63 loc) · 2.14 KB
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
let seenTweets = [];
let unSeenTweets = [];
// Date to timestamp - thanks Stack overflow
Date.prototype.yyyymmdd = function () {
var mm = this.getMonth() + 1; // getMonth() is zero-based
var dd = this.getDate();
return [
this.getFullYear(),
(mm > 9 ? "" : "0") + mm,
(dd > 9 ? "" : "0") + dd,
].join("");
};
// load all the tweets that has been seen so far that day
let today = new Date().yyyymmdd();
chrome.storage.local.get(today, function (store) {
if (!!store[today]) {
seenTweets = JSON.parse(store[today]);
} else {
chrome.storage.local.clear();
}
});
function listTweetIds() {
let tweets = document.querySelectorAll("article");
let reg = new RegExp(/\/*\/status\/(?<tweetId>[0-9]+)/);
tweets.forEach(function (tweet) {
let links = tweet.querySelectorAll("a");
for (let i = 0; i < links.length; i++) {
let href = links[i].getAttribute("href");
let matches = href.match(reg);
if (!matches) {
continue;
}
if (seenTweets.indexOf(matches.groups.tweetId) !== -1) {
tweet.setAttribute("style", "opacity: 50%;");
} else if (unSeenTweets.indexOf(matches.groups.tweetId) === -1) {
unSeenTweets.push(matches.groups.tweetId);
}
return;
}
});
let store = {};
store[today] = JSON.stringify(seenTweets.concat(unSeenTweets));
chrome.storage.local.set(store);
}
/**
* Show a small indicator to denote that Just Arrived is loaded into the page
*/
function showIndicator() {
const container = document.createElement("div")
container.setAttribute("id", "just-arrived-notifier")
container.setAttribute("style", "position: fixed; top: 10px; right: 10px; padding: 0.5rem; border: 1px solid #29B6F6; background-color: #E1F5FE; z-index: 100; font-size: xx-small;")
const text = document.createTextNode("Just Arrived")
container.appendChild(text);
let body = document.querySelector("body");
body.appendChild(container);
setTimeout(removeIndicator, 15000);
}
function removeIndicator() {
let body = document.querySelector("body");
body.querySelector("#just-arrived-notifier").remove();
}
let timer = setInterval(listTweetIds, 2000);
showIndicator();