Last active
September 25, 2015 11:18
-
-
Save saitamanodoruji/913101 to your computer and use it in GitHub Desktop.
Open Links in Caption on Tumblr Dashboard
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 Open Links in Caption on Tumblr Dashboard | |
// @namespace http://saitamanodoruji.tumblr.com/ | |
// @description Press Shift + o , and links in caption will open in new tabs. This works on Quote, Photo, Link and Video post. | |
// @include http://www.tumblr.com/dashboard* | |
// @include http://www.tumblr.com/show* | |
// @include http://www.tumblr.com/blog* | |
// @include http://www.tumblr.com/tagged* | |
// @include http://www.tumblr.com/likes* | |
// @include http://www.tumblr.com/liked/by/* | |
// @include https://www.tumblr.com/dashboard* | |
// @include https://www.tumblr.com/show* | |
// @include https://www.tumblr.com/blog* | |
// @include https://www.tumblr.com/tagged* | |
// @include https://www.tumblr.com/likes* | |
// @include https://www.tumblr.com/liked/by/* | |
// @version 1.8.0 | |
// @update 2015-01-11 | |
// ==/UserScript== | |
// | |
// This script is dependent on | |
// -Minibuffer http://userscripts.org/scripts/show/11760 | |
// -LDRize http://userscripts.org/scripts/show/11562 | |
// | |
(function() { | |
const REGEX_URL_TUMBLELOG = [ | |
/^http:\/\/[^.]+\.tumblr\.com\//, | |
/^http:\/\/plasticdrea\.ms\//, | |
] | |
var isTumblelogURL = function(url) { | |
return REGEX_URL_TUMBLELOG.some(function(re) { | |
return re.test(url) | |
}) | |
} | |
var boot = function() { | |
with (sharedObject.Minibuffer) { | |
addShortcutkey({ | |
key: 'O', | |
description: 'openLinksInCaption', | |
command: function() { | |
try { | |
execute('openLinksInCaption', execute('current-node')) | |
} catch (e) { console.log(e) } | |
} | |
}) | |
addCommand({ | |
name: 'openLinksInCaption', | |
command: function(stdin) { | |
try { | |
if (!stdin.length) stdin = execute('current-node') | |
var c = $X('./descendant-or-self::div[starts-with(@id, "post_")]', stdin[0])[0].getAttribute('data-type'); | |
if ( c.match('quote') ) { | |
var url = $X( | |
'.//td[contains(concat(" ", @class, " "), " quote_source ")]//a' + | |
'|.//a[contains(concat(" ", @class, " "), " post_source_link ")]' | |
, stdin[0]) | |
} | |
if (c.match('photo') || c.match('video')) { | |
var url = $X( | |
'.//div[contains(concat(" ", @class, " "), " post_body ")]//a' + | |
'|.//a[contains(concat(" ", @class, " "), " post_source_link ")]' + | |
'|.//div[contains(concat(" ", @class, " "), " post_content ")]//div/a[img]' + | |
'|.//div[contains(@id, "photo_info_")]/a' | |
, stdin[0]) | |
} | |
if (c.match('link')) { | |
var url = $X( | |
'.//a[contains(concat(" ", @class, " "), " link_title ")]' | |
, stdin[0]) | |
} | |
if (c.match('regular')) { | |
var url = $X( | |
'.//a[contains(concat(" ", @class, " "), " post_source_link ")]' | |
, stdin[0]) | |
} | |
if (c.match('audio')) { | |
var url = $X( | |
'.//a[contains(concat(" ", @class, " "), " post_source_link ")]' | |
, stdin[0]); | |
} | |
// remove duplicate | |
var i = 1, j, k | |
while ( i < url.length ) { | |
k = url.length | |
for ( j = 0; j < i; j++ ) { | |
if ( url[i].href == url[j].href ) { | |
url.splice(i,1) | |
break | |
} | |
} | |
if ( k == url.length ) i++ | |
} | |
url.forEach(function(u) { | |
if ( (!isTumblelogURL(u.href) || c.match(' link ')) | |
&& !/^(?:(?:(?!http).)*:\/\/|mailto:)/.test(u.href) ) { | |
GM_openInTab(u, true) | |
} | |
}) | |
return stdin | |
} catch (e) { console.log(e) } | |
return stdin | |
}, | |
}) | |
} | |
} | |
if ( sharedObject.Minibuffer ) { | |
boot() | |
} else { | |
window.addEventListener('GM_MinibufferLoaded', boot, false) | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment