Video Player Toothbrush

牙刷科技!让所有视频播放器网页全屏!默认快捷键ALT+1,可自行修改

  1. // ==UserScript==
  2. // @name Video Player Toothbrush
  3. // @namespace http://www.icycat.com
  4. // @description 牙刷科技!让所有视频播放器网页全屏!默认快捷键ALT+1,可自行修改
  5. // @author 冻猫
  6. // @include *www.bilibili.com/*
  7. // @include *www.bilibili.tv/*
  8. // @include *bilibili.kankanews.com/*
  9. // @include *.iqiyi.com/*
  10. // @include *v.youku.com/*
  11. // @include *www.youtube.com/*
  12. // @include *v.17173.com/*
  13. // @include *www.tudou.com/*
  14. // @include *.letv.com/*
  15. // @include *v.pptv.com/*
  16. // @include *tv.sohu.com/*
  17. // @include *v.ku6.com/*
  18. // @include *vod.kankan.com/*
  19. // @include *v.qq.com/*
  20. // @include *www.56.com/*
  21. // @include *live.yy.com/*
  22. // @include *yy.tv/*
  23. // @include *www.acfun.com/*
  24. // @include *www.acfun.tv/*
  25. // @include *donghua.dmzj.com/*
  26. // @include *video.sina.com.cn/*
  27. // @include *.cntv.cn/*
  28. // @include *.douyutv.com/*
  29. // @include *music.163.com/*
  30. // @include *v.163.com/*
  31. // @include *www.twitch.tv/*
  32. // @include *v.yinyuetai.com/*
  33. // @version 3.2
  34. // @grant none
  35. // @run-at document-end
  36. // ==/UserScript==
  37.  
  38. //若有需要可以自行添加域名,按照include的格式添加即可。
  39. //自行修改快捷键,请参考下面代码中的注释。注意焦点在flash上时快捷键会失效。
  40.  
  41. (function() {
  42.  
  43. var parentArray = new Array(),
  44. player = null,
  45. fullStatus = false,
  46. backStyle = new Array(),
  47. childStyle = new Array(),
  48. playerStyle, parent, type, iframe;
  49.  
  50. function init() {
  51. createButton();
  52. window.addEventListener("keydown", function(e) {
  53. //默认快捷键为alt + 1, 全屏/恢复。altkey是按键ALT,keyCode=49是按键1,需要修改为其他快捷键的请搜索"keycode",修改为按键对应的数字。
  54. if (e.altKey && e.keyCode == 49) {
  55. playerControl();
  56. }
  57. }, false);
  58. console.log('Video Player Toothbrush 初始化');
  59. }
  60.  
  61. function createButton() {
  62. var leftButton = document.createElement('span');
  63. leftButton.id = 'leftFullStackButton';
  64. leftButton.onclick = function() {
  65. playerControl();
  66. };
  67. document.body.appendChild(leftButton);
  68. addStyle('#leftFullStackButton{position:fixed;width:1px;height:100%;top:0;left:0;z-index:2147483646;}');
  69. var rightButton = document.createElement('span');
  70. rightButton.id = 'rightFullStackButton';
  71. rightButton.onclick = function() {
  72. playerControl();
  73. };
  74. document.body.appendChild(rightButton);
  75. addStyle('#rightFullStackButton{position:fixed;width:1px;height:100%;top:0;right:0;z-index:2147483646;}');
  76. }
  77.  
  78. function addStyle(css) {
  79. var style = document.createElement('style');
  80. style.type = 'text/css';
  81. var node = document.createTextNode(css);
  82. style.appendChild(node);
  83. document.head.appendChild(style);
  84. }
  85.  
  86. function playerControl() {
  87. if (!player) {
  88. checkPlayer();
  89. fullWin();
  90. } else {
  91. if (!fullStatus) {
  92. switch (type) {
  93. case 'object':
  94. var objectArray = parent.getElementsByTagName('object');
  95. checkObject(objectArray);
  96. break;
  97. case 'embed':
  98. var embedArray = parent.getElementsByTagName('embed');
  99. checkEmbed(embedArray);
  100. break;
  101. case 'html5':
  102. var html5Array = parent.getElementsByTagName('html5');
  103. checkHtml5(html5Array);
  104. break;
  105. case 'innerIframe':
  106. case 'iframe':
  107. var iframeArray = parent.getElementsByTagName('iframe');
  108. checkIframe(iframeArray);
  109. break;
  110. }
  111. fullWin();
  112. } else {
  113. smallWin();
  114. }
  115. }
  116. }
  117.  
  118. function checkPlayer() {
  119. var objectArray = document.getElementsByTagName('object');
  120. console.log('object数量' + objectArray.length);
  121. checkObject(objectArray);
  122. if (!player) {
  123. console.log('未找到object播放器');
  124. var embedArray = document.getElementsByTagName('embed');
  125. console.log('embed数量' + embedArray.length);
  126. checkEmbed(embedArray);
  127. }
  128. if (!player) {
  129. console.log('未找到embed播放器');
  130. var html5Array = document.getElementsByTagName('video');
  131. console.log('html5视频数量' + html5Array.length);
  132. checkHtml5(html5Array);
  133. }
  134. if (!player) {
  135. console.log('未找到html5播放器');
  136. var iframeArray = document.getElementsByTagName('iframe');
  137. console.log('iframe数量' + iframeArray.length);
  138. checkIframe(iframeArray);
  139. }
  140. if (!player) {
  141. console.log('未找到iframe引用的播放器');
  142. return;
  143. }
  144. parent = player.parentNode;
  145. var full = player;
  146. while (full = full.parentNode) {
  147. if (full.getAttribute && full.nodeName != 'OBJECT') {
  148. full.setAttribute('full_stack', true);
  149. parentArray.push(full);
  150. }
  151. if (full.nodeName == 'HTML') {
  152. break;
  153. }
  154. }
  155. if (type == 'innerIframe') {
  156. full = iframe;
  157. do {
  158. if (full.getAttribute) {
  159. full.setAttribute('full_stack', true);
  160. parentArray.push(full);
  161. }
  162. if (full.nodeName == 'HTML') {
  163. break;
  164. }
  165. } while (full = full.parentNode);
  166. }
  167. }
  168.  
  169. function checkObject(objectArray) {
  170. if (objectArray.length > 0) {
  171. for (i = 0; i < objectArray.length; i++) {
  172. console.log('object播放器检测' + i);
  173. if (objectArray[i].type == 'application/x-shockwave-flash' && objectArray[i].offsetWidth > 299 && objectArray[i].offsetHeight > 199) {
  174. player = objectArray[i];
  175. type = 'object';
  176. console.log('找到object播放器');
  177. break;
  178. }
  179. }
  180. }
  181. }
  182.  
  183. function checkEmbed(embedArray) {
  184. if (embedArray.length > 0) {
  185. for (i = 0; i < embedArray.length; i++) {
  186. console.log('embed播放器检测' + i);
  187. if (embedArray[i].type == 'application/x-shockwave-flash' && embedArray[i].offsetWidth > 299 && embedArray[i].offsetHeight > 199) {
  188. player = embedArray[i];
  189. type = 'embed';
  190. console.log('找到embed播放器');
  191. break;
  192. }
  193. }
  194. }
  195. }
  196.  
  197. function checkHtml5(html5Array) {
  198. if (html5Array.length > 0) {
  199. for (i = 0; i < html5Array.length; i++) {
  200. console.log('html5播放器检测' + i);
  201. if (html5Array[i].offsetWidth > 299 && html5Array[i].offsetHeight > 199) {
  202. player = html5Array[i];
  203. type = 'html5';
  204. console.log('找到html5播放器');
  205. break;
  206. }
  207. }
  208. }
  209. }
  210.  
  211. function checkIframe(iframeArray) {
  212. if (iframeArray.length > 0) {
  213. for (var i = 0; i < iframeArray.length; i++) {
  214. if (iframeArray[i].offsetWidth > 299 && iframeArray[i].offsetHeight > 199) {
  215. try {
  216. var objectArray = iframeArray[i].contentWindow.document.getElementsByTagName('object');
  217. console.log('iframe' + i + '中object数量' + objectArray.length);
  218. checkObject(objectArray);
  219. if (!player) {
  220. console.log('iframe' + i + '中未找到object播放器');
  221. var embedArray = iframeArray[i].contentWindow.document.getElementsByTagName('embed');
  222. console.log('iframe' + i + '中embed数量' + embedArray.length);
  223. checkEmbed(embedArray);
  224. }
  225. if (player) {
  226. iframe = iframeArray[i];
  227. type = 'innerIframe';
  228. break;
  229. } else {
  230. console.log('未找到iframe' + i + '中的播放器');
  231. }
  232. } catch (e) {
  233. player = iframeArray[i];
  234. type = 'iframe';
  235. console.log('找到可能通过iframe跨域引用的播放器');
  236. break;
  237. }
  238. }
  239. }
  240. }
  241. }
  242.  
  243. function fullWin() {
  244. if (!fullStatus) {
  245. window.addEventListener('resize', fullWin, false);
  246. playerStyle = player.style.cssText;
  247. }
  248. for (var i = 0; i < parentArray.length; i++) {
  249. if (!fullStatus) {
  250. backStyle[i] = parentArray[i].style.cssText;
  251. }
  252. parentArray[i].style.cssText = 'width:100% !important;height:100% !important;max-width:100% !important;margin:0px !important;padding:0px !important;top:0px !important;left:0px !important;z-index:2147483645 !important;overflow:hidden !important;position:fixed !important;background:#000 !important;';
  253. }
  254. player.style.cssText = 'width:calc(100% - 2px) !important;height:100% !important;z-index:2147483645 !important;left:1px !important;position:relative !important;visibility:visible !important;'
  255. console.log('网页全屏完成');
  256. fullStatus = true;
  257. }
  258.  
  259. function smallWin() {
  260. window.removeEventListener('resize', fullWin, false);
  261. for (var i = 0; i < parentArray.length; i++) {
  262. parentArray[i].style.cssText = backStyle[i];
  263. }
  264. player.style.cssText = playerStyle;
  265. console.log('恢复完成');
  266. fullStatus = false;
  267. }
  268.  
  269. init();
  270.  
  271. })();