Last active
December 24, 2015 07:48
-
-
Save Qofar/6765760 to your computer and use it in GitHub Desktop.
livedoor ReaderからGoogleBookmarksにiキーで一発登録する
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
// ==UserScript== | |
// @name LDR_googlebookmarks | |
// @description add to Google Bookmarks | |
// @include http://reader.livedoor.com/reader/* | |
// @grant unsafeWindow | |
// @grant GM_xmlhttpRequest | |
// @version 1.0.2 | |
// ==/UserScript== | |
// via http://la.ma.la/blog/diary_200605070200.htm | |
(function(w){ | |
var regex = /<smh:signature>(.+)<\/smh:signature>/; | |
var sig = ""; | |
function add_link(){ | |
var buttons = document.getElementById("control_buttons").getElementsByTagName("ul")[0]; | |
var li = document.createElement("li"); | |
li.className = "button icon"; | |
li.innerHTML = [ | |
'<a href="https://www.google.com/bookmarks/" target="_blank">', | |
'<img src="https://www.google.com/favicon.ico" border="none">', | |
'</a>' | |
].join(""); | |
buttons.appendChild(li) | |
} | |
add_link(); | |
document.addEventListener("keydown", function(e){ | |
if(e.keyCode === 73 && | |
!e.shiftKey && !e.ctrlKey && !e.altKey && !e.metaKey && | |
!/^input|^textarea/i.test(e.target.tagName)){ | |
add_to_googlebookmarks(); | |
} | |
},true); | |
// get sig | |
function get_Sig(){ | |
window.setTimeout(function() { | |
GM_xmlhttpRequest({ | |
method: "get", | |
url: "https://www.google.com/bookmarks/lookup?output=rss&num=10", | |
onload: function(res){ | |
if(res.status === 200 && | |
res.finalUrl.indexOf("https://www.google.com/bookmarks/") === 0 && | |
res.responseText.match(regex)){ | |
sig = new String(RegExp.$1); | |
}else{ | |
w.message("Google Bookmarks - not login..."); | |
} | |
} | |
}) | |
}, 0); | |
} | |
get_Sig(); | |
function make_label(){ | |
// folder | |
var folder = w.subs_item(w.State.now_reading).folder; | |
return ["LDR",folder].join(","); | |
} | |
function form_encode(param){ | |
var buf = []; | |
for(var key in param){ | |
var value = param[key]; | |
buf.push( | |
encodeURIComponent(key)+"="+ | |
encodeURIComponent(value) | |
) | |
} | |
return buf.join("&"); | |
} | |
function add_to_googlebookmarks(){ | |
var item = w.get_active_item(true); | |
if(item === null){ | |
return; | |
} | |
var postdata = form_encode({ | |
sig: sig, | |
bkmk: item.link, | |
title: item.title, | |
labels: make_label(), | |
prev: "/lookup" | |
}); | |
window.setTimeout(function(){ | |
GM_xmlhttpRequest({ | |
method: "post", | |
headers: {"Content-type": "application/x-www-form-urlencoded"}, | |
url: "https://www.google.com/bookmarks/mark", | |
data: postdata, | |
onload: function(res){ | |
if(res.status === 200 && | |
res.finalUrl.indexOf("https://www.google.com/bookmarks/") === 0 && | |
res.responseText.indexOf(item.link) > -1){ | |
w.message("Google Bookmarks - " + item.title); | |
}else{ | |
w.message("Google Bookmarks... Failed - please retry later."); | |
get_Sig(); | |
} | |
} | |
}) | |
}, 0); | |
} | |
})(unsafeWindow); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment