Last active
August 8, 2018 01:18
-
-
Save DanH42/182f4a3b95f2e390aec207135e219b51 to your computer and use it in GitHub Desktop.
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 Watch is the actual worst | |
// @namespace com.facebook.watchnt | |
// @version 1.0.3 | |
// @description Like actually the worst | |
// @author Dan Hlavenka | |
// @updateURL https://gist.githubusercontent.com/DanH42/182f4a3b95f2e390aec207135e219b51/raw | |
// @match https://www.facebook.com/* | |
// @grant none | |
// ==/UserScript== | |
(function(){ | |
var feedID = ""; | |
var checkFeedID = function(){ | |
var currentFeed = document.querySelector('[id^="feed_stream_"][role="region"]'); | |
if(!currentFeed) | |
currentFeed = document.querySelector('[id^="group_mall_"][role="region"]'); | |
if(currentFeed){ | |
var currentFeedID = currentFeed.id; | |
if(currentFeedID !== feedID){ | |
feedID = currentFeedID; | |
console.log("Feed ID: " + feedID); | |
updatePostObserver(); | |
} | |
}else | |
console.log("Couldn't find a feed on the page"); | |
}; | |
var globalContainer = document.getElementById('globalContainer'); | |
if(!globalContainer) return; | |
new MutationObserver(checkFeedID).observe(globalContainer, {childList: true, subtree: true}); | |
var observerDebounce = -1; | |
var postObserver = new MutationObserver(function(){ | |
if(observerDebounce !== -1) | |
clearTimeout(observerDebounce); | |
observerDebounce = setTimeout(function(){ | |
observerDebounce = -1; | |
checkPosts(); | |
}, 100); | |
}); | |
var updatePostObserver = function(){ | |
postObserver.disconnect(); | |
postObserver.observe(document.getElementById(feedID), {childList: true, subtree: true}); | |
var watchSuggestions = document.getElementById('pagelet_video_home_suggested_for_you_rhc'); | |
if(watchSuggestions) | |
watchSuggestions.style.display = "none"; | |
}; | |
var checkPosts = function(){ | |
var videoPosts = Array.from(document.querySelectorAll('._4mpt')).map(getPostParent); | |
var count = 0; | |
for(var post of videoPosts){ | |
var video = post.querySelector("._5r69 ._150c"); | |
if(video && video.getAttribute('data-hidden') !== "true"){ | |
count++; | |
addShowButton(video); | |
} | |
} | |
if(count) | |
console.log(count + " video" + (count === 1 ? "" : "s") + " hidden"); | |
}; | |
var addShowButton = function(el){ | |
el.style.display = "none"; | |
var showButton = document.createElement('a'); | |
showButton.href = "#"; | |
showButton.innerText = "Show video"; | |
Object.assign(showButton.style, { | |
display: "block", | |
textAlign: "center", | |
fontWeight: "bold", | |
padding: ".5em", | |
margin: "0 12px", | |
backgroundColor: "#eee", | |
border: "1px solid #ccc", | |
borderRadius: "2px" | |
}); | |
showButton.onclick = function(e){ | |
el.style.display = ""; | |
showButton.style.display = "none"; | |
return false; | |
}; | |
el.parentElement.prepend(showButton); | |
el.setAttribute('data-hidden', "true"); | |
}; | |
var getPostParent = function(el){ | |
if(el.getAttribute('role') === "article") | |
return el; | |
if(el.tagName === "HTML") | |
return; | |
return getPostParent(el.parentElement); | |
}; | |
checkFeedID(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment