webview.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. function main(item) {
  2. let url = item.url;
  3. let id = ku9.getQuery(url, "id"); // 从URL中获取id参数
  4. // 统一使用桌面端User-Agent
  5. let headers = {
  6. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36'
  7. };
  8. // 将注入到网页中的JavaScript代码
  9. const jscode = `(function(){
  10. const startTime = Date.now();
  11. // 设置页面基本样式
  12. document.documentElement.style.backgroundColor = 'black';
  13. document.documentElement.style.height = '10%';
  14. document.documentElement.style.margin = '0';
  15. document.documentElement.style.padding = '0';
  16. document.body.style.visibility = 'hidden';
  17. document.body.style.margin = '0';
  18. document.body.style.padding = '0';
  19. document.body.style.minHeight = '10vh';
  20. // 在Shadow DOM中寻找video元素
  21. function getVideoParentShadowRoots() {
  22. const allElements = document.querySelectorAll('*');
  23. for(const element of allElements) {
  24. const shadowRoot = element.shadowRoot;
  25. if(shadowRoot) {
  26. const video = shadowRoot.querySelector('video');
  27. if(video) return video;
  28. }
  29. }
  30. return null;
  31. }
  32. // 移除视频控制条
  33. function removeControls() {
  34. ['#control_bar', '.controls', '.vjs-control-bar', 'xg-controls'].forEach(selector => {
  35. document.querySelectorAll(selector).forEach(e => e.remove());
  36. });
  37. }
  38. // 设置视频播放器
  39. function setupVideo(video) {
  40. const container = document.createElement('div');
  41. container.style.position = 'fixed';
  42. container.style.top = '0';
  43. container.style.left = '0';
  44. container.style.width = '100vw';
  45. container.style.height = '100vh';
  46. container.style.zIndex = '2147483647';
  47. container.style.backgroundColor = 'black';
  48. video.style.width = '100%';
  49. video.style.height = '100%';
  50. video.style.objectFit = 'fill';
  51. video.style.transform = 'translateZ(0)';
  52. container.appendChild(video);
  53. document.body.appendChild(container);
  54. document.body.style.overflow = 'hidden';
  55. document.documentElement.style.overflow = 'hidden';
  56. // 进入全屏模式
  57. const enterFullscreen = () => {
  58. if(container.requestFullscreen) {
  59. container.requestFullscreen();
  60. } else if(container.webkitRequestFullscreen) {
  61. container.webkitRequestFullscreen();
  62. }
  63. const fullscreenStyle = () => {
  64. video.style.objectFit = 'contain';
  65. container.style.width = '100%';
  66. container.style.height = '100%';
  67. };
  68. container.addEventListener('fullscreenchange', fullscreenStyle);
  69. video.muted = false;
  70. video.volume = 1;
  71. video.playsInline = false;
  72. video.setAttribute('playsinline', 'false');
  73. try {
  74. video.play();
  75. } catch(e) {
  76. video.muted = true;
  77. video.play();
  78. }
  79. };
  80. setTimeout(enterFullscreen, 300);
  81. }
  82. // 检查视频元素
  83. function checkVideo() {
  84. if(Date.now() - startTime > 15000) { // 15秒超时
  85. clearInterval(interval);
  86. document.body.style.visibility = 'visible';
  87. document.documentElement.style.visibility = 'visible';
  88. return;
  89. }
  90. let video = document.querySelector('video') || getVideoParentShadowRoots();
  91. if(video && video.readyState > 0) {
  92. clearInterval(interval);
  93. removeControls();
  94. setupVideo(video);
  95. if(video.requestFullscreen) {
  96. video.requestFullscreen();
  97. } else if(video.webkitRequestFullscreen) {
  98. video.webkitRequestFullscreen();
  99. }
  100. document.body.style.visibility = 'visible';
  101. document.documentElement.style.visibility = 'visible';
  102. }
  103. }
  104. const interval = setInterval(checkVideo, 100);
  105. })();`;
  106. return {
  107. webview: id, // 网页视图使用的URL路径
  108. headers: headers, // 请求头信息
  109. jscode: jscode // 要注入的JavaScript代码
  110. };
  111. }