Skip to content

Instantly share code, notes, and snippets.

@shokai
Created September 4, 2009 17:39
Show Gist options
  • Save shokai/181001 to your computer and use it in GitHub Desktop.
Save shokai/181001 to your computer and use it in GitHub Desktop.
Fav on LDR
// ==UserScript==
// @name Favon LDR
// @namespace http://hoge.sub.jp/blog/
// @description add to twitter's favorites on LDR
// @include http://reader.livedoor.com/reader/*
// @include http://fastladder.com/reader/*
// @resource star http://assets0.twitter.com/images/icon_star_full.gif
// @version 0.0.3
// ==/UserScript==
GM_addStyle(<><![CDATA[
.favon_ldr_complete {background-color : #F5EBE1 !important;}
]]></>);
(function(){
//keymap
const KEY = 'e';
const ICON_STAR = GM_getResourceURL('star');
//domain_name to id_regex
function get_id_regex(domain){
if(domain == 'twitter.com' || //twitter and kichikutter
domain == 'kichiku.oq.la' ||
domain == 'pcod.no-ip.org' ||
domain == 'search.twitter.com' ||
domain == 'pipes.yahoo.com'
) {
return /.*\/([0-9]+)/;
} else if (domain == 'favotter.net' || //favotter and twitter_search
domain == 'twitter.1x1.jp'){
return /.*?id=(.*)/;
} else {
return false;
}
}
var w = unsafeWindow;
var _onload = w.onload;
var onload = function(){
w.Keybind.add(KEY, function(){
var item = w.get_active_item(true);
var feed = w.get_active_feed();
var domain = feed.channel.feedlink.replace( /^http:\/\/([^\/]+).*/ig , "$1" );
var id_regex = get_id_regex(domain);
//do nothing
if(!id_regex){
w.message('Zzz...');
return;
}
w.updater('item_body_' + item.id, function(){
this.innerHTML = '<div class="favon_ldr_complete">' + item.body + '&nbsp;&nbsp;'
+ '<img src="'+ICON_STAR+'">' + '</div>';
});
var id = item.link.replace(id_regex, "$1");
var request_opt = {
method: 'POST',
url: 'http://twitter.com/favorites/create/' + id + '.xml',
headers: {
'Content-Type' : 'application/x-www-form-urlencoded',
'Content-Length' : 0
},
onload: function(res){
//error check
if(res.status == 200 && res.statusText == 'OK'){
w.message('[status:'+ id +'] you add favorites');
w.update('item_body_' + item.id);
} else {
w.message('fail... please retry later.');
}
},
onerror: function(res){
w.message('fail... please retry later.');
}
}
w.setTimeout(function() {
GM_xmlhttpRequest(request_opt);
}, 0);
});
}
w.onload = function(){
_onload();
onload();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment