Created
May 1, 2009 09:22
-
-
Save liquidz/104952 to your computer and use it in GitHub Desktop.
nicotter: greasemonkey script for posting NicottoTown mutter to Twitter
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 nicotter | |
// @version 0.01 | |
// @namespace http://www.nicotto.jp | |
// @description post NicottoTown mutter to Twitter | |
// @author uochan (or liquidz@GitHub) | |
// @include http://www.nicotto.jp/user/mypage* | |
// ==/UserScript== | |
(function(){ | |
// ========== YOU CAN EDIT ========== | |
var flags = { | |
// Twitterへの投稿確認の有無 | |
confirm_before_post: true, | |
// Twitterへの投稿結果の表示・非表示 | |
alert_result: true | |
}; | |
var messages = { | |
question: "Twitterに投稿しますか?", | |
post_success: "Twitterへ投稿しました。", | |
post_error: "Twitterへの投稿に失敗しました。" | |
}; | |
// ================================== | |
// =foreach | |
// ---------------------------------------------------- | |
var foreach = function(obj, fn){ | |
for(var i = 0; i < obj.length; ++i){ | |
if(fn(obj[i], i) == -1) break; | |
} | |
}; | |
// =findElementByAttribute | |
// ---------------------------------------------------- | |
var findElementByAttribute = function(params){ | |
var result = null; | |
if(params.tag && params.key && params.value){ | |
foreach(document.getElementsByTagName(params.tag), function(x){ | |
if(x.getAttribute(params.key).indexOf(params.value) != -1){ | |
result = x; | |
return -1; // foreachから抜ける(breakの代わり) | |
} | |
}); | |
} | |
return result; | |
} | |
// =hookPostMessage | |
// ---------------------------------------------------- | |
var hookPostMessage = function(hook_fn){ | |
var form = findElementByAttribute({tag: 'form', key: 'onsubmit', value: 'postMessage'}); | |
var a = findElementByAttribute({tag: 'a', key: 'href', value: 'postMessage()'}); | |
if(form) form.addEventListener("submit", hook_fn, false); | |
if(a) a.addEventListener("click", hook_fn, false); | |
}; | |
// =postToTwitter | |
// ---------------------------------------------------- | |
var postToTwitter = function(status){ | |
GM_xmlhttpRequest({ | |
method: 'POST', | |
url: 'http://twitter.com/statuses/update.json', | |
data: 'status=' + encodeURIComponent(status), | |
headers: { | |
"Content-type": "application/x-www-form-urlencoded" | |
}, | |
onload: function(res){ | |
if(flags.alert_result) alert(messages.post_success); | |
}, | |
onerror: function(res){ | |
if(flags.alert_result) alert(messages.post_error); | |
} | |
}); | |
}; | |
// =main | |
// ---------------------------------------------------- | |
hookPostMessage(function(){ | |
if(flags.confirm_before_post ? (confirm(messages.question) ? true : false) : true){ | |
var input_form = findElementByAttribute({tag: 'input', key: 'name', value: 'message'}); | |
if(input_form) postToTwitter(input_form.value); | |
} | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment