Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@GrahamBlanshard
Last active June 1, 2018 19:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GrahamBlanshard/d7211436088e0159164a to your computer and use it in GitHub Desktop.
Save GrahamBlanshard/d7211436088e0159164a to your computer and use it in GitHub Desktop.
FacebookTrends
// ==UserScript==
// @name FacebookTrends
// @author Graham Blanshard
// @namespace https://www.pro-graham.com
// @description Get rid of Trending/Suggestions on Facebook
// @include *facebook.com/*
// @run-at document-idle
// @grant GM_xmlhttpRequest
// @grant GM_setValue
// @grant GM_getValue
// @version 1.7
// ==/UserScript==
//Fetch Trending section and test for in-page reloads
var trendDiv = document.getElementById('pagelet_trending_tags_and_topics');
var coworkerDiv = document.getElementById('pagelet_ego_pane');
var gamesDiv = document.getElementById('pagelet_games_rhc');
var liveStreams = document.getElementById('pagelet_live_videos_v2');
if (trendDiv == null) return;
if (coworkerDiv != null) {
var tempParent = coworkerDiv.parentNode;
tempParent.removeChild(coworkerDiv);
}
if (gamesDiv != null) {
var tempParent = gamesDiv.parentNode;
tempParent.removeChild(gamesDiv);
}
if (liveStreams != null) {
var tempParent = liveStreams.parentNode;
tempParent.removeChild(liveStreams);
}
//Grab parent, delete the child
var parentDiv = trendDiv.parentNode;
var fillDiv;
parentDiv.removeChild(trendDiv);
//Create user entry text + properties, link to document
var elmFloatingContent = document.createElement('div');
var inSub = document.createElement("input");
var subreddit = GM_getValue('sub','aww');
inSub.style.width = "95%";
inSub.id = "fbtFloaterInput";
inSub.value = subreddit;
inSub.addEventListener("change",function(){ GM_setValue('sub',document.getElementById('fbtFloaterInput').value)});
elmFloatingContent.style.position = "fixed";
elmFloatingContent.style.top = document.getElementById('pagelet_bluebar').scrollHeight;
elmFloatingContent.style.right = "0%";
elmFloatingContent.style.width = "5%";
elmFloatingContent.style.border = "1px solid";
elmFloatingContent.style.backgroundColor = "white";
elmFloatingContent.style.padding = "5px";
elmFloatingContent.id = "fbtFloater";
elmFloatingContent.appendChild(inSub);
document.getElementById('pagelet_bluebar').appendChild(elmFloatingContent);
//Query the API, load the new content
GM_xmlhttpRequest({
method: "GET",
url: "https://api.imgur.com/3/gallery/r/" + subreddit,
headers: {
'Authorization': 'Client-ID 475df88e1f76ce6',
},
onload: function(response) {
var json = JSON.parse(response.responseText);
var links = fetchImage(json);
var titles = fetchTitle(json);
var IDs = fetchIDs(json);
var imgurID = Math.floor(Math.random() * (links.length));
var directLink = 'https://imgur.com/gallery/' + IDs[imgurID]
var imgurImg = links[imgurID];
var imgurTitle = titles[imgurID];
var newDiv = document.createElement('div');
newDiv.id = "imgurLink";
newDiv.innerHTML = '<a href="'+ directLink + '">' + imgurTitle + '</a>';
parentDiv.appendChild(newDiv);
fillDiv = ReferrerKiller.imageNode(imgurImg,'');
trendDiv = parentDiv.appendChild(fillDiv);
},
onerror: function(reponse) {
console.log("error: ", reponse);
}
});
//For parsing JSON response down to the link array
function fetchImage(jData) {
var links = jData.data.map(function(item) {
return item.link;
})
return links;
}
//For parsing JSON response down to the title array
function fetchTitle(jData) {
var links = jData.data.map(function(item) {
return item.title;
})
return links;
}
//For parsing JSON response down to the ID array
function fetchIDs(jData) {
var links = jData.data.map(function(item) {
return item.id;
})
return links;
}
/**
* @Project: ReferrerKiller.
* @Licence: The MIT License.
* @Author: Juan Pablo Guereca.
* @Description: Crossbrowser referrer killing solution.
* It's a hack that prevents the browser of sending the http referrer in the following cases:
* - Link: You can have a link in your website being sure that the destiny won't know about your site.
* - Image: You can display an image from another site being sure the other site won't know your website is displaying it.
* Other interesting use is displaying an image without blocking the rest of the content, this way in case the image fails it allows the rest of the page to load normally.
*/
var ReferrerKiller = (function () {
var URL_REDIRECTION = "https://www.google.com/url?q=", // You can use another service if you use https protocol no referrer will be sent.
PUB = {},
IE_GT_8 = (function () {
/*- Detecting if it's IE greater than 8 -*/
var trident,
match = navigator.userAgent.match(/Trident\/(\d)+/);
if (null === match) {
return false;
}
trident = parseInt(match[1], 10);
if (isNaN(trident)) {
return false;
}
return trident > 4;
})();
/**
* Escapes double quotes in a string.
*
* @private
* @param {string} str
* @return {string}
*/
function escapeDoubleQuotes(str) {
return str.split('"').join('\\"');
}
/**
* Given a html string returns an html node.
*
* @private
* @param {string} html
* @return {Node}
*/
function htmlToNode(html) {
var container = document.createElement('div');
container.innerHTML = html;
return container.firstChild;
}
/**
* Converts object to html attributes string.
*
* @private
* @param {object} obj
* @return {string}
*/
function objectToHtmlAttributes(obj) {
var attributes = [],
value;
for (var name in obj) {
value = obj[name];
attributes.push(name + '="' + escapeDoubleQuotes(value) + '"');
}
return attributes.join(' ');
}
/**
* It applies the hack to kill the referrer to some html.
*
* @public
* @param {string} html.
* @param {object} [iframeAttributes]
* @return {string} html.
*/
function htmlString(html, iframeAttributes) {
var iframeAttributes = iframeAttributes || {},
defaultStyles = 'border:none; overflow:hidden; ',
id;
/*-- Setting default styles and letting the option to add more or overwrite them --*/
if ('style' in iframeAttributes) {
iframeAttributes.style = defaultStyles + iframeAttributes.style;
} else {
iframeAttributes.style = defaultStyles;
}
id = '__referrer_killer_' + (new Date).getTime() + Math.floor(Math.random()*9999);
/*-- Returning html with the hack wrapper --*/
return '<iframe \
style="border 1px solid #ff0000" \
scrolling="no" \
frameborder="no" \
allowtransparency="true" ' +
/*-- Adding style attribute --*/
objectToHtmlAttributes( iframeAttributes ) +
'id="' + id + '" ' +
' src="javascript:\'\
<!doctype html>\
<html>\
<head>\
<meta charset=\\\'utf-8\\\'>\
<style>*{margin:0;padding:0;border:0;}</style>\
</head>' +
/*-- Function to adapt iframe's size to content's size --*/
'<script>\
function resizeWindow() {\
var elems = document.getElementsByTagName(\\\'*\\\'),\
width = 0,\
height = 0,\
first = document.body.firstChild,\
elem;\
if (first.offsetHeight && first.offsetWidth) {\
width = first.offsetWidth;\
height = first.offsetHeight;\
} else {\
for (var i in elems) {\
elem = elems[i];\
if (!elem.offsetWidth) {\
continue;\
}\
width = Math.max(elem.offsetWidth, width);\
height = Math.max(elem.offsetHeight, height);\
}\
}\
var ifr = parent.document.getElementById(\\\'' + id + '\\\');\
ifr.height = height;\
ifr.width = width;\
}\
</script>' +
'<body onload=\\\'resizeWindow()\\\'>\' + decodeURIComponent(\'' +
/*-- Content --*/
encodeURIComponent(html) +
'\') +\'</body></html>\'"></iframe>';
}
/*-- Public interface --*/
/**
* It displays an image without sending the referrer.
*
* @public
* @param {String} url
* @param {Object} [imgAttributesParam]
* @return {String} html
*/
function imageHtml(url, imgAttributesParam) {
var imgAttributes = imgAttributesParam || {},
/*-- Making sure this styles are applyed in the image but let the possibility to overwrite them --*/
defaultStyles = 'border:none; margin: 0; padding: 0; width: 250px';
if ('style' in imgAttributes) {
imgAttributes.style = defaultStyles + imgAttributes.style;
} else {
imgAttributes.style = defaultStyles;
}
return htmlString('<img src="' + escapeDoubleQuotes(url) + '" ' + objectToHtmlAttributes(imgAttributes) + '/>');
}
PUB.imageHtml = imageHtml;
/**
* It displays an image without sending the referrer.
*
* @public
* @param {string} url
* @param {object} [imgParams]
* @return {Node}
*/
function imageNode(url, imgParams) {
return htmlToNode(imageHtml(url, imgParams));
}
PUB.imageNode = imageNode;
/*-- Exposing the module interface --*/
return PUB;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment