forked from imrehorvath/facebook-feeds-filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacebook-feeds-filter.js
84 lines (84 loc) · 3.35 KB
/
facebook-feeds-filter.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
/// facebook-feeds-filter.js
(function() {
const categoriesToHide = '{{1}}'; // Optional argument to filter posts by their category
const setOfCategoriesToHide = (( ) => {
if ( categoriesToHide === '' || categoriesToHide === '{{1}}' ) { return new Set(); }
return new Set(categoriesToHide.split(/\s*\|\s*/).map(s => s.toUpperCase()));
})();
const magic = String.fromCharCode(Date.now() % 26 + 97) +
Math.floor(Math.random() * 982451653 + 982451653).toString(36);
const findNestedProperty = (obj, patt, fn, depth) => {
const helper = (objs, depth) => {
for ( const o of objs ) {
const keys = Object.keys(o).filter(k => k.match(patt));
if ( keys.length === 1 ) { // We expect a single match
return fn(o[keys[0]]);
}
}
if ( depth ) {
return helper(objs.flatMap(o => Object.values(o)
.filter(o => typeof o === 'object' &&
o != null)),
depth - 1);
}
return undefined;
};
return helper(new Array(obj), depth);
};
const processInsertedFeedUnit = (feedUnit, category) => {
switch ( category ) {
case 'ORGANIC':
// Organic feed units are the regular feed units and gets never filtered
break;
case 'SPONSORED':
// Sponsored feed units gets always filtered
feedUnit.classList.add(magic);
break;
default:
// Categories like ENGAGEMENT and PROMOTION gets filtered optionally.
if ( setOfCategoriesToHide.has(category) ) {
feedUnit.classList.add(magic);
}
}
};
const checkWhetherFeedUnit = ( node ) => {
if ( node instanceof HTMLDivElement ) {
const keys = Object.keys(node).filter(k => k.startsWith('__reactProps'));
if ( keys.length === 1 ) {
const obj = node[keys[0]];
const root = obj.children &&
obj.children.props &&
obj.children.props.children &&
obj.children.props.children.props;
if ( root ) {
const category = findNestedProperty(root, /feed/, o => o.category, 4);
if ( category ) {
processInsertedFeedUnit(node, category);
}
}
}
}
};
const start = ( ) => {
const style = document.createElement('style');
style.innerHTML = `
.${magic} {
display: none !important;
}
`;
document.head.appendChild(style);
const observer = new MutationObserver(mutations => {
mutations.forEach((mutation) => {
mutation.addedNodes.forEach((node) => {
checkWhetherFeedUnit(node);
});
});
});
observer.observe(document.body, { childList: true, subtree: true });
};
if ( document.readyState === 'loading' ) {
document.addEventListener('DOMContentLoaded', start, { once: true });
} else {
start();
}
})();