视频网页全屏

让所有视频网页全屏

目前为 2017-06-16 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Maximize Video
  3. // @name:zh-CN 视频网页全屏
  4. // @namespace http://www.icycat.com
  5. // @description Maximize all video players
  6. // @description:zh-CN 让所有视频网页全屏
  7. // @author 冻猫
  8. // @include *
  9. // @exclude *www.w3school.com.cn*
  10. // @version 9.4.3
  11. // @grant unsafeWindow
  12. // @run-at document-end
  13. // ==/UserScript==
  14.  
  15. (function() {
  16.  
  17. 'use strict';
  18.  
  19. var fullStatus = false,
  20. isIframe = false,
  21. isFullIframePlayer = false,
  22. isRbtn = true,
  23. parentArray = new Array(),
  24. backStyle = new Object(),
  25. mouse = {
  26. leave: 'listener',
  27. over: 'listener'
  28. },
  29. browser, btnText, player, controlBtn, leftBtn, rightBtn;
  30.  
  31. //Html5规则[overlay|到player节点数],适用于自适应大小HTML5播放器
  32. var html5Rules = {
  33. 'www.dailymotion.com': ['#player video|4', '#player .dmp_VideoView|2'],
  34. 'v.youku.com': ['#player video|4', '#ykPlayer .spv_shadow>div>div|4'],
  35. 'www.iqiyi.com': ['#flashbox video|2'],
  36. 'www.youtube.com': ['#player-api video|3', '#c4-player video|2'],
  37. 'www.twitch.tv': ['#player .js-control-fullscreen-overlay|1'],
  38. 'www.huya.com': ['#player-video|3'],
  39. 'www.bilibili.com': ['#bilibiliPlayer video|4', '.bilibili-player-info|2'],
  40. 'www.pornhub.com': ['.mhp1138_eventCatcher|2'],
  41. 'www.redtube.com': ['.mhp1138_eventCatcher|1'],
  42. 'www.youporn.com': ['.mhp1138_eventCatcher|1'],
  43. 'www.icourse163.org': ['.ux-video-player .bbg|3'],
  44. 'www.panda.tv': ['#room-player-video-danmu div|3'],
  45. 'vk.com': ['.videoplayer_media|1'],
  46. 'www.douyu.com': ['.danmu-wrap video|2'],
  47. 'store.steampowered.com': ['#highlight_player_area video|2'],
  48. 'vimeo.com': ['.player .target|1'],
  49. 'ecchi.iwara.tv': ['#video-player video|1'],
  50. 'live.bilibili.com': ['.bilibili-live-player-video-danmaku|2', '.bilibili-live-player-video-gift|2'],
  51. 'v.qq.com': ['#tenvideo_player .txp_shadow|3', '#video_container_body .txp_shadow|3']
  52. };
  53.  
  54. //iframe播放器显示按钮规则
  55. //iframe关键字id、classname、src
  56. var iframeRules = /play|youtube\.com\/embed|video|movie|anime|flv|mp4/i;
  57. //网站域名
  58. var iframeUrlRules = [
  59. 'kisshentai.net',
  60. 'www.watchseries.ac',
  61. 'www.panda.tv',
  62. 'animeflv.net',
  63. 'www.vodlocker.city',
  64. 'projectwatchseries.com',
  65. 'reyanime.com'
  66. ];
  67.  
  68. //自动缩放内层内播放器规则
  69. var fullIframePlayerRules = [
  70. 'newplayer.jfrft.com',
  71. 'player.005.tv',
  72. 'player.xcmh.cc',
  73. 'www.auroravid.to',
  74. 'www.mp4upload.com',
  75. 'vodlocker.com',
  76. '52dongm.duapp.com'
  77. ];
  78.  
  79. //flash游戏页面,不在flash上显示还原按钮
  80. var excludeRbtnRules = [
  81. 'www.dmm.com',
  82. 'www.dmm.co.jp',
  83. 'www.4399.com',
  84. 'www.3366.com',
  85. 'flash.17173.com',
  86. 'www.7k7k.com'
  87. ];
  88.  
  89. if (excludeRbtnRules.indexOf(document.location.hostname) != -1) {
  90. isRbtn = false;
  91. }
  92.  
  93. if (window.top !== window.self) {
  94. isIframe = true;
  95. }
  96.  
  97. if (navigator.language.toLocaleLowerCase() == 'zh-cn') {
  98. btnText = {
  99. out: '网页全屏',
  100. inner: '内层全屏',
  101. restore: '还原大小'
  102. };
  103. } else {
  104. btnText = {
  105. out: 'Maximize',
  106. inner: 'M<br/>A<br/>X',
  107. restore: 'Restore'
  108. };
  109. }
  110.  
  111. if (/Firefox/i.test(navigator.userAgent)) {
  112. browser = 'firefox';
  113. } else if (/Chrome/i.test(navigator.userAgent)) {
  114. browser = 'chrome';
  115. } else {
  116. browser = 'other';
  117. }
  118.  
  119. var createButton = function(id) {
  120. var btn = document.createElement('tbdiv');
  121. btn.id = id;
  122. btn.onclick = function() {
  123. maximize.playerControl();
  124. };
  125. document.body.appendChild(btn);
  126. return btn;
  127. };
  128.  
  129. var tool = {
  130. getRect: function(element) {
  131. var rect = element.getBoundingClientRect();
  132. var scroll = tool.getScroll();
  133. return {
  134. pageX: rect.left + scroll.left,
  135. pageY: rect.top + scroll.top,
  136. screenX: rect.left,
  137. screenY: rect.top
  138. };
  139. },
  140. isFullClient: function(element) {
  141. var client = tool.getClient();
  142. var rect = tool.getRect(element);
  143. if (Math.abs(client.width - element.offsetWidth) < 21 && Math.abs(client.height - element.offsetHeight) < 21 && rect.screenY < 10 && rect.screenX < 20) {
  144. return true;
  145. } else {
  146. return false;
  147. }
  148. },
  149. isHtml5FullClient: function(element) {
  150. var client = tool.getClient();
  151. var rect = tool.getRect(element);
  152. var w = client.width - element.offsetWidth;
  153. var h = client.height - element.offsetHeight;
  154. if (w >= 0 && h >= 0) {
  155. if ((w < 21 && rect.screenX < 20) || (h < 21 && rect.screenY < 10)) {
  156. return true;
  157. } else {
  158. return false;
  159. }
  160. } else {
  161. return false;
  162. }
  163.  
  164. },
  165. getScroll: function() {
  166. return {
  167. left: document.documentElement.scrollLeft || document.body.scrollLeft,
  168. top: document.documentElement.scrollTop || document.body.scrollTop
  169. };
  170. },
  171. getClient: function() {
  172. return {
  173. width: document.compatMode == 'CSS1Compat' ? document.documentElement.clientWidth : document.body.clientWidth,
  174. height: document.compatMode == 'CSS1Compat' ? document.documentElement.clientHeight : document.body.clientHeight
  175. };
  176. },
  177. addStyle: function(css) {
  178. var style = document.createElement('style');
  179. style.type = 'text/css';
  180. var node = document.createTextNode(css);
  181. style.appendChild(node);
  182. document.head.appendChild(style);
  183. return style;
  184. }
  185. };
  186.  
  187. var setButton = {
  188. init: function() {
  189. //防止页面脚本干扰,重新初始化样式
  190. if (!document.getElementById('playerControlBtn')) {
  191. init();
  192. }
  193. if (isIframe && !isFullIframePlayer && fullIframePlayerRules.indexOf(document.location.hostname) != -1) {
  194. if (player.nodeName == 'OBJECT' || player.nodeName == 'EMBED') {
  195. maximize.checkParent();
  196. maximize.addClass();
  197. tool.addStyle('#htmlToothbrush #bodyToothbrush .playerToothbrush {left:0px !important;width:100vw !important;}');
  198. isFullIframePlayer = true;
  199. }
  200. }
  201. if (tool.isFullClient(player) || isFullIframePlayer) {
  202. return;
  203. }
  204. if (isIframe && player.nodeName == 'VIDEO' && tool.isHtml5FullClient(player)) {
  205. return;
  206. }
  207. this.show();
  208. },
  209. show: function() {
  210. try {
  211. player.addEventListener('mouseleave', handle.leavePlayer, false);
  212. } catch (e) {
  213. mouse.leave = player.onmouseleave;
  214. player.onmouseleave = function() {
  215. handle.leavePlayer();
  216. player.onmouseleave = mouse.leave;
  217. };
  218. }
  219. if (!fullStatus) {
  220. document.addEventListener('scroll', handle.scrollFix, false);
  221. }
  222. controlBtn.style.display = 'block';
  223. controlBtn.style.visibility = 'visible';
  224. this.locate();
  225. },
  226. locate: function() {
  227. var playerRect = tool.getRect(player);
  228. if (playerRect.pageY < 20 || fullStatus) {
  229. if (fullStatus) {
  230. controlBtn.classList.remove('playerControlBtnCol');
  231. playerRect.screenY = playerRect.screenY + 50;
  232. playerRect.screenX = playerRect.screenX - 30;
  233. controlBtn.innerHTML = btnText.restore;
  234. } else {
  235. playerRect.screenY = playerRect.screenY + 20;
  236. if (Math.abs(tool.getClient().width - player.offsetWidth) < 21 && Math.abs(tool.getClient().height - player.offsetHeight) > 21) {
  237. playerRect.screenX = playerRect.screenX + 44;
  238. } else {
  239. playerRect.screenX = playerRect.screenX + 64;
  240. }
  241. controlBtn.classList.add('playerControlBtnCol');
  242. if (isIframe) {
  243. controlBtn.innerHTML = btnText.inner;
  244. } else {
  245. controlBtn.innerHTML = btnText.out;
  246. }
  247. }
  248. if (browser == 'firefox' && fullStatus) {
  249. controlBtn.style.opacity = '1';
  250. } else {
  251. controlBtn.style.opacity = '0.5';
  252. }
  253. } else {
  254. controlBtn.classList.remove('playerControlBtnCol');
  255. controlBtn.style.opacity = '0.5';
  256. controlBtn.innerHTML = btnText.out;
  257. }
  258. controlBtn.style.top = playerRect.screenY - 20 + 'px';
  259. controlBtn.style.left = playerRect.screenX - 64 + player.offsetWidth + 'px';
  260. }
  261. };
  262.  
  263. var handle = {
  264. getPlayer: function(e) {
  265. if (fullStatus) {
  266. return;
  267. }
  268. var target = e.target;
  269. if (html5Rules[document.location.hostname]) {
  270. var overlay = [],
  271. playerNum = [];
  272. var overlayRules = html5Rules[document.location.hostname];
  273. for (var i = 0; i < overlayRules.length; i++) {
  274. var rules = overlayRules[i].split('|');
  275. overlay[i] = document.querySelector(rules[0]);
  276. playerNum[i] = rules[1];
  277. }
  278. if (overlay.indexOf(target) != -1) {
  279. var html5Player = target;
  280. for (var i = 0; i < playerNum[overlay.indexOf(target)]; i++) {
  281. html5Player = html5Player.parentNode;
  282. }
  283. player = html5Player;
  284. setButton.init();
  285. return;
  286. }
  287. }
  288. switch (target.nodeName) {
  289. case 'IFRAME':
  290. if (!iframeRules.test(target.className) && !iframeRules.test(target.src) && !iframeRules.test(target.id) && !iframeRules.test(document.location.href) && iframeUrlRules.indexOf(document.location.hostname) == -1) {
  291. handle.leavePlayer();
  292. break;
  293. }
  294. case 'OBJECT':
  295. case 'EMBED':
  296. case 'VIDEO':
  297. if (target.offsetWidth > 99 && target.offsetHeight > 99) {
  298. player = target;
  299. setButton.init();
  300. }
  301. break;
  302. default:
  303. handle.leavePlayer();
  304. }
  305. },
  306. leavePlayer: function() {
  307. if (controlBtn.style.visibility == 'visible') {
  308. controlBtn.style.opacity = '';
  309. controlBtn.style.visibility = '';
  310. try {
  311. player.removeEventListener('mouseleave', handle.leavePlayer, false);
  312. } catch (e) {}
  313. document.removeEventListener('scroll', handle.scrollFix, false);
  314. }
  315. },
  316. scrollFix: function(e) {
  317. clearTimeout(backStyle.scrollFixTimer);
  318. backStyle.scrollFixTimer = setTimeout(function() {
  319. setButton.locate();
  320. }, 20);
  321. },
  322. hotKey: function(e) {
  323. //默认退出键为ESC。需要修改为其他快捷键的请搜索"keycode",修改为按键对应的数字。
  324. if (e.keyCode == 27) {
  325. maximize.playerControl();
  326. }
  327. },
  328. restoreButton: function() {
  329. if (isIframe) {
  330. return;
  331. }
  332. switch (browser) {
  333. case 'chrome':
  334. if (window.outerWidth < window.screen.width - 10) {
  335. setButton.show();
  336. }
  337. break;
  338. case 'firefox':
  339. if (window.innerWidth < window.screen.width - 10) {
  340. setButton.show();
  341. }
  342. break;
  343. }
  344. }
  345. };
  346.  
  347. var maximize = {
  348. playerControl: function() {
  349. if (!player) {
  350. return;
  351. }
  352. this.checkParent();
  353. if (!fullStatus) {
  354. this.fullWin();
  355. } else {
  356. this.smallWin();
  357. }
  358. },
  359. checkParent: function() {
  360. parentArray = [];
  361. var full = player;
  362. while (full = full.parentNode) {
  363. if (full.nodeName == 'BODY') {
  364. break;
  365. }
  366. if (full.getAttribute) {
  367. parentArray.push(full);
  368. }
  369. }
  370. },
  371. fullWin: function() {
  372. if (!fullStatus) {
  373. document.removeEventListener('mouseover', handle.getPlayer, false);
  374. if (isRbtn) {
  375. try {
  376. player.addEventListener('mouseover', handle.restoreButton, false);
  377. } catch (e) {
  378. mouse.over = player.onmouseover;
  379. player.onmouseover = handle.restoreButton;
  380. }
  381. }
  382. backStyle = {
  383. htmlId: document.body.parentNode.id,
  384. bodyId: document.body.id
  385. };
  386. if (document.location.hostname == 'www.youtube.com' && !document.querySelector('.watch-stage-mode')) {
  387. document.getElementById('page').classList.add('watch-wide');
  388. document.getElementById('page').classList.add('watch-stage-mode');
  389. backStyle.ytbStageChange = true;
  390. }
  391. if (document.location.hostname == 'live.bilibili.com' && document.querySelector('.bilibili-live-player') && document.querySelector('.bilibili-live-player').getAttribute('data-player-state') != 'web-fullscreen') {
  392. unsafeWindow.$('.bilibili-live-player-video-danmaku').dblclick();
  393. backStyle.biliPlayerChange = true;
  394. }
  395. if (document.location.hostname == 'v.youku.com' && document.querySelector('.vpactionv5_iframe_wrap') && tool.getRect(player).pageY + player.offsetHeight - tool.getRect(document.querySelector('.vpactionv5_iframe_wrap')).pageY > 20) {
  396. player.style.cssText = 'height: calc(100vh + 50px) !important;';
  397. }
  398. if (document.location.hostname == 'www.tudou.com' && document.querySelector('.action_buttons.fix') && tool.getRect(player).pageY + player.offsetHeight - tool.getRect(document.querySelector('.action_buttons.fix')).pageY > 20) {
  399. player.style.cssText = 'height: calc(100vh + 55px) !important;';
  400. }
  401. leftBtn.style.display = 'block';
  402. rightBtn.style.display = 'block';
  403. controlBtn.style.display = '';
  404. this.addClass();
  405. }
  406. fullStatus = true;
  407. },
  408. addClass: function() {
  409. document.body.parentNode.id = 'htmlToothbrush';
  410. document.body.id = 'bodyToothbrush';
  411. for (var i = 0; i < parentArray.length; i++) {
  412. parentArray[i].classList.add('parentToothbrush');
  413. //父元素position:fixed会造成层级错乱
  414. if (getComputedStyle(parentArray[i]).position == 'fixed') {
  415. parentArray[i].classList.add('absoluteToothbrush');
  416. }
  417. }
  418. player.classList.add('playerToothbrush');
  419. if (player.nodeName == 'VIDEO') {
  420. backStyle.controls = player.controls;
  421. player.controls = true;
  422. }
  423. window.dispatchEvent(new Event('resize'));
  424. },
  425. smallWin: function() {
  426. if (isRbtn) {
  427. try {
  428. player.removeEventListener('mouseover', handle.restoreButton, false);
  429. } catch (e) {}
  430. if (mouse.over != 'listener') {
  431. player.onmouseover = mouse.over;
  432. }
  433. }
  434. document.body.parentNode.id = backStyle.htmlId;
  435. document.body.id = backStyle.bodyId;
  436. for (var i = 0; i < parentArray.length; i++) {
  437. parentArray[i].classList.remove('parentToothbrush');
  438. parentArray[i].classList.remove('absoluteToothbrush');
  439. }
  440. player.classList.remove('playerToothbrush');
  441. if (document.location.hostname == 'www.youtube.com' && backStyle.ytbStageChange) {
  442. document.getElementById('page').classList.remove('watch-wide');
  443. document.getElementById('page').classList.remove('watch-stage-mode');
  444. }
  445. if (document.location.hostname == 'live.bilibili.com' && backStyle.biliPlayerChange) {
  446. unsafeWindow.$('.bilibili-live-player-video-danmaku').dblclick();
  447. }
  448. if (document.location.hostname == 'v.youku.com' || document.location.hostname == 'www.tudou.com') {
  449. player.style.cssText = '';
  450. }
  451. if (player.nodeName == 'VIDEO') {
  452. player.controls = backStyle.controls;
  453. }
  454. leftBtn.style.display = '';
  455. rightBtn.style.display = '';
  456. controlBtn.style.display = '';
  457. document.addEventListener('mouseover', handle.getPlayer, false);
  458. window.dispatchEvent(new Event('resize'));
  459. fullStatus = false;
  460. }
  461. };
  462.  
  463. var init = function() {
  464. controlBtn = createButton('playerControlBtn');
  465. leftBtn = createButton('leftFullStackButton');
  466. rightBtn = createButton('rightFullStackButton');
  467. if (getComputedStyle(controlBtn).position != 'fixed') {
  468. tool.addStyle([
  469. '#htmlToothbrush, #bodyToothbrush {overflow:hidden !important;zoom:100% !important}',
  470. '#htmlToothbrush #bodyToothbrush .parentToothbrush {overflow:visible !important;z-index:auto !important;transform:none !important;-webkit-transform-style:flat !important;transition:none !important;contain:none !important;}',
  471. '#htmlToothbrush #bodyToothbrush .absoluteToothbrush {position:absolute !important;}',
  472. '#htmlToothbrush #bodyToothbrush .playerToothbrush {position:fixed !important;top:0px !important;left:1px !important;width:calc(100vw - 2px) !important;height:100vh !important;max-width:none !important;max-height:none !important;min-width:0 !important;min-height:0 !important;margin:0 !important;padding:0 !important;z-index:2147483645 !important;border:none !important;background-color:#000 !important;transform:none !important;}',
  473. '#playerControlBtn {visibility:hidden;opacity:0;display:none;transition: all 0.5s ease;cursor: pointer;font: 12px "微软雅黑";margin:0;width:64px;height:20px;line-height:20px;border:none;text-align: center;position: fixed;z-index:2147483646;background-color: #27A9D8;color: #FFF;} #playerControlBtn:hover {visibility:visible;opacity:1;background-color:#2774D8;}',
  474. '#playerControlBtn.playerControlBtnCol {width:20px;height:64px;line-height:16px;}',
  475. '#leftFullStackButton{display:none;position:fixed;width:1px;height:100vh;top:0;left:0;z-index:2147483646;background:#000;}',
  476. '#rightFullStackButton{display:none;position:fixed;width:1px;height:100vh;top:0;right:0;z-index:2147483646;background:#000;}'
  477. ].join('\n'));
  478. }
  479. };
  480.  
  481. init();
  482.  
  483. document.addEventListener('mouseover', handle.getPlayer, false);
  484. document.addEventListener('keydown', handle.hotKey, false);
  485.  
  486. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址