Video Player Toothbrush

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

目前为 2014-07-05 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Video Player Toothbrush
  3. // @namespace http://www.icycat.com
  4. // @description 牙刷科技! 让所有视频播放器网页全屏!默认快捷键ALT+1
  5. // @include *www.bilibili.com/video/av*
  6. // @include *www.iqiyi.com/*
  7. // @include *v.youku.com/*
  8. // @include *v.17173.com/*
  9. // @include *www.tudou.com/*
  10. // @include *.letv.com/*
  11. // @include *v.pptv.com/*
  12. // @include *tv.sohu.com/*
  13. // @include *v.ku6.com/*
  14. // @include *vod.kankan.com/*
  15. // @include *v.qq.com/*
  16. // @include *www.56.com/*
  17. // @include *live.yy.com/*
  18. // @include *www.acfun.com/v/ac*
  19. // @version 1.3
  20. // @grant none
  21. // @run-at document-start
  22. // ==/UserScript==
  23.  
  24. //若有需要可以自行添加域名,按照include的格式添加即可。
  25. //自行修改快捷键请参考81行注释。注意焦点在flash上时快捷键会失效。
  26.  
  27. var i, divArray, player = null, status = false, backStyle = new Array(), playerStyle, first = true;
  28.  
  29. document.addEventListener('DOMContentLoaded', init, false);
  30.  
  31. function init() {
  32. if (!checkPlayer()) {
  33. console.log('延迟2秒重新检测');
  34. var t = setTimeout(function() {
  35. checkPlayer();
  36. }, 2000);
  37. }
  38. }
  39.  
  40. function checkPlayer() {
  41. var embedArray = document.getElementsByTagName('embed');
  42. console.log('embed数量' + embedArray.length);
  43. if (embedArray.length > 0) {
  44. for (i = 0; i < embedArray.length; i++) {
  45. console.log('embed播放器检测' + i);
  46. if (embedArray[i].getAttribute('allowfullscreen') && embedArray[i].offsetWidth > 500) {
  47. player = embedArray[i];
  48. console.log('找到embed播放器');
  49. break;
  50. }
  51. }
  52. }
  53. if (!player) {
  54. console.log('未找到embed播放器');
  55. var objectArray = document.getElementsByTagName('object');
  56. if (objectArray.length > 0) {
  57. console.log('object数量' + objectArray.length);
  58. objectloop: for (i = 0; i < objectArray.length; i++) {
  59. console.log('object播放器检测' + i);
  60. if (objectArray[i].offsetWidth > 500) {
  61. var paramArray = objectArray[i].getElementsByTagName('param');
  62. var j;
  63. for (j = 0; j < paramArray.length; j++) {
  64. if (paramArray[j].name.toLowerCase() == 'allowfullscreen') {
  65. player = objectArray[i];
  66. console.log('找到object播放器');
  67. break objectloop;
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }
  74. if (!player) {
  75. console.log('未找到object播放器');
  76. return false;
  77. } else {
  78. console.log('创建按钮&生成快捷键');
  79. createButton();
  80. window.addEventListener("keydown", function(e) {
  81. // 默认快捷键为alt + 1, 全屏/恢复。altkey是按键ALT,keyCode=49是按键1,需要修改为其他快捷键的请搜索"keycode",修改为按键对应的数字。
  82. if (e.altKey && e.keyCode == 49) {
  83. playerControl();
  84. }
  85. }, false);
  86. return true;
  87. }
  88. }
  89.  
  90. function createButton() {
  91. var button = document.createElement('span');
  92. button.id = 'fullStackButton';
  93. button.style = 'position:fixed;width:1px;height:100%;top:0;left:0;z-index:33333;';
  94. button.onclick = function() {
  95. playerControl();
  96. };
  97. document.body.appendChild(button);
  98. }
  99.  
  100. function playerControl() {
  101. if (!status) {
  102. status = true;
  103. fullWin();
  104. } else {
  105. status = false;
  106. smallWin();
  107. }
  108. }
  109.  
  110. function fullWin() {
  111. if (first) {
  112. first = false;
  113. var full = player;
  114. do {
  115. if (full.getAttribute) {
  116. full.setAttribute('full_stack', true);
  117. }
  118. } while (full = full.parentNode);
  119. divArray = document.getElementsByTagName('div');
  120. playerStyle = {
  121. width: player.style.width,
  122. height: player.style.height,
  123. position: player.style.position,
  124. margin: player.style.margin,
  125. padding: player.style.padding,
  126. left: player.style.left,
  127. zIndex: player.parentNode.style.zIndex
  128. };
  129. console.log(playerStyle);
  130. }
  131. for (i = 0; i < divArray.length; i++) {
  132. if (divArray[i].getAttribute) {
  133. if (divArray[i].getAttribute('full_stack')) {
  134. backStyle[i] = {
  135. width: divArray[i].style.width,
  136. height: divArray[i].style.height,
  137. position: divArray[i].style.position,
  138. margin: divArray[i].style.margin,
  139. padding: divArray[i].style.padding,
  140. background: divArray[i].style.background
  141. };
  142. divArray[i].style.width = '100%';
  143. divArray[i].style.height = '100%';
  144. divArray[i].style.position = 'fixed';
  145. divArray[i].style.margin = '0 0';
  146. divArray[i].style.padding = '0 0';
  147. divArray[i].style.background = '#000';
  148. } else {
  149. backStyle[i] = divArray[i].style.display;
  150. divArray[i].style.display = 'none';
  151. }
  152. }
  153. }
  154. player.style.width = '100%';
  155. player.style.height = '100%';
  156. player.style.position = 'fixed';
  157. player.style.margin = '0 0';
  158. player.style.padding = '0 0 0 1px';
  159. player.style.left = '0';
  160. player.parentNode.style.zIndex = '22222';
  161. console.log('网页全屏完成');
  162. }
  163.  
  164. function smallWin() {
  165. for (i = 0; i < divArray.length; i++) {
  166. if (divArray[i].getAttribute) {
  167. if (divArray[i].getAttribute('full_stack')) {
  168. divArray[i].style.width = backStyle[i].width;
  169. divArray[i].style.height = backStyle[i].height;
  170. divArray[i].style.position = backStyle[i].position;
  171. divArray[i].style.margin = backStyle[i].margin;
  172. divArray[i].style.padding = backStyle[i].padding;
  173. divArray[i].style.background = backStyle[i].background;
  174. } else {
  175. divArray[i].style.display = backStyle[i];
  176. }
  177. }
  178. }
  179. player.style.width = playerStyle.width;
  180. player.style.height = playerStyle.height;
  181. player.style.position = playerStyle.position;
  182. player.style.margin = playerStyle.margin;
  183. player.style.padding = playerStyle.padding;
  184. player.style.left = playerStyle.left;
  185. player.parentNode.style.zIndex = playerStyle.zIndex;
  186. console.log('恢复完成');
  187. }