Created
February 27, 2010 06:07
-
-
Save miya2000/316515 to your computer and use it in GitHub Desktop.
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 Quicktime Movie Download | |
// @include http://* | |
// ==/UserScript== | |
(function() { | |
function evaluate(xpath, context) { | |
var eles = document.evaluate(xpath, context || document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null); | |
var arr = []; | |
for (var i = 0, len = eles.snapshotLength; i < len; i++) { | |
arr.push(eles.snapshotItem(i)); | |
} | |
return arr; | |
} | |
function download(e) { | |
if (typeof btoa == 'undefined') return; // Opera 10.50 or later. | |
var href = e.currentTarget.href; | |
if (/\.mov$/i.test(href)) { | |
var xhr = new XMLHttpRequest(); | |
xhr.open('GET', e.currentTarget.href, false); | |
xhr.overrideMimeType('text/plain; charset=x-user-defined'); | |
xhr.send(null); | |
if (xhr.responseText && xhr.responseText.length < 1000) { | |
var str = encodeURI(xhr.responseText).replace(/%[0-9A-Za-z]{2}/g, ' '); | |
var m = /(\S*\.m4[va])\b/.exec(str); | |
if (m) { | |
e.preventDefault(); | |
location.href = m[1]; | |
} | |
} | |
} | |
} | |
function doHogeHoge(target) { | |
var objects = evaluate('descendant-or-self::object[@type="video/quicktime" or @classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B"]', target); | |
if (objects.length == 0) { | |
return; | |
} | |
for (var i = 0; i < objects.length; i++) { | |
var obj = objects[i]; | |
var params = evaluate('param[@name="src" and @value]', obj); | |
if (params.length > 0) { | |
var link = document.createElement('a'); | |
link.href = params[0].value; | |
link.textContent = 'movie'; | |
link.target = '_blank'; | |
link.addEventListener('click', download, false); | |
obj.parentNode.insertBefore(link, obj); | |
} | |
} | |
} | |
function main() { | |
if (!document.body) return; | |
doHogeHoge(document.body); | |
document.addEventListener('DOMNodeInserted', function(e) { | |
document.removeEventListener('DOMNodeInserted', arguments.callee, false); | |
doHogeHoge(e.target); | |
document.addEventListener('DOMNodeInserted', arguments.callee, false); | |
}, false); | |
} | |
document.addEventListener('DOMContentLoaded', main, false); | |
})() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment