-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblissfully.js
319 lines (258 loc) · 8.69 KB
/
blissfully.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
(function(window, document, undefined) {
window.Blissfully = (function() {
// chrome.storage.sync.clear();
// Default config parameters
var DEFAULT_CONFIG = {
words: [],
blockCount: 0,
};
var CONFIG = DEFAULT_CONFIG;
/**
* Saves an object to a storage key
*
* @param {String} storageKey - name of place to store data
* @param {String} json - a JSON string being saved
*/
function setChromeStorage(storageKey, obj) {
// Initialize an object to store
var storageObj = {};
storageObj[storageKey] = obj;
// Save object to chrome storage
chrome.storage.sync.set(storageObj, function() {
CONFIG = obj;
});
}
/**
* Returns a storage key's value
*
* @param {String} storageKey - name of place to store data
* @param {Object} config - object to save chrome data to.
*/
function getChromeStorage(storageKey) {
chrome.storage.sync.get(storageKey, function(data) {
CONFIG = data[storageKey];
});
}
/**
* Function that completely loads and configures storage
*
* @param {String} storageKey - name of chrome data object
* @param {String} config - JSON string of current config
*/
function loadStorage(storageKey, callback) {
// Get object (based on key) from Chrome storage
chrome.storage.sync.get(storageKey, function(data) {
if (data[storageKey] === undefined) {
setChromeStorage(storageKey, CONFIG);
} else {
CONFIG = data[storageKey];
}
callback();
});
}
/**
* Creates a MutationObserver instance that will detect
* anytime an element is added (or removed) from the DOM
* - Only will searchDOM on the homePage though.
*/
function trackDOMChanges() {
// create an observer instance
this.observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (window.location.pathname == '/') {
// console.log('We are on the home page!!!')
var nodeList = mutation.addedNodes;
// Make sure nodes were added and they are elements
if (nodeList.length && nodeList[0].nodeType === 1) {
searchDOM(nodeList[0], filterDivs);
}
}
});
});
this.isActive = false;
// configuration of the observer:
this.observerConfig = { attributes: true, childList: true,
subtree: true, characterData: true };
// pass in the target node, as well as the observer options
}
/**
* Calls the Mutation Object's .observe() method, and
* marks the observer as active
*/
trackDOMChanges.prototype.observe = function() {
this.isActive = true;
this.observer.observe(document, this.observerConfig);
}
/**
* Calls the Mutation Object's .disconnect() method, and
* marks the observer as inactive
*/
trackDOMChanges.prototype.disconnect = function() {
this.isActive = false;
this.observer.disconnect();
}
/**
* Checks to see what page user is on by logging
* current URL. Disconnects the observer if
* not on homepage and reconnects if on homepage
*/
trackDOMChanges.prototype.validateInstance = function() {
var _this = this;
setInterval(function() {
var homePage = window.location.pathname === '/';
// console.log('Is Homepage: ', homePage);
// Disconnect observer if user not on homepage
if (!homePage) {
if (_this.isActive) {
// console.log("I'm disconnecting the observer");
_this.disconnect();
}
}
// Observe the DOM if user is on homepage
if (homePage) {
if (!_this.isActive) {
// console.log("I'm going to observe again.");
_this.observe();
}
}
}, 250);
}
/**
* Search DOM for elements that might be user posts and executes callback
* (callback will apply filter for each element)
*
* @param {Object} nodeList - Added nodes to be searched
* @param {Callback} callback - Function to execute upon DOM search
*/
function searchDOM(nodeList, callback) {
var configObj = CONFIG;
// console.log(nodeList);
var divList = nodeList.getElementsByTagName('div');
if (divList.length) {
// Sort through node list
for (var i = 0, len = divList.length; i < len; i++) {
var currentDiv = divList[i];
var currentDivID = currentDiv.getAttribute('id');
if ((/hyperfeed_story_i|u_jsonp|u_ps|substream/g).test(currentDivID)) {
// currentDiv.style.cssText = 'border: 1px solid red';
callback(currentDiv, configObj);
}
}
}
}
/**
* Checks the element to see if it should be filtered or not
*
* @param {Object} element - The element to apply a filter on
* @param {Object} configObj - object containing all the filter keywords
*/
function filterDivs(element, configObj) {
if (configObj['words'].length) {
var regExp = new RegExp('\\b(' + configObj['words'].join('|') + ')\\b' , 'gi');
if (element.parentElement.classList.contains('_4-u2')) {
// If the div matches a word in the configObj, hide it
if (regExp.test(element.textContent)) {
blockDiv(element);
}
}
}
}
/**
* Hide an element from a facebook timeline. Use a className
* to detect whether this element has been parsed by Blissfully
* yet. If not, then increment the counter
*
* @param element - element that's being hidden by Blissfully
*/
function blockDiv(element) {
var hiddenStyle = 'display: none;';
// var hiddenStyle ='border: 1px solid green;'; //<- for debug
if (!element.classList.contains('blissfully__Blocked')) {
element.className += 'blissfully__Blocked';
incrementBlockCounter();
}
element.style.cssText = hiddenStyle;
}
/**
* log # of times keywords have been blocked
*
*/
function incrementBlockCounter() {
CONFIG['blockCount'] += 1;
setChromeStorage('blissfulData', CONFIG);
}
/**
* Add a keyword to the Blissfully blacklist
*
* @param keyword - word to block
* @param key - the config object's key
*/
function addToFilter(keyword, key) {
// If keyword does not exist, add to configObj
if (CONFIG[key].indexOf(keyword) == -1) {
CONFIG[key].push(keyword);
}
// Update the Chrome Storage
setChromeStorage('blissfulData', CONFIG);
}
/**
* Remove word from blacklist
*
* @param {String} word - Word to remove
* @param {String} type - type of source to filter (is it a word, or a domain name?)
*/
function removeFromFilter(keyword, type) {
var index = CONFIG[type].indexOf(keyword);
// Add a check to ensure it's deletable, just in case
if (index > -1) {
CONFIG[type].splice(index, 1);
}
// Save to Global Config variable
setChromeStorage('blissfulData', CONFIG);
}
/**
* Send the value of blockCount to the background script
*/
function sendBlockCount() {
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.method == "getLocalData") {
sendResponse({ blockCount: CONFIG['blockCount'] });
}
});
}
/**
* itshappening.gif!! Let's get the party started
* and initialize Blissfully!!
*/
function initialize() {
loadStorage('blissfulData', function() {
if (window.location.pathname == '/') {
searchDOM(document.getElementById('substream_0'), filterDivs);
searchDOM(document.getElementById('substream_1'), filterDivs);
}
// searchDOM(document, filterDivs);
var DOMObserver = new trackDOMChanges();
DOMObserver.observe();
DOMObserver.validateInstance();
sendBlockCount();
});
}
return {
init: initialize,
config: function() {
return CONFIG;
},
addToFilter: addToFilter,
removeFromFilter: removeFromFilter
};
})();
Blissfully.init();
})(window, document);
/*
//TODO
√ TRIM whitespace before and after a word
√ Adjust nodelist params that are scanned. Make sure to scan hyperfeed
Add a class to the item that's going to be blocked
Make sure script runs on homepage, but excludes status update box. Only
for contentArea
*/