Skip to content

Instantly share code, notes, and snippets.

@rabel001
Last active May 13, 2018 16:37
Show Gist options
  • Save rabel001/07ece04dbba43f4475f88c3e685c7f47 to your computer and use it in GitHub Desktop.
Save rabel001/07ece04dbba43f4475f88c3e685c7f47 to your computer and use it in GitHub Desktop.
Open links contained in answers\responses on StackExchange, StackOverflow, AskUbuntu, and SuperUser in a new tab. Marks all edited links with a unicode checkbox followed by the link in red for visibility.
// ==UserScript==
// @name Add _blank to StackExchange\StackOverflow\Others
// @description Open StackExchange\StackOverflow\Other links in a new tab
// @namespace https://rawgit.com/rabel001/userscripts/master/superStackOverEx.js
// @updateURL https://rawgit.com/rabel001/userscripts/master/superStackOverEx.js
// @downloadURL https://rawgit.com/rabel001/userscripts/master/superStackOverEx.js
// @version 1.00
// @author rabel001
// @include *superuser.com*
// @include *askubuntu.com*
// @include *stackoverflow.com*
// @include *stackexchange.com*
// @grant none
// ==/UserScript==
(() => {
'use strict';
var pages = [
"askubuntu.com",
"superuser.com",
"stackoverflow.com",
"stackexchange.com"
];
var contains = (iArray, iNeedle) => {
for(var i in iArray) {
if(iArray[i] == iNeedle) {
return true;
}
}
return false;
}
var setBlank = (elem, page) => {
//function setBlank(elem) {
elem.setAttribute('target', '_blank');
switch(page) {
case pages.stackoverflow:
try {
elem.style.fontSize = '15px';
elem.style.color = 'red';
var code = elem.getElementsByTagName('code').item(0);
var nStyle = 'font-size: 15px; font-family: Arial,"Helvetica Neue",Helvetica,sans-serif; line-height: 1.26666667;';
var newstyle = (code.getAttribute('style') == null) ? nStyle : code.getAttribute('style') + '' + nStyle;
code.setAttribute('style', newstyle);
} catch(e) {}
break;
}
};
var ErrorArray = [];
var setBallotbox = (elem, page) => {
var span = document.createElement('span');
span.innerHTML = "☑ "; //"☑ "; //"✔ "; //"▣ ";
span.id = "ascii-ballotbox";
span.style.color = "red";
span.style.fontSize = 'large';
try {
elem.insertAdjacentElement('beforebegin', span);
} catch(err) {
elem[0].insertAdjacentElement('beforebegin', span);
ErrorArray.push(elem);
}
};
var recursiveElements = (elem, page) => {
if(elem.tagName == "A") {
setBlank(elem, page);
setBallotbox(elem, page);
} else {
var innerArr = [].slice.call(elem.children);
innerArr.forEach(function(child) {
recursiveElements(child, page);
});
}
};
var setBlanks = () => {
var posttext = document.querySelectorAll('.post-text');
posttext.forEach(function(elem) {
var ahrefs = elem.getElementsByTagName('a');
var count = ahrefs.length;
switch(true) {
case(count === 1):
setBlank(ahrefs.item(0), pages.stackoverflow);
setBallotbox(ahrefs, pages.stackoverflow);
break;
case(count > 1):
Array.prototype.slice.call(ahrefs).forEach(function(elem2) {
recursiveElements(elem2, pages.stackoverflow);
});
break;
default:
break;
}
});
};
var splitHostName = location.hostname.split('.');
var host = splitHostName[splitHostName.length - 2];
var root = splitHostName[splitHostName.length - 1];
var hostname = `${host}.${root}`;
if(contains(pages, hostname)) {
setBlanks();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment