影视网页标题清理

从疑似影视网页的标题中提取片名与集数重新显示

// ==UserScript==
// @name         影视网页标题清理
// @version      11
// @description  从疑似影视网页的标题中提取片名与集数重新显示
// @author       Lemon399
// @match        *://*/*
// @grant        none
// @namespace https://greasyfork.org/users/452911
// ==/UserScript==

(() => {
  function matcher(title) {
    let result = "",
      ji = NaN,
      temp = title;
    /* 匹配 [数字]集 */
    const ji1 = title.match(/(\d+)集/);
    /* 如果存在 */
    if (ji1) {
      /* 取 [数字] 为集数 */
      ji = ji1[1];
      /* 删除 [数字]集 以及后面所有内容 */
      temp = title.split(ji + "集")[0];
      /* 如果末尾是 第,删除 第 */
      if (temp.match(/第$/)) {
        temp = temp.replace(/第$/, "");
      }
    }
    /* 如果上面没有得到集数,匹配 》[空格][数字] 为集数 */
    const ji2 = title.match(/》\s*(\d+)/);
    if (!ji && ji2) ji = ji2[1];
    /* 删除 《 以及前面内容 和 》以及后面内容,清理多余空格 */
    temp = temp.replace(/^.*《/, "").replace(/》.*$/, "").trim();
    result = temp + (ji ? " " + ji.padStart(2, "0") : "");
    return result ? result : title;
  }
  document.title = matcher(document.title);
})();