// ==UserScript==
// @name Auto Full HD Resolution for Viu.com Video Playing
// @version 1.0a1
// @description The script is to auto click the "全高清" Button for viu.com videos (only avaliable for paid member)
// @match https://www.viu.com/ott/*/vod/*
// @icon https://www.google.com/s2/favicons?domain=viu.com
// @grant unsafeWindow
// @grant window.onurlchange
// @namespace https://greasyfork.org/users/371179
// ==/UserScript==
(function $$(uWin) {
'use strict';
var lastPlayingStatus = null;
var settings = {}
var _userPaused = null;
Object.defineProperty(settings, 'userPaused', {
get() {
return _userPaused;
},
set(nv) {
_userPaused = nv;
console.log('[viu] userPaused: ', _userPaused);
}
})
if (!document || !document.documentElement) return window.requestAnimationFrame($$);
function getVideoFromEvent(evt) {
return (evt && evt.target && evt.target.nodeName == 'VIDEO') ? evt.target : null;
}
function noAutoStart(evt) {
const video = getVideoFromEvent(evt);
if (video) {
if (settings.userPaused === true) {
video.autoplay = false;
video.pause();
} else {
if (lastPlayingStatus === true && document.hidden === true) {} else if (document.hidden === true) {
video.autoplay = false;
video.pause();
}
}
}
}
var fn = function() {
this.setAttribute('__userscript_viu_force_selected', 'true')
};
var cid = 0;
var mDate = 0;
var cid2 = 0;
const TIMEOUT = 8000; // just in case DOM is not found
var gn = function(evt) {
const video = getVideoFromEvent(evt);
if (!video) return;
if (video) {
if (cid2 > 0) clearTimeout(cid2)
cid2 = setTimeout(function() {
if (video.hasAttribute('_viu_js_hooked')) return;
video.setAttribute('_viu_js_hooked', '')
video.addEventListener('playing', function() {
if (settings.userPaused === true) settings.userPaused = false;
}, true)
video.addEventListener('pause', function() {
if (settings.userPaused === null || settings.userPaused === false) settings.userPaused = true;
}, true)
}, 800);
}
noAutoStart(evt);
mDate = +new Date + TIMEOUT;
var jn = function(btn1080) {
//button is found
const bool = btn1080.matches(':not([aria-disabled=""]):not([aria-disabled="true"]):not([aria-checked="true"]):not([aria-checked=""])');
if (bool) {
const menuBtns = btn1080.parentNode.querySelectorAll('.vjs-menu-item[data-r]');
btn1080.click();
queueMicrotask(() => {
for (const menuBtn of menuBtns) menuBtn.addEventListener("click", fn, true)
})
}
}
var zn = function() {
//query when the video is loading/loaded/ready...
if (cid > 0 && mDate < +new Date) {
cid = clearInterval(cid);
return;
}
var btn1080 = document.querySelector('.vjs-menu-item[data-r="1080"]');
if (!btn1080) return;
if (cid > 0) cid = clearInterval(cid);
if (btn1080.matches('[__userscript_viu_loaded]')) return true;
btn1080.setAttribute('__userscript_viu_loaded', 'true');
window.requestAnimationFrame(() => jn(btn1080)); // prevent too fast
}
if (cid > 0) cid = clearInterval(cid);
if (!zn()) cid = setInterval(zn, 33);
}
document.addEventListener('loadstart', gn, true)
document.addEventListener('durationchange', gn, true)
document.addEventListener('loadedmetadata', gn, true)
document.addEventListener('loadeddata', gn, true)
document.addEventListener('progress', gn, true)
document.addEventListener('canplay', gn, true)
document.addEventListener('canplaythrough', gn, true)
const detection1 = function() {
console.log('[viu] viu.com reloaded url - detection #1')
}
const detection2 = function() {
console.log('[viu] viu.com reloaded url - detection #2')
}
const detection3 = function(info) {
console.log('[viu] viu.com reloaded url - detection #3', info)
}
if (window.onurlchange === null) {
// feature is supported
window.addEventListener('urlchange', (info) => {
detection3(info);
}, true);
}
function loader(detection) {
return function() {
let oldHref = document.location.href,
bodyDOM = document.querySelector("body");
const observer = new MutationObserver(function(mutations) {
if (oldHref != document.location.href) {
oldHref = document.location.href;
detection();
window.requestAnimationFrame(function() {
let tmp = document.querySelector("body");
if (tmp != bodyDOM) {
bodyDOM = tmp;
observer.observe(bodyDOM, config);
}
})
}
});
const config = {
childList: true,
subtree: true
};
observer.observe(bodyDOM, config);
}
}
window.addEventListener("load", loader(detection1), true);
uWin.addEventListener("load", loader(detection2), true);
uWin.addEventListener("beforeunload", (event) => {
console.log('[viu] viu.com reloaded url - detection #4')
// Cancel the event
//event.preventDefault();
//return (event.returnValue = ""); // Legacy method for cross browser support
}, true);
if (typeof document.hidden !== "undefined") {
document.addEventListener("visibilitychange", function handleVisibilityChange() {
const video = document.querySelector('video#viu-player_html5_api') || document.querySelector('video#viu-player_html5_api');
if (!video) lastPlayingStatus = null;
if (document.hidden === false) {
console.log('[viu] page show')
} else if (document.hidden === true) {
console.log('[viu] page hide')
if (video) lastPlayingStatus = !video.paused
} else {
lastPlayingStatus = null;
}
}, true);
}
// Your code here...
})(unsafeWindow || window);