Last active
November 8, 2017 18:28
-
-
Save tkarabela/304c1e0567b38473835c to your computer and use it in GitHub Desktop.
Facebook Posts Filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // ==UserScript== | |
| // @name Facebook Posts Filter | |
| // @description Regex-based filter for Facebook posts, comments and likes. | |
| // @version 1.3 | |
| // @namespace https://github.com/tkarabela | |
| // @include http://*.facebook.com/* | |
| // @include https://*.facebook.com/* | |
| // @grant GM_registerMenuCommand | |
| // @grant GM_getValue | |
| // @grant GM_setValue | |
| // ==/UserScript== | |
| // ---------------------------------------------------------------------------- | |
| /* | |
| GreaseMonkey API polyfill | |
| Tested with usi 0.1.28 (https://addons.mozilla.org/en-us/firefox/addon/userunified-script-injector) | |
| GM_{get,set,delete,list}Value - implemented with Local Storage | |
| GM_registerMenuCommand - dummy implementation | |
| */ | |
| { | |
| const PREFIX = "_greasemonkey_polyfill_"; | |
| let _GM_getValue = (name, default_) => { | |
| let data = window.localStorage.getItem(PREFIX + name); | |
| return (data !== null) ? JSON.parse(data)[0] : default_; | |
| } | |
| let _GM_setValue = (name, value) => { | |
| window.localStorage.setItem(PREFIX + name, JSON.stringify([value])); | |
| } | |
| let _GM_deleteValue = (name) => { | |
| window.localStorage.removeItem(PREFIX + name); | |
| } | |
| let _GM_listValues = () => { | |
| let arr = []; | |
| for (let i = 0; i < window.localStorage.length; i++) { | |
| let key = window.localStorage.key(i); | |
| if (key.startsWith(PREFIX)) { | |
| arr.push(key.substr(PREFIX.length)); | |
| } | |
| } | |
| return arr; | |
| } | |
| let _GM_registerMenuCommand = (caption, commandFunc, accessKey, accelKey, accelModifiers) => { | |
| console.log("GreaseMonkey polyfill: GM_registerMenuCommand is unsupported"); | |
| }; | |
| if (typeof(GM_getValue) === typeof(undefined)) GM_getValue = _GM_getValue; | |
| if (typeof(GM_setValue) === typeof(undefined)) GM_setValue = _GM_setValue; | |
| if (typeof(GM_deleteValue) === typeof(undefined)) GM_deleteValue = _GM_deleteValue; | |
| if (typeof(GM_listValues) === typeof(undefined)) GM_listValues = _GM_listValues; | |
| if (typeof(GM_registerMenuCommand) === typeof(undefined)) GM_registerMenuCommand = _GM_registerMenuCommand; | |
| } | |
| // ---------------------------------------------------------------------------- | |
| const INITIAL_DELAY = 500; | |
| const REFRESH_INTERVAL = 2000; | |
| const IMPOSSIBLE_REGEX = /.^/; | |
| const PATTERN_STORAGE_KEY = "pattern"; | |
| const CHECKED_CLASS = "_FBPOSTFILTER_CHECKED"; | |
| const SETTINGS_LINK_ID = "_FBPOSTFILTER_SETTINGS_LINK"; | |
| var pattern = new RegExp(GM_getValue(PATTERN_STORAGE_KEY, IMPOSSIBLE_REGEX)); | |
| var hiddenCount = 0; | |
| // ---------------------------------------------------------------------------- | |
| let hidePredicate = (node) => node.textContent.search(pattern) != -1; | |
| let uncheckedPredicate = (node) => !node.classList.contains(CHECKED_CLASS); | |
| function main() { | |
| addSettingsButton(); | |
| let examinedNodes = []; | |
| let nodesToHide = []; | |
| let posts = [...document.querySelectorAll("div[role='article'] > div:first-child, article > div:first-child")].filter(uncheckedPredicate); | |
| examinedNodes.push(...posts); | |
| nodesToHide.push(...posts.filter(hidePredicate).map(node => node.parentNode)); | |
| let comments = [...document.querySelectorAll("li.UFIComment")].filter(uncheckedPredicate); | |
| examinedNodes.push(...comments); | |
| nodesToHide.push(...comments.filter(hidePredicate)); | |
| let likes = [...document.querySelectorAll("a.profileLink, .like_def a")].filter(uncheckedPredicate); | |
| examinedNodes.push(...likes); | |
| nodesToHide.push(...likes.filter(hidePredicate)); | |
| examinedNodes.forEach(node => node.classList.add(CHECKED_CLASS)); | |
| if (nodesToHide.length > 0) { | |
| hiddenCount += nodesToHide.length; | |
| console.log("Hidden some items:", nodesToHide); | |
| nodesToHide.forEach(node => node.hidden = true); | |
| let settingsLink = document.getElementById(SETTINGS_LINK_ID); | |
| if (settingsLink) { | |
| settingsLink.title = `FB Posts Filter – settings (hidden objects: ${hiddenCount})`; | |
| } | |
| } | |
| setTimeout(main, REFRESH_INTERVAL); | |
| } | |
| function changeSettings() { | |
| let oldSrc = pattern.source; | |
| let newSrc = prompt("Please enter regex pattern for posts/comments to be hidden.\n(Example: keyword1|keyword2)", oldSrc); | |
| if (newSrc === null) return; // prompt closed | |
| try { | |
| if (/^\s*$/.test(newSrc)) throw "Pattern must not be empty!\n(Would match anything.)"; | |
| pattern = new RegExp(newSrc); | |
| GM_setValue(PATTERN_STORAGE_KEY, newSrc); | |
| console.log("Changed pattern to:", newSrc); | |
| } catch (e) { | |
| alert(`Error: ${e}`); | |
| } | |
| } | |
| function addSettingsButton() { | |
| if (document.getElementById(SETTINGS_LINK_ID)) return; | |
| let link = document.createElement("a"); | |
| link.id = SETTINGS_LINK_ID; | |
| link.innerHTML = "π"; | |
| link.title = "FB Posts Filter – settings"; | |
| link.onclick = changeSettings; | |
| link.style.fontFamily = "serif"; | |
| link.style.display = "block"; | |
| link.style.float = "right"; | |
| let regularFooter = document.querySelector(".rhcFooterCopyright"); | |
| let mobileFooter = null; // FIXME | |
| if (regularFooter) { | |
| regularFooter.appendChild(link); | |
| console.log("Added settings link (desktop)"); | |
| } else if (mobileFooter) { | |
| mobileFooter.appendChild(link); | |
| console.log("Added settings link (mobile)"); | |
| } | |
| } | |
| // ---------------------------------------------------------------------------- | |
| GM_registerMenuCommand("FB Posts Filter – settings", changeSettings); | |
| setTimeout(main, INITIAL_DELAY); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment