Created
February 21, 2011 07:02
-
-
Save eagletmt/836754 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
/* | |
* 現在のページ (http://$username.tumblr.com/post/$id のような) を reblog するコマンドを提供する Vimperator plugin | |
* _libly.js 必須 | |
* 全面的に http://coderepos.org/share/browser/lang/javascript/userscripts/reblogcommand.user.js を参考にした | |
*/ | |
(function() { | |
commands.addUserCommand(['reblog'], 'reblog this post', function() reblog(buffer.URI), { argCount: '0' }); | |
let libly = liberator.plugins.libly; | |
let $U = libly.$U; | |
function reblog(postUrl) { | |
function fail() liberator.echoerr('reblog failed: ' + postUrl); | |
let iframes = $U.getNodesFromXPath('//iframe[@src]').filter(function(e) /^http:\/\/.+\.tumblr\.com\//.test(e.src)); | |
if (iframes.length != 1) { | |
liberator.echoerr('not a tumblr page? ' + postUrl); | |
return; | |
} | |
let m = decodeURIComponent(iframes[0].src).match(/&pid=([^&]+)&rk=([^&]+)/); | |
if (!m) { | |
liberator.echoerr('not a tumblr page? ' + postUrl); | |
return; | |
} | |
let endpoint = 'http://www.tumblr.com/reblog/' + m[1] + '/' + m[2] + '?redirect_to=/dashboard'; | |
let req = new libly.Request(endpoint); | |
req.addEventListener('onSuccess', function(res) { | |
let params = []; | |
res.getHTMLDocument( | |
'id("edit_post")//input | id("edit_post")//textarea | id("edit_post")//select' | |
).forEach(function(e) { | |
if (e.name != 'preview_post' && e.name != 'send_to_twitter') { | |
params.push([e.name, e.value].map(encodeURIComponent).join('=')); | |
} | |
}); | |
let req = new libly.Request(endpoint, null, { postBody: params.join('&') }); | |
req.addEventListener('onSuccess', function() liberator.echo('reblogged ' + postUrl)); | |
req.addEventListener('onFailure', fail); | |
req.addEventListener('onException', fail); | |
req.post(); | |
}); | |
req.addEventListener('onFailure', fail); | |
req.addEventListener('onException', fail); | |
req.get(); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment