Skip to content

Instantly share code, notes, and snippets.

@0mg
Created July 27, 2010 07:59
Show Gist options
  • Save 0mg/491919 to your computer and use it in GitHub Desktop.
Save 0mg/491919 to your computer and use it in GitHub Desktop.
Twitter Prompt (iframe.postMessage)
// ==UserScript==
// @name Twitter Prompt
// @include http://twitter.com/share?url=www.u.js
// @description Tweet via window.prompt
// ==/UserScript==
/* Bookmarklet */
if (false) {
javascript: (function(s, f, t, m, l) {
if (s = prompt('いまどうしてる?', s)) {
if ((l = s.length) > 140 &&
(m = s.match(/https:\/\/\S{11,}|http:\/\/\S{12,}/g))) {
l = l - m.join('').length + m.length * 19;
}
if (confirm(s.slice(0, 140) +
'\n\nあと\x20' + (140 - l) + '\x20字入力可能')) {
addEventListener('message', function(v) {
v.data == t && f.parentNode.removeChild(f) &&
removeEventListener(v.type, arguments.callee, true);
}, true);
f = document.createElement('iframe');
f.style.display = 'none';
f.onload = function() {
f.contentWindow.postMessage((t = +new(Date)) + '&' +
encodeURIComponent(s), '*')
};
f.src = 'http://twitter.com/share?url=www.u.js';
document.body.appendChild(f)
} else { arguments.callee(s) }
}
})(encodeURI(decodeURI(location)))
}
/* Main */
opera.addEventListener("BeforeScript", function(v) {
v.preventDefault();
}, true);
opera.addEventListener("BeforeExternalScript", function(v) {
v.preventDefault();
}, true);
window.addEventListener("message", function(v) {
twitterPrompt(decodeURIComponent(v.data.split("&")[1]));
function twitterPrompt(tweet) {
if (!document.getElementsByName("authenticity_token")[0]) {
return exit("ログインしてやり直して下さい", tweet);
}
if (tweet.length > 140) {
var longurls = tweet.match(/https:\/\/\S{11,}|http:\/\/\S{12,}/g);
if (longurls) {
longurls.forEach(function(url) {
var xhr = new XMLHttpRequest;
xhr.open("GET",
"/share?url=" + encodeURIComponent(url), false);
xhr.onreadystatechange = function() {
if (this.readyState < 4) return;
if (this.status === 200) {
tweet = tweet.replace(url, this.responseText.
match(/aria-describedby=\"notice\"> ([^<]+)/)[1]);
} else exit(this.getAllResponseHeaders(), tweet);
};
xhr.send(null);
});
}
}
if (!confirm(tweet.slice(0, 140) +
"\n\nこの文をツイートします")) return exit();
var xhr = new XMLHttpRequest;
xhr.open("POST", "/status/update", true);
xhr.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded")
xhr.onreadystatechange = function() {
if (this.readyState < 4) return;
if (this.status === 200) {
exit();
} else exit(this.getAllResponseHeaders(), tweet);
};
xhr.send("authenticity_token=" +
document.getElementsByName("authenticity_token")[0].value +
"&status=" + encodeURIComponent(tweet));
};
function exit(msg, query) {
if (msg !== void 0 && query !== void 0) prompt(Error(msg), query);
v.source.postMessage(v.data.split("&")[0], "*");
};
}, true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment