video fullpage

2024-7-12 18:05:52

当前为 2024-07-14 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        video fullpage
// @namespace   github.q962
// @match       https://*/*
// @version     1.5
// @author      q962
// @grant       GM_registerMenuCommand
// @grant       GM_addStyle
// @grant       GM_deleteValue
// @grant       GM_deleteValues
// @grant       GM_setValue
// @grant       GM_setValues
// @grant       GM_getValue
// @grant       GM_getValues
// @grant       GM_listValues
// @grant       GM_addElement
// @license     MIT

// @description 2024-7-12 18:05:52
// ==/UserScript==

let global_css = `
.__visiable_true.__level_up {
  display: block !important;
  width: 100% !important;
  height: 100% !important;
  max-width: unset !important;
  max-height: unset !important;
  margin: unset !important;
}

.__hidden_all > :not(.__visiable_true, .__dialog) {
  display: none !important;
  padding: 0 !important;
  margin: 0 !important;
}

.__dialog {
  position: fixed;
  width: 600px;
  height: auto;
  top: 30vh;
  z-index: 99999;
  background: white;
  border: 1px #aaa solid;
  border-radius: 5px;
  padding: 10px;
  left: calc( ( 100vw - 600px ) / 2 );
  box-shadow: 0px 4px 8px 0px black;
}
.__dialog > *, .__dialog > h1 {
  color: black;
  width: 100%;

}
.__dialog input {
  width: 90%;
}
`;

GM_addStyle(global_css);

///////////////////////////////

var all_video_elems = [];

var upCount = get_upCount();
var current_video_elem = null;

/////////////////////////////////////////////////////////

var count_dialog = GM_addElement('div');
count_dialog.classList.add("__dialog");
count_dialog.innerHTML = `
  <h1>设置上层层数</h1>
  <label>正则:<input id="regex"/></label><br/>
  <label>层数:<input id="count" min=0 max=15 type="number"  value=0 /></label><br/>
  <br/>
  <button id="ok">确定</button>
  <button id="cancel">取消</button>
  <p id="msg"></p>
`;
count_dialog.hidden = true;

var regex_elem = count_dialog.querySelector("#regex");
var count_elem = count_dialog.querySelector("#count");
var msg_elem = count_dialog.querySelector("#msg");

count_dialog.querySelector("#ok").addEventListener("click", ()=>{
  let regex_str = regex_elem.value;
  let count = count_elem.value;

  if( !regex_str ) return;
  try{
    new RegExp(regex_str);
  } catch(error){
    msg_elem.innerText = error;
    return;
  }

  if( count>15 || count < 0 ){
    msg_elem.innerText = "0 <= 层数  <=15";
    return;
  }

  GM_setValue(location.origin, JSON.stringify({regex: regex_str, count: count}));

  count_dialog.hidden = true;

  upCount = count;

});

count_dialog.querySelector("#cancel").addEventListener("click", ()=>{
  count_dialog.hidden = true;
});

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function get_upCount(){
  let j = JSON.parse(GM_getValue(location.origin)||null);
  if(!j) return 0;

  if( j.regex ) {
    try{
      let regex = new RegExp(j.regex);
      if( !regex.test(location.href) ) return 0;
   } catch(error){return 0;}
  }

  return parseInt(j.count);
}

function settings(){
  count_elem.value = 0;
  regex_elem.value = (location.origin+location.pathname).replaceAll(".","\\.");

  let j = JSON.parse(GM_getValue(location.origin)||null);
  if( j ) {
    if( j.regex ) regex_elem.value = j.regex;
    if( j.count ) count_elem.value = j.count;
  }

  msg_elem.innerText = "";

  count_dialog.hidden = false;
}

function do_full(target){

  let _upCount = upCount;

  do {
      target = target.parentElement;
  } while( --_upCount >= 0 );

  let action = '';
  if(target.classList.contains("__visiable_this")){
    action = 'remove';
  } else{
    action = 'add';
  }

  target.classList[action]("__visiable_this");

  let p = target;
  while(p){

    p.classList[action]("__visiable_true");
    p.classList[action]("__level_up");

    if( p.parentElement )
      p.parentElement.classList[action]("__hidden_all");

    p = p.parentElement;
  }
}

function try_iframe(){
  let playing_href = GM_getValue("playing_href");

  let all_iframes = document.querySelectorAll("iframe");
  for(let i=0;i<all_iframes.length;i++){
    let frame = all_iframes[i];

    if( playing_href == frame.src ) {
      do_full(frame);
      return;
    }
  }
}

function try_find(){
  let all = document.querySelectorAll("video");
  for(let i=0;i < all.length;i++){
    let videl_elem = all[i];

    if( videl_elem.currentTime > 0 && !videl_elem.paused && !videl_elem.ended ) {
      do_full(videl_elem);
      return true;
    }
  }
  return false;
}

function set_fullpage(){
  if(!current_video_elem) {
    try_find() || try_iframe();
    return;
  }

  do_full( current_video_elem );
}

GM_registerMenuCommand('全页', set_fullpage);
GM_registerMenuCommand('设置', settings);

//////////////////////////////////////////////////////////////////////////////

function bind_evnet(elem){
  elem.addEventListener('play',  function(e) {
    // 记录当前的 location,用于判读 iframe
    GM_setValue("playing_href", location.href);
    current_video_elem = e.target;
  });

  elem.addEventListener('pause', function (e) {
  });
}

function findingVideoElem(){

  let video_elems = document.querySelectorAll("video");

  for( let index=0; index < video_elems.length; index++){
    let video_elem = video_elems[index];

    if( !( video_elem in all_video_elems )) {
      bind_evnet( video_elem );
      all_video_elems.push( video_elem );
    }
  }
}

setInterval(findingVideoElem, 2000);