Created
January 22, 2009 04:26
-
-
Save 958/50417 to your computer and use it in GitHub Desktop.
[Sleipnir]ReblogCommand for SeaHorse
This file contains 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 ReblogCommand for SeaHorse | |
// @include http://*.tumblr.com/* | |
// @include http://fastladder.com/reader/ | |
// @include http://reader.livedoor.com/reader/ | |
// @exclude http://www.tumblr.com/share | |
// @type SleipnirScript | |
// ==/UserScript== | |
// modified for seahorse by 958 | |
// base on http://white.s151.xrea.com/blog/2008-02-29-11-50.html | |
(function(){ | |
if ((!_document) || (!_document.body) || (_window.__ReblogCommand) || (!_window.LDRize) || (typeof(_window.execScript) != 'object')) return; | |
_window.sleipnir = sleipnir; | |
try { | |
_window.execScript('('+function() { | |
var sleipnir = window.sleipnir; | |
window.sleipnir = null; | |
window.__ReblogCommand = true; | |
var ALLOW_OWN_DOMAIN = false; | |
var KEY = 'u'; | |
var KEY_MANUAL = 'S-u'; | |
var $J = window.LDRize.jQuery; | |
// ---------------------------------------------------------------------------- | |
// Reblog | |
// ---------------------------------------------------------------------------- | |
function isTumblrDashboardURL(url) { | |
return url.match("^http://www\\.tumblr\\.com/dashboard.*$") ? true : false; | |
} | |
function isTumblrUserURL(url) { | |
return url.match("^https?://[^.]+\\.tumblr\\.com/post/(\\d+)/?.*$") || | |
// tumblr allow to use own domain. but this is risky. | |
(ALLOW_OWN_DOMAIN && url.match("^https?://[^/]+/post/(\\d+)/?.*$")) ? true : false; | |
} | |
function isFdrOrLdrURL(url) { | |
return (url == 'http://fastladder.com/reader/' || | |
url == 'http://reader.livedoor.com/reader/') ? true : false; | |
} | |
function getIDByPermalink(url) { | |
if (isTumblrUserURL(url)) { | |
return RegExp.$1; | |
} else { | |
// return what ? | |
return false; | |
} | |
} | |
function getURLByID(id, token) { | |
if (token) return "http://www.tumblr.com/reblog/" + id + "/" + token + "?redirect_to=/dashboard"; | |
return "http://www.tumblr.com/reblog/" + id; | |
} | |
// copy from tombloo | |
function unescapeHTML(s) { | |
return s.replace(/"/g, '"') | |
.replace(/</g, '<') | |
.replace(/>/g, '>') | |
.replace(/&/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 GM_xmlhttpRequest(opt) { | |
var http = null; | |
if (opt.redirect) | |
http = sleipnir.CreateObject('MSXML2.ServerXMLHTTP'); | |
else | |
http = sleipnir.CreateObject('Microsoft.XMLHTTP'); | |
http.open(opt.method.toUpperCase(), opt.url, true); | |
if (opt.headers) { | |
for (var i in opt.headers) { | |
if (!opt.headers.hasOwnProperty(i)) continue; | |
http.setRequestHeader(i, opt.headers[i]); | |
} | |
} | |
http.onreadystatechange = function() { | |
if(http.readyState == 4){ | |
if(http.status == 200){//succeed | |
if (opt.onload) opt.onload(http); | |
} else { | |
if (opt.onerror) opt.onerror(http); | |
} | |
http = null; | |
} | |
}; | |
var send = null; | |
if (opt.data) send = opt.data; | |
http.send(send); | |
} | |
function convertToHTMLDocument(html) { | |
html=html.replace(/(<iframe\s+[^>]*?)#[^>\s"']*([>\s"'])/mgi,'$1$2'); | |
var htmlDoc=sleipnir.CreateObject('htmlfile'); | |
htmlDoc.designMode = 'on'; | |
htmlDoc.open('text/html'); | |
htmlDoc.write(html); | |
htmlDoc.close(); | |
return htmlDoc; | |
} | |
function parseParams(doc) { | |
var elms = $J('#edit_post input, #edit_post textarea, #edit_post select', doc).get(); | |
var params = {}; | |
$J.each(elms, function(i, elm) { | |
params[elm.name] = elm.value; | |
}); | |
return params; | |
} | |
function createPostData(params) { | |
var arr = []; | |
for(var param in params) { | |
if (!params.hasOwnProperty(param)) continue; | |
if(param != "preview_post") { | |
arr.push(encodeURIComponent(param)); | |
arr.push("="); | |
arr.push(encodeURIComponent(params[param])); | |
arr.push("&"); | |
} | |
} | |
return arr.join(''); | |
} | |
function reblog(aURL, callback) { | |
var id = getIDByPermalink(aURL); | |
Status.show('ReblogCommand'+id, 'Reblog ...'); | |
var url = aURL; | |
GM_xmlhttpRequest({ | |
method: 'GET', | |
url: url, | |
onload: function(res) { | |
var token = getReblogToken(res.responseText.match('iframe src="((?:\\"|[^"])*)"')[1]); | |
url = getURLByID(token.id, token.token); | |
GM_xmlhttpRequest({ | |
method: 'GET', | |
url: url, | |
onload: function(res) { | |
GM_xmlhttpRequest({ | |
method: 'POST', | |
url: url, | |
headers:{"Content-Type":"application/x-www-form-urlencoded"}, | |
onload: function(res) { | |
Status.show('ReblogCommand'+id, 'Reblog ... done.', 500); | |
callback(); | |
}, | |
onerror: function(res) { | |
if (confirm('reblog manually ? \n' + url)) reblogManually(url); | |
callback(); | |
}, | |
data: createPostData(parseParams(convertToHTMLDocument(res.responseText))) | |
}); | |
}, | |
onerror: function(res) { | |
Status.show('ReblogCommand'+id, 'Reblog ... failed.', 500); | |
} | |
}); | |
}, | |
onerror: function(res) { | |
Status.show('ReblogCommand'+id, 'Reblog ... failed.', 500); | |
} | |
}); | |
} | |
function reblogManually(aURL) { | |
var url = aURL; | |
GM_xmlhttpRequest({ | |
method: 'GET', | |
url: url, | |
onload: function(res) { | |
var token = getReblogToken(res.responseText.match('iframe src="((?:\\"|[^"])*)"')[1]); | |
url = getURLByID(token.id, token.token); | |
sleipnir.API.NewWindow(url, false); | |
}, | |
onerror: function(res) { | |
} | |
}); | |
} | |
function spClearPins(url) { | |
unsafeWindow.pin.remove(url); | |
} | |
///////////////////////////////////////////////////////////////// | |
// Status | |
var Status = { | |
msgTmr: null, | |
msg: null, | |
_init: false, | |
list: {}, | |
shown: false, | |
init: function() { | |
var self = Status; | |
var _d = document; | |
self._init = true; | |
self.documentBody = (_d.compatMode == "CSS1Compat") ? _d.documentElement : _d.body; | |
self.msg = _d.createElement('div'); | |
self.msg.style.cssText = [ | |
'width:auto; height:auto; color: white; background-color: black; background-image:none; cursor:normal;', | |
'filter:Alpha(opacity=70); font-size: 150%; font-weight:bold; font-family:sans-serif;', | |
'padding:10px 0; margin:0; clear:both; line-height:1.4em; text-indent:0; text-align:center; text-decoration:none; text-spacing:normal; text-transform:none;', | |
'display:none; position:absolute; z-index: 999;' | |
].join(''); | |
_d.body.appendChild(self.msg); | |
window.attachEvent('onscroll', self.attach); | |
window.attachEvent('onresize', self.attach); | |
}, | |
show: function(title, msg, time) { | |
var self = Status; | |
if (self._init == false) | |
self.init(); | |
if ((self.list[title]) && (self.list[title].timer)) clearTimeout(self.list[title].timer); | |
self.list[title] = { | |
'msg': msg, | |
'timer': null | |
}; | |
self.redraw(); | |
if (time) | |
self.list[title].timer = setTimeout(function() { | |
self.list[title].msg = null; | |
self.list[title].time = null; | |
self.redraw(); | |
}, time); | |
}, | |
redraw: function() { | |
var self = Status; | |
var msg = []; | |
for (var i in self.list) { | |
if (!self.list.hasOwnProperty(i)) continue; | |
if (self.list[i].msg) | |
msg.push(self.list[i].msg); | |
} | |
if (msg.length > 0) { | |
self.msg.innerText = msg.join('\n'); | |
self.shown = true; | |
self.attach(); | |
self.msg.style.display = 'block'; | |
} else { | |
self.shown = false; | |
self.msg.style.display = 'none'; | |
} | |
}, | |
attach: function() { | |
var self = Status; | |
if (self.shown) { | |
self.msg.style.left = self.documentBody.scrollLeft + 'px'; | |
self.msg.style.width = self.documentBody.clientWidth + 'px'; | |
// self.msg.style.top = self.documentBody.scrollTop + self.documentBody.clientHeight - 100; | |
self.msg.style.bottom = -self.documentBody.scrollTop; | |
} | |
} | |
}; | |
///////////////////////////////////////////////////////////////// | |
// Shortcut Key | |
var ShortcutKey = { | |
repKey: { | |
'8': 'BS','9': 'TAB','13': 'Enter','27': 'ESC', | |
'33': 'PageUp','34': 'PageDown', | |
'35': 'End','36': 'Home', | |
'37': 'Left','38': 'Up','39': 'Right','40': 'Down', | |
'45': 'Insert','46': 'Delete', | |
'188': ',', '189': '-', '190': '.','191': '/', '192': '@', | |
'219': '[', '220': '\\', '221': ']', '222': '^', '226': '\\' | |
}, | |
deepKey: ['ESC', 'TAB', 'Home', 'End', 'Insert', 'Delete', 'C-.+', 'A-.+'].join('|'), | |
_deepKey: null, | |
keyEvent: [], | |
_init: false, | |
init: function() { | |
document.attachEvent('onkeydown', ShortcutKey.keyHandle); | |
ShortcutKey._init = true; | |
}, | |
keyHandle: function() { | |
var event = window.event; | |
var key = ''; | |
var rep = false; | |
for (var i in ShortcutKey.repKey) { | |
if (i == event.keyCode) { | |
key = ShortcutKey.repKey[i]; | |
rep = true; | |
break; | |
} | |
} | |
if (key == '') | |
key = String.fromCharCode(event.keyCode).toLowerCase(); | |
key = (event.shiftKey) ? "S-" + key : key; | |
key = (event.ctrlKey) ? "C-" + key : key; | |
key = (event.altKey || event.metaKey) ? "A-" + key : key; | |
if (!ShortcutKey._deepKey) | |
ShortcutKey._deepKey = new RegExp(ShortcutKey.deepKey); | |
var isDeep = ShortcutKey._deepKey.test(key); | |
if ((document.activeElement.tagName.match(/input|textarea/i)) && (!isDeep)) return; | |
var func = ShortcutKey.keyEvent[key]; | |
if(func){ | |
func(); | |
event.cancelBubble = true; | |
event.returnValue = false; | |
} | |
}, | |
add: function(key, func) { | |
if (!ShortcutKey._init) ShortcutKey.init(); | |
ShortcutKey.keyEvent[key] = func; | |
} | |
}; | |
// ---------------------------------------------------------------------------- | |
// Command | |
// ---------------------------------------------------------------------------- | |
var reblogCommand = function(mode){ | |
var urls = []; | |
if ((!window.LDRize) || (!window.LDRize.getCurrentParagraph())) { | |
urls = [window.location.href]; | |
} else { | |
var pins = window.LDRize.getPinList(); | |
if (pins.length == 0) { | |
var link = window.LDRize.getParagraphLink(window.LDRize.getCurrentParagraph()); | |
if (link) urls = [link]; | |
} | |
$J.each(pins, function(i, cur) { | |
try { | |
var url = window.LDRize.getParagraphLink(cur); | |
if (url) urls.push(url); | |
}catch(e){} | |
}); | |
} | |
// reblog | |
if (mode) { | |
$J.each(urls, function(i, v){ | |
reblogManually(v); | |
}); | |
} else { | |
urls = $J.grep(urls, function(url, i) { | |
return isTumblrUserURL(url); | |
}); | |
if (!urls.length) return; | |
var urlIndex = 0; | |
(function() { | |
var f = arguments.callee; | |
reblog(urls[urlIndex], function(){ | |
urlIndex++; | |
if (urlIndex == urls.length) { | |
if (urls.length > 1) | |
Status.show('ReblogCommand','Everything is OK', 1000); | |
if (window.LDRize) window.LDRize.clearPin(); | |
} else | |
f(urls[urlIndex]); | |
}); | |
})(); | |
} | |
}; | |
ShortcutKey.add(KEY, function() { | |
reblogCommand(false); | |
}); | |
ShortcutKey.add(KEY_MANUAL, function() { | |
reblogCommand(true); | |
}); | |
}+')()'); | |
}catch(e){} | |
_window.sleipnir = null; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment