-
-
Save azu/788054 to your computer and use it in GitHub Desktop.
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
var embedReply, tweetCache = {}; | |
(function () { | |
// APIが足りない時用proxy(気休め) | |
var proxies = [ | |
{ | |
url: twitterAPI + 'statuses/show/{id}.json?suppress_response_codes=true', | |
filter: function (d) { | |
return d | |
} | |
}, | |
{ | |
url: 'https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20twitter.status%20where%20id%3D{id}%3B&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys', | |
filter: function (d) { | |
return d.query.results.status | |
} | |
} | |
]; | |
// トリガーになるボタンをつくる | |
function makeEmbedButton(element, tw) { | |
//console.log(['makeEmbedButton', element, tw]); | |
if (tw.in_reply_to_status_id_str) { | |
// getElementsByClassName使ってるのでIEでは動かない? | |
var reply = element.getElementsByClassName('reply'); | |
if (reply && reply.length > 0) { | |
reply = reply[0]; | |
var embrep = document.createElement('a'); | |
embrep.className = 'button embrep'; | |
embrep.setAttribute('href', '#'); | |
/* | |
embrep.onclick = function () { | |
embedReply(tw.in_reply_to_screen_name, tw.in_reply_to_status_id_str, this); | |
} | |
*/ | |
embrep.addEventListener("click", function(evt) { | |
evt.preventDefault(); | |
embedReply(tw.in_reply_to_screen_name, tw.in_reply_to_status_id_str, evt.currentTarget); | |
}, false); | |
embrep["textContent" || "innerText"] = '@'; | |
reply.parentNode.insertBefore(embrep, reply); | |
} | |
} | |
} | |
// 上でつくった埋め込みボタンを自動的に押す | |
function autoEmbed(tw, tw_node, nr_show) { | |
// console.log(['autoEmbed', tw, tw_node, nr_show]); | |
if (nr_show > 0) { | |
var links = tw_node.getElementsByClassName('embrep'); | |
//console.log(['search embrep', links]); | |
for (var i = 0,len = links.length; i < len; i++) (function(link){ | |
// console.log(['click function', links[i].onclick, link]); | |
var div = getTweetDiv(link); | |
if (div && div.id.indexOf('emb-') == -1) { | |
dispatchMouseEvents({ type:'click', target:link, button:0 }); | |
} | |
})(links[i]) | |
} | |
} | |
// ボタンから呼ばれる | |
embedReply = function (user, id, elem) { | |
//console.log(arguments); | |
$('loading').style.display = 'block'; | |
fetchTweet(id, function (tw) { | |
//console.log(tw); | |
$('loading').style.display = 'none'; | |
var el = document.createElement('div'); | |
el.id = 'emb-' + id; | |
el.className = 'emb'; | |
el.style.paddingLeft = '32px'; //とりあえずのスタイル | |
el.innerHTML = makeHTML(tw, false, 'emb'); | |
el.tw = tw; | |
callPlugins("newMessageElement", el, tw, 'emb'); | |
var p_elem = getTweetDiv(elem); | |
if (p_elem.parentNode) { | |
p_elem.parentNode.insertBefore(el, p_elem.nextSibling); | |
} | |
elem.parentNode.removeChild(elem); | |
}); | |
}; | |
function dispatchMouseEvents(opt) { | |
var evt = document.createEvent('MouseEvents'); | |
evt.initMouseEvent(opt.type, opt.canBubble || true, opt.cancelable || true, opt.view || window, | |
opt.detail || 0, opt.screenX || 0, opt.screenY || 0, opt.clientX || 0, opt.clientY || 0, | |
opt.ctrlKey || false, opt.altKey || false, opt.shiftKey || false, opt.metaKey || false, | |
opt.button || 0, opt.relatedTarget || null); | |
opt.target.dispatchEvent(evt); | |
return evt; | |
} | |
// API消費が激しい可能性があるのでできるだけキャッシュしておく | |
function fetchTweet(id, callback) { | |
if (tweetCache[id]) { | |
callback(tweetCache[id]); | |
return; | |
} | |
var d = $('tw-' + id) || $('re-' + id) || $('tw2c-' + id) || $('emb-' + id); | |
if (d && d.tw) { | |
tweetCache[id] = d.tw; | |
callback(d.tw); | |
} else { | |
var proxy = proxies[0]; // 0はproxyなし | |
var url = proxy.url.replace(/\{id\}/, id); | |
xds.load(url, function (tw) { | |
tw = proxy.filter(tw); | |
tweetCache[id] = tw; | |
callback(tw); | |
}); | |
} | |
} | |
// 任意のElementから親をたどっていって、含まれるツイートのdivを得る | |
function getTweetDiv(elem) { | |
var t = elem; | |
while (t && !(t.tagName.toLowerCase() == 'div' && /^[a-z0-9]+-\d{10,}$/.test(t.id))) t = t.parentNode; | |
return t; | |
} | |
// twShowToNode関数に独自のフックポイントを追加 | |
var twShowToNode_original = twShowToNode; | |
twShowToNode = function (tw, tw_node) { | |
var nr_show = twShowToNode_original.apply(window, arguments); | |
callPlugins("afterTwShowToNode", tw, tw_node, nr_show, arguments); | |
return nr_show; | |
}; | |
registerPlugin({ | |
newMessageElement: makeEmbedButton, | |
afterTwShowToNode: autoEmbed | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment