Last active
November 23, 2016 13:41
-
-
Save jaukia/beb792388251a8927b1ac56c3148fe56 to your computer and use it in GitHub Desktop.
User script for hiding all posts with links from Facebook newsfeed
This file contains 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 Filter FB posts with links | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description try to take over the world! | |
// @author You | |
// @match *://www.facebook.com/* | |
// @exclude *://www.facebook.com/plugins/* | |
// @exclude *://www.facebook.com/ajax/* | |
// @exclude *://www.facebook.com/xti.php* | |
// @grant none | |
// @run-at document-start | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
//console.log("filter fb set up"); | |
var CONTENT_HAS_LINKS_SELECTORS = ".userContentWrapper, ._5pcr"; | |
var BLOCK_LIST_SELECTORS = "._5r69, ._5g-l, ._2r3x, .uiLikePageButton, .uiStreamSponsoredLink"; | |
var observer; | |
var contentAreaFoundTimeout; | |
function findAncestor (el, cls) { | |
while ((el = el.parentElement) && !el.classList.contains(cls)); | |
return el; | |
} | |
function filterPosts(elements) { | |
//console.log("filterPosts"); | |
if(!Array.isArray(elements) && !(elements instanceof NodeList)) { | |
return filterPosts([elements]); | |
} | |
//console.log("elements",elements); | |
for (var i = 0; i < elements.length; i++) { | |
var element = elements[i]; | |
var elementDirty = false; | |
//console.log("element",element); | |
if (element.matches(BLOCK_LIST_SELECTORS) || element.querySelectorAll(BLOCK_LIST_SELECTORS).length>0) { | |
elementDirty = true; | |
} else { | |
var allLinks = element.querySelectorAll('a'); | |
for (var j = 0; j < allLinks.length; j++) { | |
var str = allLinks[j].getAttribute("href"); | |
if (str !== undefined && !str.startsWith("/") && !str.startsWith("#") && !str.includes("www.facebook.com")) { | |
elementDirty = true; | |
break; | |
} | |
} | |
} | |
//console.log("element dirty",elementDirty, element, element.innerText); | |
// could possibly do this the other way around as well, by hiding everything by default and | |
// showing only relevant entries | |
if(elementDirty) { | |
findAncestor(element, "_4-u2").style.display = "none"; | |
//element.className += " userFilterBlocked"; | |
} else { | |
//element.className += " userFilterApproved"; | |
} | |
} | |
} | |
function mutationsHandler(mutations) { | |
mutations.forEach(function(mutation) { | |
var newNodes = mutation.addedNodes; | |
if (newNodes !== null) { | |
for (var i = 0; i < newNodes.length; i++) { | |
if (newNodes[i] && newNodes[i].className && (newNodes[i].className.contains("userContentWrapper") || newNodes[i].className.contains("_5pcr"))) { | |
filterPosts(newNodes[i]); | |
} else if (newNodes[i] && newNodes[i].querySelectorAll) { | |
filterPosts(newNodes[i].querySelectorAll(CONTENT_HAS_LINKS_SELECTORS)); | |
} | |
} | |
} | |
}); | |
} | |
function setupHistoryStateListener(callback) { | |
(function (old) { | |
window.history.pushState = function () { | |
old.apply(window.history, arguments); | |
console.log("push state"); | |
callback(); | |
}; | |
})(window.history.pushState); | |
} | |
function main() { | |
//console.log("main"); | |
if (window.top != window.self) { | |
return; | |
} | |
if(window.self.location.pathname !== "/") { | |
console.log("not on main feed"); | |
return; | |
} | |
if(!window.self.location.search) { | |
window.self.location.search = "?sk=h_chr"; | |
} | |
var contentArea = document.getElementById("contentArea"); | |
if(!contentArea) { | |
console.log("contentarea not found, retry soon"); | |
if(contentAreaFoundTimeout) { | |
clearTimeout(contentAreaFoundTimeout); | |
contentAreaFoundTimeout = null; | |
} | |
contentAreaFoundTimeout = setTimeout(main, 200); | |
return; | |
} | |
console.log("contentarea found"); | |
observer.disconnect(); | |
observer.observe(contentArea, { attributes: false, childList: true, characterData: false, subtree: true }); | |
filterPosts(document.querySelectorAll(CONTENT_HAS_LINKS_SELECTORS)); | |
setTimeout(function() { | |
document.body.scrollTop = 160; | |
contentArea.style.display = "block"; | |
},100); | |
} | |
function setup() { | |
//console.log("set up"); | |
observer = new MutationObserver(mutationsHandler); | |
if (document.readyState === "complete") { | |
main(); | |
} else { | |
window.addEventListener("load", main); | |
} | |
setupHistoryStateListener(function() { | |
setTimeout(main,1000); | |
}); | |
var style = document.createElement('style'); | |
style.type = 'text/css'; | |
style.innerHTML = | |
'.home .newsFeedComposer #contentArea { display:none; }'+ | |
// '.home._3og5 .homeWiderContent #contentArea, .home._5wel .homeWiderContent #contentArea { display:block; }'; | |
document.getElementsByTagName('head')[0].appendChild(style); | |
} | |
setup(); | |
})(); |
This file contains 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 Style FB feed | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description try to take over the world! | |
// @author You | |
// @match *://www.facebook.com/* | |
// @exclude *://www.facebook.com/plugins/* | |
// @exclude *://www.facebook.com/ajax/* | |
// @exclude *://www.facebook.com/xti.php* | |
// @grant none | |
// @run-at document-start | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
var isRun = false; | |
function init() { | |
if(isRun) return; | |
isRun = true; | |
// removes borders on fb posts, both empty and non-empty ones | |
var style = document.createElement('style'); | |
style.type = 'text/css'; | |
style.innerHTML = | |
'._4-u2 { border: 0px !important; }'+ | |
'.home #rightCol, .home #pagelet_sidebar { display: none !important; }'+ | |
'.home #contentArea { width: 611px !important; }'+ | |
'html.sidebarMode .home #contentArea { width: 800px !important; }'+ | |
'.home * { border-radius: 0 !important; border: 0!important; transition: opacity 0.5s ease-in-out; }'+ | |
'._5p3y ._5pbw, ._5p3y ._5pbx, ._5p3y ._5pbx span.text_exposed_link { font-family: sans-serif !important; font-size: 17px !important; font-weight: 400; font-style: normal !important; }'+ | |
'._4f7n, ._4f7n:after, ._4jy0 { background-image: none!important; }'+ | |
'div._fmc { opacity: 0.5; }'+ | |
'#leftCol, #rightCol { opacity: 0.0; }'+ | |
'#leftCol:hover, #rightCol:hover, div._fmc:hover { opacity: 1.0 }'; | |
document.getElementsByTagName('head')[0].appendChild(style); | |
} | |
init(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment