Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GreasemonkeyでOAuth認証を手助けするライブラリとそのサンプル
// ==UserScript==
// @name OAuth for Greasemonkey sample
// @namespace http://efcl.info/
// @include http://*
// @require http://userscripts.org/scripts/source/78102.user.js
// ==/UserScript==
// http://userscripts.org/scripts/show/78102
/*認証手順
tw.getRequestToken
consumerKeyとconsumerSecretを使って、OAuth認証のページURLに必要なRequestTokenを取得。
RequestTokenを元にOAuth認証のページにアクセスしてもらって、PINコードを取ってきてもらう。
tw.getAccessToken
入力されたPINコードを元にAccessToken(APIをたたくのに使う)を取得して保存。
一回AccessTokenが手に入れば認証は半永久的。
APIたたくときにAccessTokenを一緒に使う。
*/
// ClientInfomation
// http://dev.twitter.com/apps
var clientInfo = {
name: 'PNBT',
consumerKey: '9zzFsVFm4nLyfF5WXZsbrg',
consumerSecret: '318LbLBmvECeZZmSUKVzdq8dpazGtMFf2P6hMPUjWU',
}
// TwitterOauthにクライアント情報を渡してnewする(必須)
var tw = new TwitterOauth(clientInfo);
if(tw.isAuthorize()){
// update test with client name.
GM_registerMenuCommand("OAuth update test",function(){
var content = {status: "update test", source: clientInfo.name};
tw.post('http://twitter.com/statuses/update.json', content ,function(){
console.log(res); // firebug
});
});
var query = {
q : "oauth",
per_page : 5
}
// get test
GM_registerMenuCommand("OAuth search test(Firebug)",function(){
tw.get("http://api.twitter.com/1/users/search.json" , query ,function(res){
console.log(res); // firebug
})
});
}else{ // tokenがまだないなら認証させる
var iframeDoc;
GM_registerMenuCommand("OAuth Authorization",function(){
makeFrame(function gotFrame1(iframe, win, doc) {
iframeDoc = doc;
iframe.height = "350";
iframe.width = "500";
iframe.style.position = "fixed";
iframe.style.bottom = iframe.style.left = "0";
tw.getRequestToken(createAuthorizationMenu);
});
});
}
function createAuthorizationMenu(authorizeURL){
var div = iframeDoc.createElement("div");
div.id = "GM_iframe_Oauth";
var imgOauth = iframeDoc.createElement("img");
imgOauth.src = "data:image/png;base64,iVBORw0KGgo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NWNL37kOdYsGU9OQzKUWT2/owOf3MzTpYWZOoH98ks9L7cRmG8Xj40GvnJvf9ogZQWBo0kOWQzwm91oci3L/VnEPgK9s9xV7BuoGU/tHo/jfXkXPU2KzjRy+K957rrg6VfVzW7rx+f2MTE0H3S8228hm3a1FmqGcJ3Cd4ezg+ayX2TkfM3M+7E+GFXWWQjZXTFaRIttN4scRJ/rUzbWtXTSXmoZkLrV4TFYRH+bd4rMSGx8X3OatJfLitZms2ZfGmv0nOFfpAOAPhZVLaq8W3rtexm/yLf/zOdTmCVzHZxfz0bcVvJtb+kL6srk2XDCo8vfKewiCeATufijewSS2tQu0jAv0uydU6yVzLdVjpTwZn6TfPcHjsXF8fj/61u6Q6EYIPbK53jyvX5K/llbT/t0/F13mBUGg/9kEv9ffUq2VzLVcj5WQmFnAZ0WV7Cqt5hfXSkKiGSE8yOZ648yNsCGZK5w9Iqw+ZHO9fio3bEjmCmePCKsP2Vzr03LChmSucPaIsPqQzbXuWHbYkMwVzh4RVh8LvgEUIUIo0Wg0mn8B8xp8c6hoOjUAAAAASUVORK5CYII=";
imgOauth.addEventListener("click",function(){
GM_openInTab(authorizeURL);
},false);
var textPin = iframeDoc.createElement("p");
textPin.innerHTML = "input PIN code";
var inputPin = iframeDoc.createElement("input");
var submitBt = iframeDoc.createElement("Button");
submitBt.textContent = "SAVE";
submitBt.addEventListener("click",function(e){
tw.getAccessToken(inputPin.value.replace(/\s/g ,"") , function(){
div.innerHTML += "success!";
location.reload();
});
},false)
div.appendChild(imgOauth);
div.appendChild(textPin);
div.appendChild(inputPin);
div.appendChild(submitBt);
iframeDoc.body.appendChild(div);
}
// about:configなフレームを作成する。
function makeFrame(callback/*(iframeTag, window, document)*/, name, debug) {
function testInvasion() {
iframe.removeEventListener("load", done, true);
var message = ((new Date) - load.start) + "ms passed, ";
try { // probe for security violation error, in case mozilla struck a bug
var url = unsafeWindow.frames[framename].location.href;
message += url == "about:blank" ? "but we got the right document." : "and we incorrectly loaded " + url;
if (debug) console.log(message);
done();
}
catch(e) {
if (console && console.error && console.trace) {
console.error(e);
console.trace();
}
if (debug) console.log(message + "and our iframe was invaded. Trying again!");
document.body.removeChild(iframe);
makeFrame(callback, name);
}
}
function done() {
clearTimeout(load.timeout);
if (debug) console.log("IFrame %x load event after %d ms", framename, (new Date) - load.start);
var win = unsafeWindow.frames[framename];
var doc = iframe.contentWindow.document;
// 苦し紛れのエスケープ
var esframeName = "'"+framename+"'";
// 自分自身のiframeを閉じるボタン
var xImg = doc.createElement("img");
xImg.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAA4AAAAOCAYAAAAfSC3RAAAATElEQVQoka2RSQ4AIAgD+f+jp96M0aq49AgdUiB0qZCkONQ/EBAwDOrrU7A1uZqN2hodtNwRqNdz0VOg62+jzuDUcVzkf+/I6h28UQHjW25Gob5AIAAAAABJRU5ErkJggg=="
xImg.setAttribute("onclick", "parent.document.getElementsByName("+esframeName+")[0].style.display='none';");
xImg.setAttribute("style","background-color:red;border:3px;position:fixed;top:0px;right:0px");
doc.body.appendChild(xImg);
callback(iframe, win, doc);
}
var iframe = document.createElement("iframe");
var framename = iframe.name = typeof name != "undefined" ? name : ("pane" + (makeFrame.id = (makeFrame.id || 0) - 1));
iframe.setAttribute("style", "overflow:auto;z-index:18999; border:0; margin:0; padding:0;top:auto; right:auto; bottom:auto; left:auto;background-color:#fff");
iframe.src = "about:blank";
iframe.addEventListener("load", done, true);
var frames = makeFrame.data || {};
var load = frames[framename] || {
start: new Date,
sleepFor: 400
};
load.timeout = setTimeout(testInvasion, load.sleepFor);
load.sleepFor *= 1.5;
frames[framename] = load;
makeFrame.data = frames;
document.body.appendChild(iframe);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.