Advertisement
rustfoot

w2g Auto English CC 2.0

Oct 16th, 2021
342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name     Auto-English CC
  3. // @author   Paperlanty
  4. // @version  2021.10
  5. // @grant    none
  6. // @include  https://w2g.tv/*
  7. // ==/UserScript==
  8.  
  9. // console.log('script');
  10. window.addEventListener('load', function () {
  11.   console.log('auto english CC loaded');
  12.   var element = document.querySelector("#w2g-video");
  13.  
  14.   var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
  15.   var observer = new MutationObserver(initializeIframeObserver);
  16.   observer.observe(element, {
  17.       childList: true,
  18.     characterData: true
  19.   });
  20.  
  21.   if (document.querySelector("#w2g-video iframe"))
  22.     setSubtitle();
  23.  
  24. });
  25.  
  26. function initializeIframeObserver() {
  27. //   console.log('initializeIframeObserver');
  28.   let iframe = document.querySelector("#w2g-video iframe");
  29.  
  30.   var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
  31.   var iframeObserver = new MutationObserver(setSubtitle);
  32.   iframeObserver.observe(iframe, {
  33.       attributeFilter: ['src'],
  34.     characterData: true
  35.   });
  36.  
  37.   setSubtitle();
  38. }
  39.  
  40. function setSubtitle() {
  41. //     console.log('setSubtitle');
  42.     let iframe = document.querySelector("#w2g-video iframe");
  43.     let englishOption = [];
  44.  
  45.     setTimeout(function() {
  46. //         console.log('waitForSubtitles');
  47.         var waitForSubtitles = new Promise((resolve, reject) => {
  48.             let timeWas = new Date();
  49.             let wait = setInterval(function() {
  50.                 if (contains(iframe.contentWindow.document, '#subtitle_button div.popup_item', 'English').length > 0) {
  51.                     console.log("resolved after", new Date() - timeWas, "ms");
  52.                     clearInterval(wait);
  53.                     englishOption = contains(iframe.contentWindow.document, '#subtitle_button div.popup_item', 'English');
  54.                     englishOption[0].click();
  55.                     console.log('subtitle set');
  56.                     resolve();
  57.                 } else if (new Date() - timeWas > 5000) { // Timeout
  58.                     console.log("rejected after", new Date() - timeWas, "ms");
  59.                     clearInterval(wait);
  60.                     reject();
  61.                 }
  62.             }, 20); // ms interval per subtitle check
  63.         }, 5000); // ms timeout for checking for subtitles
  64.     }, 1000); // ms delay after video load
  65. }
  66.  
  67. function contains(doc, selector, text) {
  68.   var elements = doc.querySelectorAll(selector);
  69.   return Array.prototype.filter.call(elements, function(element) {
  70.     return RegExp(text).test(element.textContent);
  71.   });
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement