Skip to content

Instantly share code, notes, and snippets.

@saitamanodoruji
Last active February 13, 2016 13:09
Show Gist options
  • Save saitamanodoruji/4641203 to your computer and use it in GitHub Desktop.
Save saitamanodoruji/4641203 to your computer and use it in GitHub Desktop.
FastReblogCommand
// ==UserScript==
// @id FastReblogCommand
// @name FastReblogCommand
// @version 1.2.11
// @namespace http://saitamanodoruji.tumblr.com/
// @author saitamanodoruji
// @description add Minibuffer shortcut key 't' for fast_reblog, 'q' for queue
// @include *
// @exclude https://twitter.com/*
// @grant GM_openInTab
// @run-at document-end
// @updateURL https://raw.github.com/gist/4641203
// @update 2016-02-13
// ==/UserScript==
(function() {
const FAST_REBLOG_URL = 'http://www.tumblr.com/fast_reblog'
const DASHBOARD_URL = 'http://www.tumblr.com/dashboard'
const ALLOW_OWN_DOMAIN = true
// ----------------------------------------------------------------------------
// Reblog
// ----------------------------------------------------------------------------
var boot = function() {
with (sharedObject.Minibuffer) {
function click(node, altKey) {
var e = document.createEvent('MouseEvents')
e.initMouseEvent('click',1,1,window,1,0,0,0,0,0,altKey,0,0,0,null)
node.dispatchEvent(e)
}
function isTumblrDashboardURL(url) {
return url.match(/^https?:\/\/www\.tumblr\.com\/(?:dashboard|blog|show|likes|liked\/by|tagged)/) ? true : false
}
function isTumblrUserURL(url) {
return url.match(/^https?:\/\/[^.]+\.tumblr\.com\/post\/(\d+)/) ||
// tumblr allow to use own domain. but this is risky.
// $X('id("tumblr_controls")[self::iframe]', Boolean)
(ALLOW_OWN_DOMAIN && url.match(/^https?:\/\/[^\/]+\/post\/(\d+)/)) ? true : false
}
function isLdrOrFldrURL(url) {
return url.indexOf('http://reader.livedoor.com/reader/') == 0 ||
url.indexOf('http://reader.livedoor.com/public/') == 0 ||
url.indexOf('http://fastladder.com/reader/') == 0 ||
url.indexOf('http://fastladder.com/public/') == 0
}
// copy from tombloo
function unescapeHTML(s) {
return s.replace(/"/g, '"')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>')
.replace(/&amp;/g, '&')
}
// copy from tombloo
function getReblogToken(url) {
url = unescapeHTML(url)
if (/&pid=([^&]*)&rk=([^&"]*)/.test(url) || /\/reblog\/([^\/]+)\/([^?]*)/.test(url))
return {
id : RegExp.$1,
token : RegExp.$2
}
}
function createToken(deferred, permalinkDocument) {
var tumblr_ctrls = permalinkDocument.querySelector('#tumblr_controls'),
tmblr_iframe = permalinkDocument.querySelector('iframe.tmblr-iframe')
if (tumblr_ctrls) {
return deferred.next(function() {
return getReblogToken(tumblr_ctrls.src)
})
} else if (tmblr_iframe) {
var ctd = deferred.xhttp.get(tmblr_iframe.src).
next(function(res) {
var re = /&quot;reblogUrl&quot;:.+reblog\\\/([^\\]*)\\\/([^&]*)/
if (re.test(res.responseText)) {
return {
id : RegExp.$1,
token : RegExp.$2
}
} else {
throw "createToken() could not find reblog key"
}
}).
error(function(e) {
var message = e.message || e
if (confirm(message + '\nfailed to get reblog key.\nopen the post page ? \n' + url))
GM_openInTab(url, true)
})
return ctd
} else {
throw "createToken() could not find reblog button"
}
}
function createReblogURLFromToken(token) {
return 'http://www.tumblr.com/reblog/' + token.id + '/' + token.token
}
function createPostData(params) {
var arr = []
for (var param in params) {
arr.push(encodeURIComponent(param))
arr.push('=')
arr.push(encodeURIComponent(params[param]))
arr.push('&')
}
return arr.join('')
}
function getTokenFromPermalink(deferred, url) {
var gtfpd = deferred.xhttp.get(url).
next(function(res) {
var doc = createDocumentFromString(res.responseText)
return createToken(deferred, doc)
}).
error(function(e) {
var message = e.message || e
if (confirm(message + '\nfailed to get reblog key.\nopen the post page ? \n' + url))
GM_openInTab(url, true)
})
return gtfpd
}
function getTokens(stdin, deferred, args, manually) {
var getTokensd
if (isTumblrDashboardURL(window.location.href)
&& stdin.every(function(s) { return s.nodeName == 'LI' })) {
getTokensd = deferred.next(function() {
var tokens = {}
stdin.forEach(function(s, i) {
var s = s.querySelector('div[id^="post_"]'); // 2013-08-22
tokens[i] = {
token: s.getAttribute('data-reblog-key'),
id: s.getAttribute('data-post-id')
}
if (!manually) {
var mes = args.filter(function(a) { return a == '-q' }).length ? 'Queue ...' : 'Reblog ...'
status('FastReblogCommand' + tokens[i].id, mes)
}
if (!tokens[i] || !tokens[i].token || !tokens[i].id) {
var u = s.querySelector('.post_permalink').href
if (u && confirm('getTokens() failed.\nopen the post ? \n' + u)) {
GM_openInTab(u, true)
}
}
})
return tokens
})
} else if (isTumblrUserURL(window.location.href) && !stdin.length) {
var post_id = window.location.href.match(/\/post\/(\d+)/)[1]
if (!manually) {
var mes = args.filter(function(a) { return a == '-q' }).length ? 'Queue ...' : 'Reblog ...'
status('FastReblogCommand' + post_id, mes)
}
getTokensd = deferred.parallel([document].map(function(doc) {
return createToken(deferred, doc)
}))
} else {
var urls = []
if (isLdrOrFldrURL(window.location.href)) {
var tmp = []
stdin.forEach(function(url, index) { tmp[index] = url })
urls = tmp.filter(isTumblrUserURL)
if (args.length > 0 && args.filter(function(a) { return a == '-c' }).length) {
urls.forEach(spClearPins)
}
} else {
if (stdin.every(function(s) { return s.nodeName == 'A' } )) {
urls = stdin.map(function(s) { return s.href })
} else if (stdin.every(function(s) { return typeof s == 'string' })) {
urls = stdin
}
}
urls = urls.filter(isTumblrUserURL)
urls.forEach(function(url) {
if (!manually) {
var mes = args.filter(function(a) { return a == '-q' }).length ? 'Queue ...' : 'Reblog ...'
status('FastReblogCommand' + url.match(/\/post\/(\d+)/)[1], mes)
}
})
getTokensd = deferred.parallel(urls.map(function(u) {
return getTokenFromPermalink(deferred, u)
}))
}
return getTokensd
}
function getFormKey(deferred, tokens) {
var getFormKeyd
if(isTumblrDashboardURL(window.location.href)) {
getFormKeyd = deferred.next(function() {
return document.getElementById('tumblr_form_key').getAttribute('content')
})
} else {
getFormKeyd = deferred.next(function() {
return deferred.xhttp.get(DASHBOARD_URL)
}).
next(function(res) {
return res.responseText.match(/<meta name="tumblr-form-key" content="([^"]+)"/)[1]
}).
error(function(e) {
var message = e.message || e
reblog_urls = tokens.map(createReblogURLFromToken)
if (confirm(message + '\nfailed to get form_key.\nreblog ' + tokens.length + ' post(s) manually ? \n' + reblog_urls.join('\n'))) {
reblog_urls.forEach(function(url) { GM_openInTab(url, true) })
}
})
}
return getFormKeyd
}
function fast_reblog(token, form_key, queue) {
var d = D(),
fastreblogd = d.next(function() {
var data = { reblog_key: token.token,
reblog_post_id: token.id,
form_key: form_key,
queue: queue || false }
return d.xhttp.post(FAST_REBLOG_URL, createPostData(data))
}).
next(function(res) {
var mes = queue ? 'Queue ... done.' : 'Reblog ... done.'
status('FastReblogCommand' + token.id, mes, 100)
}).
error(function(e) {
var message = e.message || e
var reblog_url = createReblogURLFromToken(token)
if (confirm(message + '\nfast_reblog failed.\nreblog manually ? \n' + reblog_url))
GM_openInTab(reblog_url, true)
})
return fastreblogd
}
function spClearPins(url) {
unsafeWindow.pin.remove(url)
}
// ----------------------------------------------------------------------------
// Command
// ----------------------------------------------------------------------------
function getTargetCommand() {
var target_cmd = ''
if (isTumblrDashboardURL(window.location.href)) {
target_cmd = 'pinned-or-current-node'
} else if (isTumblrUserURL(window.location.href)) {
target_cmd = ''
} else {
target_cmd = 'pinned-or-current-link'
}
return target_cmd
}
addShortcutkey({
key: 't',
description: 'tumblr.fast_reblog',
command: function() {
var target_cmd = getTargetCommand(),
clear_pin = ''
if (target_cmd != '') target_cmd = target_cmd + ' | '
if (isLdrOrFldrURL(window.location.href)) {
if (!execute('pinned-or-current-link').filter(isTumblrUserURL).length) {
target_cmd = 'current-link'
clear_pin = ''
} else {
clear_pin = '-c'
}
} else {
clear_pin = (/^pinned-or-current-(?:node|link) | $/.test(target_cmd)) ? ' | clear-pin' : ''
}
execute(target_cmd + 'tumblr.fast_reblog ' + clear_pin)
}})
addShortcutkey({
key: 'q',
description: 'tumblr.fast_reblog (queue)',
command: function() {
var target_cmd = getTargetCommand(),
clear_pin = ''
if (target_cmd != '') target_cmd = target_cmd + ' | '
if (isLdrOrFldrURL(window.location.href)) {
if (!execute('pinned-or-current-link').filter(isTumblrUserURL).length) {
target_cmd = 'current-link'
clear_pin = ''
} else {
clear_pin = '-c'
}
} else {
clear_pin = (/^pinned-or-current-(?:node|link) | $/.test(target_cmd)) ? ' | clear-pin' : ''
}
execute(target_cmd + 'tumblr.fast_reblog -q ' + clear_pin)
}})
addCommand({
name: 'tumblr.fast_reblog',
command: function(stdin) {
var tokens = [], form_key, args = this.args, deferred = D(), reblogd
if (args.filter(function(a) { return a == '-m' }).length) {
execute('tumblr.reblog_manually', stdin)
return stdin
}
reblogd = deferred.next(function() {
return getTokens(stdin, deferred, args)
}).
next(function(values) {
for (var i in values) { tokens[i] = values[i] }
if (tokens.length > 1) status('FastReblogCommand','Everything is OK', 1000)
return getFormKey(deferred, tokens)
}).
next(function(form_key) {
return deferred.parallel(tokens.map(function(token) {
var queue = args.filter(function(a) { return a == '-q' }).length ? true : false
return fast_reblog(token, form_key, queue)
}))
}).
error(function(e) {
var message = [e.message || e]
if (typeof e != 'string') {
for ( var n in e ) { message.push(n + ': ' + e[n]) }
}
alert('fastreblogcommand (line 327) : ' + message.join('\n'))
})
return stdin
}})
addShortcutkey({
key: 'T',
description: 'tumblr.reblog_manually',
command: function() {
var target_cmd = getTargetCommand(),
clear_pin = ''
if (target_cmd != '') target_cmd = target_cmd + ' | '
execute(target_cmd + 'tumblr.reblog_manually | clear-pin')
}})
addCommand({
name: 'tumblr.reblog_manually',
command: function(stdin) {
if (isTumblrDashboardURL(window.location.href) && stdin.length == 1
&& stdin[0].nodeName == 'LI' ) {
stdin.forEach(function(s){
var reblog_button = s.querySelector('.post_control.reblog')
if (reblog_button) click(reblog_button)
})
} else {
var tokens = [], args = this.args
deferred = D()
deferred.next(function() {
return getTokens(stdin, deferred, args, true)
}).
next(function(values) {
for (var i in values) { tokens[i] = values[i] }
return deferred.parallel(tokens.map(function(token) {
GM_openInTab('http://www.tumblr.com/reblog/' + token.id + '/' + token.token, true)
}))
})
}
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