|
| 1 | +// ==UserScript== |
| 2 | +// @name acfunHTML5 |
| 3 | +// @version 1.0 |
| 4 | +// @include http://www.acfun.tv/v/* |
| 5 | +// @description Play Videos with html5 on acfun.tv |
| 6 | +// @author LiuLang |
| 7 | + |
| 8 | +// @license GPLv3 |
| 9 | +// @grant GM_xmlhttpRequest |
| 10 | +// @run-at document-end |
| 11 | +// ==/UserScript== |
| 12 | + |
| 13 | +/** |
| 14 | + * v1.0 - 2013.11.1 |
| 15 | + * Project inited. |
| 16 | + */ |
| 17 | + |
| 18 | +var uw = unsafeWindow, |
| 19 | + log = uw.console.log, |
| 20 | + error = uw.console.error; |
| 21 | + |
| 22 | +var pps = { |
| 23 | + vid: '', |
| 24 | + origUrl: '', |
| 25 | + |
| 26 | + run: function() { |
| 27 | + this.getVid(); |
| 28 | + if (this.vid.length === 0) { |
| 29 | + error('Failed to get video id!'); |
| 30 | + return false; |
| 31 | + } |
| 32 | + this.getVideoLink(); |
| 33 | + }, |
| 34 | + |
| 35 | + getVid: function() { |
| 36 | + log('getVid()'); |
| 37 | + var pReg = /video\]([^\[]+)\[/, |
| 38 | + p = uw.document.querySelector('div#mainer-inner div#area-player.video div.hidden'), |
| 39 | + iframe = uw.document.querySelector('iframe#ACFlashPlayer-re'), |
| 40 | + iframeReg = /#vid=(\d+);/, |
| 41 | + match; |
| 42 | + if (p) { |
| 43 | + match = pReg.exec(p.innerHTML); |
| 44 | + this.vid = match[1]; |
| 45 | + } else { |
| 46 | + match = iframeReg.exec(iframe.src); |
| 47 | + this.vid = match[1]; |
| 48 | + } |
| 49 | + }, |
| 50 | + |
| 51 | + getVideoLink: function() { |
| 52 | + log('getVideoLink()'); |
| 53 | + log(this); |
| 54 | + var url = 'http://www.acfun.tv/api/getVideoByID.aspx?vid=' + this.vid, |
| 55 | + that = this; |
| 56 | + |
| 57 | + GM_xmlhttpRequest({ |
| 58 | + method: 'GET', |
| 59 | + url: url, |
| 60 | + onload: function(response) { |
| 61 | + log('response:', response); |
| 62 | + var reg = /vurl":"([^"]+)"/, |
| 63 | + match = reg.exec(response.responseText); |
| 64 | + if (match) { |
| 65 | + that.origUrl = match[1]; |
| 66 | + that.createUI(); |
| 67 | + } |
| 68 | + }, |
| 69 | + }); |
| 70 | + }, |
| 71 | + |
| 72 | + createUI: function() { |
| 73 | + log('createUI() --'); |
| 74 | + var div = uw.document.createElement('div'), |
| 75 | + a; |
| 76 | + |
| 77 | + this.addStyle([ |
| 78 | + '.download-wrap { ', |
| 79 | + 'position: fixed; ', |
| 80 | + 'left: 10px; ', |
| 81 | + 'bottom: 10px; ', |
| 82 | + 'border: 2px solid #ccc; ', |
| 83 | + 'border-top-right-radius: 15px; ', |
| 84 | + 'margin; 0; ', |
| 85 | + 'padding: 10px; ', |
| 86 | + 'background-color: #fff; ', |
| 87 | + 'z-index: 9999; ', |
| 88 | + '}', |
| 89 | + '.download-link { ', |
| 90 | + 'display: block;', |
| 91 | + '}', |
| 92 | + '.download-link:hover { ', |
| 93 | + 'text-decoration: underline; ', |
| 94 | + '}', |
| 95 | + '.download-link:active {', |
| 96 | + 'color: #e03434; ', |
| 97 | + 'outline: none; ', |
| 98 | + '}', |
| 99 | + ].join('')); |
| 100 | + |
| 101 | + a = uw.document.createElement('a'); |
| 102 | + a.href = this.origUrl; |
| 103 | + a.innerHTML = '视频原始地址'; |
| 104 | + a.className = 'download-link'; |
| 105 | + div.appendChild(a); |
| 106 | + |
| 107 | + div.className = 'download-wrap'; |
| 108 | + uw.document.body.appendChild(div); |
| 109 | + }, |
| 110 | + |
| 111 | + /** |
| 112 | + * Create a new <style> tag with str as its content. |
| 113 | + * @param string styleText |
| 114 | + * - The <style> tag content. |
| 115 | + */ |
| 116 | + addStyle: function(styleText) { |
| 117 | + var style = uw.document.createElement('style'); |
| 118 | + if (uw.document.head) { |
| 119 | + uw.document.head.appendChild(style); |
| 120 | + style.innerHTML = styleText; |
| 121 | + } |
| 122 | + }, |
| 123 | +} |
| 124 | + |
| 125 | +pps.run(); |
0 commit comments