-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
233 lines (200 loc) · 6.6 KB
/
app.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
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
227
228
229
230
231
232
233
submitHit();
chrome.runtime.sendMessage({requestSettings: true}, function(res) {
freedom = res.freedom;
distractions = res.distractions;
blockIfFree();
fetchVisits();
attachSearchLogger();
});
function attachSearchLogger() {
var searchBox = document.getElementById("masthead-search-term");
if (searchBox === null) {
console.log("Could not attach");
} else {
console.log("Attaching");
searchBox.onkeydown = submitReason;
}
}
function fetchVisits() {
chrome.runtime.sendMessage({requestVisits: true}, function(res) {
console.log(res);
var logoBox = document.getElementById("yt-masthead").getElementsByClassName("yt-masthead-logo-container")[0];
if (logoBox != null) {
var logoMetric = document.createTextNode(Math.ceil(res.visits.length/2));
var logoMetricBox = document.createElement("div");
logoMetricBox.setAttribute("style", "margin: 5px; background-color: #cc181e; color: #FFFFFF; display: inline; border-radius: 3px; padding: 5px; position:relative; top: 2px; font-weight: bold;");
logoMetricBox.appendChild(logoMetric)
logoBox.appendChild(logoMetricBox);
}
var counterBox = document.getElementById('turkey-counter');
if (counterBox != null) {
var counterMetric = document.createTextNode("You've visited YouTube " + Math.ceil(res.visits.length/2) + " times today.");
counterBox.appendChild(counterMetric);
}
var graphsBox = document.getElementById('turkey-graphs');
var timeList = [];
for (var i = 0; i < res.visits.length; i++) {
var time = getTimeStringFromTimestamp(res.visits[i].visitTime);
if (timeList.length === 0) {
timeList.push(time);
} else if (timeList.length > 0 && time != timeList[timeList.length-1]) {
timeList.push(time);
}
}
if (graphsBox != null) {
var timeMetric = document.createTextNode("Your visits occurred at these times: " + timeList.join(", "));
graphsBox.appendChild(timeMetric);
}
});
}
document.getElementById('content').addEventListener('DOMSubtreeModified', blockOnDebouncedDOMEvents);
function blockOnDebouncedDOMEvents() {
console.log("detected dom subtree modifiedcation");
var content = document.getElementById('content');
if (content != null) {
content.removeEventListener('DOMSubtreeModified', blockOnDebouncedDOMEvents);
setTimeout(function () {
blockIfFree();
var content = document.getElementById('content');
if (content != null) {
content.addEventListener('DOMSubtreeModified', blockOnDebouncedDOMEvents);
}
}, 500);
}
}
function blockIfFree() {
if (freedom === null) {
alert('freedom not set');
} else if (freedom == "false") {
console.log("Allow content");
//window.clearInterval(blockId);
} else {
console.log("Block content!");
blockAndDisplay();
//blockId = window.setInterval(blockAndDisplay, 10);
}
}
function submitReason(event) {
console.log(this.value);
if (event.keyCode == 13) {
chrome.runtime.sendMessage({logSearchTerm: true, reason: this.value}, function(res) {
console.log("Success? " + res);
});
}
}
function submitHit() {
console.log('logging hit');
var http = new XMLHttpRequest();
var queryString = "";
http.open("POST", hits_url, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send(queryString);
}
function removeNode(node, parent) {
if (node != null) {
if (node.parentNode == parent) {
parent.removeChild(node);
} else {
console.log(node.parentNode);
}
}
}
function removeNode(node) {
if (node != null) {
var parent = node.parentNode;
if (parent != null) {
node.parentNode.removeChild(node);
} else {
console.log("Node's parent is null.");
}
} else {
console.log("Node is null.");
}
}
function blockAndDisplay() {
var container = document.getElementById('page');
if (distractions == "true") {
// modify player page
console.log("killing sidebar and discussion");
var watchMain = document.getElementById("watch7-main");
var watchSidebar = document.getElementById("watch7-sidebar");
var watchDiscussion = document.getElementById("watch-discussion");
removeNode(watchSidebar);
removeNode(watchDiscussion);
// modify home page
//var feed = document.getElementById("feed");
var feedMain = document.getElementById("feed-main-what_to_watch");
removeNode(feedMain);
// modify trending page
var browseItems = document.getElementById("browse-items-primary");
removeNode(browseItems);
} else {
console.log("killing all content");
var content = document.getElementById('content');
removeNode(content);
}
// remove creative if it exists
var creative = document.getElementById("ad_creative_1");
removeNode(creative);
// remove appnav bar
var appnav = document.getElementById("masthead-appbar-container");
removeNode(appnav);
var mastheadPositioner = document.getElementById("masthead-positioner-height-offset");
mastheadPositioner.setAttribute("style", "height: 40px;");
// add the counter pane if it hasn't been added yet
var maybeCounter = document.getElementById('turkey-counter');
if (maybeCounter == null) {
appendCounterPane(container);
}
// add the question pane if it hasn't been added yet
var maybeQuestion = document.getElementById('question');
if (maybeQuestion == null) {
appendQuestionPane(container);
}
var mastHead = document.getElementById('video-masthead-container');
if (mastHead != null) {
mastHead.innerHTML = "";
}
}
function appendCounterPane(container) {
var counterBox = document.createElement('div');
counterBox.setAttribute('id', 'turkey-counter');
counterBox.setAttribute('style', 'margin: 20px;')
container.appendChild(counterBox);
var graphsBox = document.createElement('div');
graphsBox.setAttribute('id', 'turkey-graphs');
graphsBox.setAttribute('style', 'margin: 20px;')
container.appendChild(graphsBox);
}
function appendQuestionPane(container) {
var question = document.createElement('div');
question.setAttribute('id', 'question');
question.setAttribute('style', 'margin: 20px;')
var textbox = document.createElement('input');
textbox.setAttribute('type', 'text');
textbox.setAttribute('name', 'reason');
textbox.onkeypress = submitReason;
var questionText = document.createTextNode("Why are you on YouTube?");
question.appendChild(questionText);
question.appendChild(textbox);
container.appendChild(question);
}
function getTimeStringFromTimestamp(timestamp) {
var date = new Date(timestamp);
var timeString = "";
var hours = date.getHours();
var ampm = " AM";
if (hours > 12) {
hours -= 12;
ampm = " PM";
}
var minutes = date.getMinutes() + "";
if (minutes.length === 1) {
minutes = "0" + minutes;
}
timeString += hours;
timeString += ":";
timeString += minutes;
timeString += ampm;
return timeString;
}