JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
blank tabs black
[userscripts.git] / youtube-html5-hider / script.js
1 // ==UserScript==
2 // @name          YouTube html5 unembedder
3 // @namespace     http://jasonwoof.com/
4 // @description   Replace YouTube html5 embedds with links
5 // @include       *
6 // @exclude       http://youtube.com/*
7 // @exclude       http://www.youtube.com/*
8 // ==/UserScript==
9
10 // Copyright Jason Woofenden 2012. Use as you wish (CC0)
11
12 (function() {
13         var replace = function(old, replacement) {
14                 old.parentNode.appendChild(replacement);
15                 old.parentNode.removeChild(old);
16         }
17
18         var embed_types = [
19                 {tag: 'iframe', attr: 'src'},
20                 {tag: 'object', attr: 'data'},
21                 {tag: 'embed',  attr: 'src'}
22         ];
23
24         var i;
25         for(i = 0; i < embed_types.length; ++i) {
26                 var tag_type = embed_types[i].tag;
27                 var attr = embed_types[i].attr
28
29                 var tags = document.getElementsByTagName(tag_type);
30                 for(var j = tags.length - 1; j >= 0; --j) {
31                         var tag = tags[j];
32                         if(tag[attr]) {
33                                 var match = tag[attr].match(/^http:\/\/www.youtube(-nocookie)?.com\/(embed|v)\/([^?]*)/)
34                                 if(match) {
35                                         var div = document.createElement('div');
36                                         div.innerHTML = '<a href="http://www.youtube.com/watch?v=' + match[3] + '" style="display: block; width: 200px; height: 20px; padding: 20px; margin: 0; border: 2px dotted #f00; background: #fdd !important; text-align: center; font-size: 16px; font-weight: bold; color: #000">YouTube: ' + match[3] + '</a>';
37                                         replace(tag, div);
38                                 }
39                         }
40                 }
41         }
42 }());
43