Skip to content

Instantly share code, notes, and snippets.

@monmon
Created August 6, 2008 05:00
Show Gist options
  • Select an option

  • Save monmon/4167 to your computer and use it in GitHub Desktop.

Select an option

Save monmon/4167 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name showYouTubeInGoogleSearch
// @namespace http://github.com/monmon
// @description Show YouTube Flash in Google search result
// @include http://www.google.co.jp/search*
// ==/UserScript==
(function() {
var length = 0;
function onScroll() {
if ( $x('id("res")//table').length > length ) {
main();
length = $x('id("res")//table').length; // 長さをセット
}
}
function main() {
$x('id("res")/div/div/table/tbody/tr/td/table/tbody/tr/td/a').map(function(a){
var url = a.href.match(/\?q=([^&]*)/)[1].replace(/watch%3Fv%3D/,'v/');
var embed = '<embed src="' + url + '" type="application/x-shockwave-flash" wmode="transparent"></embed>';
a.parentNode.innerHTML = embed;
});
}
main();
window.addEventListener('scroll', onScroll, false);
/**
* $x()
* http://lowreal.net/2006/shibuya-js-1-lt.html
* http://lowreal.net/logs/2006/03/16/1
* // id
* $X('id("res")');
*
* // セクションクラスな div 列挙
* $X("//div[@class='section']");
*
* // 最初の h1 要素
* $X("//h1[0]");
*
* // 最後の p 要素
* $X("//p[last()]");
*
* // address 要素以下で、
* // mailto スキームから始まる a 要素
* $X("//address//a[starts-with(@href, 'mailto:')]");
*
* // 要素選択以外でも使えるよ?
* $X("string()", node); //=> "Foo Bar"
*
*/
function $x(exp, context) {
if (!context) context = document;
var resolver = function (prefix) {
var o = document.createNSResolver(context)(prefix);
return o ? o : (document.contentType == "text/html") ? "" : "http://www.w3.org/1999/xhtml";
}
var exp = document.createExpression(exp, resolver);
var result = exp.evaluate(context, XPathResult.ANY_TYPE, null);
switch (result.resultType) {
case XPathResult.STRING_TYPE : return result.stringValue;
case XPathResult.NUMBER_TYPE : return result.numberValue;
case XPathResult.BOOLEAN_TYPE: return result.booleanValue;
case XPathResult.UNORDERED_NODE_ITERATOR_TYPE: {
result = exp.evaluate(context, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
var ret = [];
for (var i = 0, len = result.snapshotLength; i < len ; i++) {
ret.push(result.snapshotItem(i));
}
return ret;
}
}
return null;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment