Skip to content

Instantly share code, notes, and snippets.

@958
Created January 22, 2009 04:27
Show Gist options
  • Save 958/50418 to your computer and use it in GitHub Desktop.
Save 958/50418 to your computer and use it in GitHub Desktop.
[Sleipnir]ShareTwitterOnTumblr for SeaHorse
// ==UserScript==
// @name ShareTwitterOnTumblr for SeaHorse
// @description Share Twitter conversation on Tumblr as Chat
// @include http://twitter.com/*
// @include https://twitter.com/*
// @include http://explore.twitter.com/*
// @include http://m.twitter.com/*
// @include http://twitter.1x1.jp/search/*
// @include http://terraminds.com/twitter/*
// @type SleipnirScript
// ==/UserScript==
// modified for seahorse by 958
// base on http://white.s151.xrea.com/blog/2008-03-02-09-30.html
// this script require LDRize.user.js.
//
// How to use:
// 1. access http://twitter.com/home
// 2. type 'j' or 'k' to select status
// 3. type 'p' to pin
// 4. type 'r' access your Tumblr. status you pinned will be listed.
(function() {
if ((!_document) || (!_document.body) || (_window.__ShareTwitterOnTumblr) || (!_window.LDRize) || (typeof(_window.execScript) != 'object')) return;
_window.sleipnir = sleipnir;
try {
_window.execScript('('+function() {
var sleipnir = window.sleipnir;
window.sleipnir = null;
window.__ShareTwitterOnTumblr = true;
var KEY = 'u';
var $J = window.LDRize.jQuery;
function isTwitterStatusURL(aURL){
return new RegExp("^https?://(?:(?:explore|m)\\.)?twitter\\.com/[^/]+/status(?:es)?/\\d+").test(aURL);
}
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 parseStatus(doc, aURL){
function normalizeText(arr){
return $J.map(arr, function(a){return a.innerText.replace(/^\s+|\s+$/g,'')}).join(' ')
}
// normalize anchor for reply
var arr = $J('#permalink>div>p:first>a[href^="http://twitter.com/"]', doc).get();
$J.each(arr, function(i, node){
node.parentNode.replaceChild(
document.createTextNode(node.previousSibling.innerText +node.innerText),
node.previousSibling);
node.parentNode.removeChild(node);
});
// normalize external anchor
var arr = $J('#permalink>div>p:first>a[href^="http"]',doc).get();
$J.each(arr, function(i, node){
node.parentNode.replaceChild( document.createTextNode(node.getAttribute('href')),node);
});
// scrape status
// var body = normalizeText($J('div.desc-inner>p:first', doc).get());
var body = normalizeText($J('span.entry-content', doc).get());
var name = aURL.match('https?://(?:(?:explore|m)\\.)?twitter\\.com/([^/]+)')[1];
return encodeURIComponent(name + ":" + body + " [" + aURL + "]")
}
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, body){
var arr = [];
var param;
for(param in params){
if (!params.hasOwnProperty(param)) continue;
if(param != "preview_post"){
arr.push(encodeURIComponent(param));
arr.push("=");
arr.push((param == "post[two]") ? body.join("%0D%0A") : encodeURIComponent(params[param]))
arr.push("&");
}
}
return arr.join('');
}
function share(body, callback){
var url = "http://www.tumblr.com/new/chat";
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) {
callback(true);
},
onerror: function(res) {
callback(false);
},
data: createPostData(parseParams(convertToHTMLDocument(res.responseText)), body)
});
},
onerror: function(res) {
}
});
}
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);
}
/////////////////////////////////////////////////////////////////
// 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;
}
}
};
window.LDRize.addKeyBind(KEY, function() {
var args = this.args;
var urls = [];
if ((!window.LDRize) || (!window.LDRize.getCurrentParagraph()) || (Math.max(document.body.scrollTop, document.documentElement.scrollTop) == 0)) {
// command line is just 'share-twitter-on-tumblr'
urls = [window.location.href];
} else {
// command line is 'pinned-or-current-link | share-twitter-on-tumblr'
var pins = window.LDRize.getPinList();
if (pins.length == 0)
pins = [window.LDRize.getCurrentParagraph()];
$J.each(pins, function(i, cur) {
try {
var url = window.LDRize.getParagraphLink(cur);
if (url) urls.push(url);
}catch(e){}
});
}
if (urls.length == 0) {
Status.show('ShareTwitterOnTumblr'+time, 'error!', 1000);
return;
}
var time = new Date().getTime();
var urls = $J.map(urls, function(url) {
if (isTwitterStatusURL(url))
return url;
else
return null;
});
if(urls.length == 0) {
Status.show('ShareTwitterOnTumblr'+time, 'There is invalid URL', 1000);
return;
}
// older -> newer
urls.sort(function(a,b){
return a.match(/\d+$/)[0] - b.match(/\d+$/)[0];
});
Status.show('ShareTwitterOnTumblr'+time, 'Share ... 0%');
var urlIndex = 0;
var statuses = [];
(function() {
var f = arguments.callee;
aURL = urls[urlIndex++];
GM_xmlhttpRequest({
method: 'GET',
url: aURL,
onload: function(res) {
Status.show('ShareTwitterOnTumblr'+time, 'Share ... ' + Math.floor(urlIndex / (urls.length + 1) * 100) + '%');
statuses.push(parseStatus(convertToHTMLDocument(res.responseText), aURL));
if (urls.length == urlIndex)
share(statuses, function(result){
if (result) {
Status.show('ShareTwitterOnTumblr'+time, 'Share ... done', 500);
if (window.LDRize) window.LDRize.clearPin();
} else
Status.show('ShareTwitterOnTumblr'+time, 'Share ... failed', 500);
});
else
f();
},
onerror: function(res) {
Status.show('ShareTwitterOnTumblr'+time, 'Share ... failed', 500);
}
});
})();
});
}+')()');
}catch(e){}
_window.sleipnir = null;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment