forked from fleurilafleur/steemification
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
226 lines (185 loc) · 6.42 KB
/
background.js
File metadata and controls
226 lines (185 loc) · 6.42 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/*
Displays a notification with the current time. Requires "notifications"
permission in the manifest file (or calling
"Notification.requestPermission" beforehand).
*/
function show() {
var time = /(..)(:..)/.exec(new Date()); // The prettyprinted time.
var hour = time[1] % 12 || 12; // The prettyprinted hour.
var period = time[1] < 12 ? 'a.m.' : 'p.m.'; // The period of the day.
Notification.requestPermission( function(status) {
console.log(status); // notifications will only be displayed if "granted"
var n = new Notification("title", {body: "notification body"}); // this also shows the notification
});
}
function showUpvote() {
var notification = new Notification("+1 Upvote", {
icon: 'extension_upvote.png',
body: "" + localStorage.lastUser + " voted on " + getTopicSubstring(localStorage.lastTopic)
});
setTimeout(notification.close.bind(notification), 5000);
notification.onclick = function () {
window.focus();
chrome.tabs.create({ url: localStorage.lastLink });
notification.close();
};
}
function showDownvote() {
var notification = new Notification("-1 Downvote", {
icon: 'extension_upvote.png',
body: "" + localStorage.lastUser + " voted on " + getTopicSubstring(localStorage.lastTopic)
});
setTimeout(notification.close.bind(notification), 5000);
notification.onclick = function () {
window.focus();
chrome.tabs.create({ url: localStorage.lastLink });
notification.close();
};
}
function showComment() {
var notification = new Notification("+1 Comment", {
icon: 'extension_message.png',
body: "" + localStorage.lastUser + " replied to " + getTopicSubstring(localStorage.lastTopic)
});
setTimeout(notification.close.bind(notification), 5000);
notification.onclick = function () {
window.focus();
chrome.tabs.create({ url: localStorage.lastLink });
notification.close();
};
}
function getTopicSubstring(topic)
{
if (topic.length > 62)
{
return topic.substring(0,62) + "...";
}
return topic;
}
// Conditionally initialize the options.
if (!localStorage.isInitialized) {
localStorage.isActivated = true; // The display activation.
localStorage.frequency = 30; // The display frequency, in seconds.
localStorage.isInitialized = true; // The option initialization.
localStorage.firstRequestSend = false;
localStorage.lastTime = 0;
localStorage.lastTopic = null;
localStorage.lastUser = null;
localStorage.lastLink = null;
localStorage.username = "yourusername";
localStorage.settingsChanged = false;
localStorage.notifyUpvotes = true;
localStorage.notifyDownvotes = true;
localStorage.notifyComments = true;
chrome.tabs.create({ 'url': 'chrome://extensions/?options=' + chrome.runtime.id });
}
// Test for notification support.
if (window.Notification) {
// While activated, show notifications at the display frequency.
if (JSON.parse(localStorage.isActivated))
{
}
setInterval(function() {
if (!JSON.parse(localStorage.isActivated))
{
console.log("Not active. Skipping...");
return;
}
else
{
if (localStorage.username == null || localStorage.username.length == 0)
{
console.log("Username is null: Skipping ...");
return;
}
var Api = window.steemWS.Client.get(null, true);
Api.initPromise.then(response => {
Api.database_api().exec("get_account_history", [localStorage.username, -1, 10]).then(response => {
if (localStorage.lastTime == 0 || JSON.parse(localStorage.settingsChanged) == true)
{
console.log("Settings changed or no last update time found. Refreshing...");
localStorage.lastTime = response[response.length-1][1].timestamp;
localStorage.settingsChanged = false;
}
else
{
var historyCounter = response.length-1;
while (! (response[historyCounter][1].op[0] == "vote" || response[historyCounter][1].op[0] == "comment"))
{
historyCounter--;
if (historyCounter > response.length)
{
console.log("No vote or post in the last 10 network entries found!");
return;
}
}
var historyObject = response[historyCounter][1];
if (historyObject.timestamp == localStorage.lastTime)
{
console.log("Last post already updated (Time: " + historyObject.timestamp + "). Skipping");
return;
}
localStorage.lastTime = historyObject.timestamp;
var historyObjectOp = historyObject.op;
var actionType = historyObjectOp[0];
var actionObject = historyObjectOp[1];
var actionUser = "";
var actionTopic = "";
var actionTopicUser = "";
if (actionType == "vote")
{
console.log("Recieved vote object: ", actionObject);
actionUser = actionObject.voter;
actionTopic = actionObject.permlink;
actionTopicUser = actionObject.author;
localStorage.lastTopic = actionTopic;
localStorage.lastUser = actionUser;
localStorage.lastLink = "https://steemit.com/steemit/@" + actionTopicUser + "/" + actionTopic;
if (actionUser == localStorage.username)
{
console.log("User voted on own post. Skipping notification...");
return;
}
if (actionObject.weight > 0)
{
if (JSON.parse(localStorage.notifyUpvotes))
{
showUpvote();
}
}
if (actionObject.weight < 0)
{
if (JSON.parse(localStorage.notifyDownvotes))
{
showDownvote();
}
}
}
if (actionType == "comment")
{
console.log("Recieved comment object: ", actionObject);
actionUser = actionObject.author;
actionTopic = actionObject.permlink;
actionTopicUser = actionObject.author;
localStorage.lastTopic = actionTopic;
localStorage.lastUser = actionUser;
localStorage.lastLink = "https://steemit.com/steemit/@" + actionTopicUser + "/" + actionTopic;
if (actionUser == localStorage.username)
{
console.log("User voted on own post. Skipping notification...");
return;
}
if (JSON.parse(localStorage.notifyComments))
{
showComment();
}
}
}
});
});
}
}, localStorage.frequency * 1000);
}