YouTube Embedder

Convert links and optionally URL text which points to a YouTube video, into embedded YouTube frame. For URL text, it can be converted to a link only.

目前為 2018-05-07 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         YouTube Embedder
// @namespace    https://greasyfork.org/en/users/85671-jcunews
// @description  Convert links and optionally URL text which points to a YouTube video, into embedded YouTube frame. For URL text, it can be converted to a link only.
// @author       jcunews
// @version      1.0.1
// @license      GNU AGPLv3
// @match        *://*/*
// @exclude      *://www.youtube.com/embed/*
// @grant        none
// ==/UserScript==

(function(xe, fe, ce, rx, trx, trxg, hrx, lo) {

  //===== CONFIGURATION BEGIN =====

  //The minimum width & height of the container element where the link or URL text is found, in order to apply the conversion.
  var minimumWidth  = 256;
  var minimumHeight = 144;

  //Embedded YouTube frame width in pixels (should be multiplication of 16).
  //If zero, the width is the container element width of the link or URL text, and the height is calculated using 16:9 ratio (widescreen video dimension).
  //If the final width is smaller than the minimum width setting, and if the source is an URL text, it will be converted to a link instead.
  var frameWidth = 0;

  //Enable URL text to YouTube frame converter (aside from links).
  var convertUrlText = true;

  //Convert to link only, when on these domains.
  var convertToLinkOnlyDomains = /www\.youtube\.com|somesite\.info|subnet\d+\.other\.net/i;

  //===== CONFIGURATION END =====

  function linkToFrame(node, m, pn, w, h, a) {
    if (node.attributes["ytebd_nocovert"] || !node.offsetWidth || !node.offsetHeight) return;
    if (!lo && (m = node.href.match(rx))) {
      m = "https://www.youtube.com/embed/" + m[1];
      pn = node.parentNode;
      while (pn) {
        if (pn.scrollHeight > pn.offsetHeight) break;
        pn = pn.parentNode;
      }
      h = ((pn || node.parentNode).offsetHeight * 0.9) >> 0;
      w = frameWidth > 0 ? frameWidth : node.parentNode.offsetWidth;
      if ((pn = (w / 16 * 9) >> 0) > h) {
        w = (h / 9 * 16) >> 0;
      } else h = pn;
      if ((w >= minimumWidth) && (h >= minimumHeight)) {
        c = document.createElement("IFRAME");
        c.src = m;
        c.allowFullscreen = true;
        c.referrerPolicy = "no-referrer";
        c.style.border = "none";
        c.width = w;
        c.height = h;
        c.setAttribute("ytebd_nocovert", "1");
        node.replaceWith(c);
      } else if (w && h) node.setAttribute("ytebd_nocovert", "1");
    } else node.setAttribute("ytebd_nocovert", "1");
  }

  function processNode(node, c, m, w, h, pn) {
    switch (node.nodeType) {
      case Node.ELEMENT_NODE:
      case Node.DOCUMENT_NODE:
        if ((node.attributes && node.attributes["ytebd_nocovert"])) break;
        if (node.nodeName === "A") {
          linkToFrame(node);
          node = null;
        }
        if (node && (xe.indexOf(node.nodeName) < 0)) {
          if (fe.indexOf(node.nodeName) >= 0) {
            m = (m = node.src.match(/:\/\/(.*?)\//)) && (m[1] === location.hostname);
          } else m = true;
          if (m && ((node.nodeName !== "A") || convertUrlText)) {
            node.childNodes.forEach(processNode);
          }
        }
        break;
      case Node.TEXT_NODE:
        if (convertUrlText && (m = node.nodeValue.match(trx))) {
          w = node.nodeValue;
          h = -1;
          a = [];
          while (c = trxg.exec(w)) {
            if (c.index > 0) a.push(w.substring(h, c.index));
            a.push("ytebd_" + c[1]);
            h = c.index + c[0].length;
          }
          if ((h > 0) && (h < w.length)) a.push(w.substr(h));
          for (c = a.length - 1; c >= 0; c--) {
            if (!a[c].replace(/^\s+|\s+$/g, "")) a.splice(c, 1);
          }
          if (node.parentNode.nodeName === "A") {
            if (a.length === 1) {
              node.href = "https://www.youtube.com/embed/" + a[0].substr(6);
              node.rel = "nofollow noopener noreferrer";
              node.target = "_blank";
            }
            a = null;
          }
          if (a) {
            pn = node.parentNode;
            c = node.nextSibling;
            a.forEach(function(v, i, n) {
              if (v.substr(0, 6) !== "ytebd_") {
                n = document.createTextNode(v);
              } else {
                n = document.createElement("A");
                n.textContent = "[Link]";
                n.title = v.substr(6);
                n.href = "https://www.youtube.com/embed/" + v.substr(6);
                n.rel = "nofollow noopener noreferrer";
                n.target = "_blank";
              }
              if (i > 0) {
                pn.insertBefore(n, c);
              } else {
                node.replaceWith(n);
              }
            });
          }
        }
    }
  }

  xe = ["BUTTON", "INPUT", "SCRIPT", "SELECT", "STYLE"];
  fe = ["FRAME", "IFRAME"];
  rx = /^(?:https?:\/\/)?(?:(?:(?:www\.)?youtube\.com\/)(?:embed\/|watch\?.*v=)|youtu\.be\/)([0-9a-z_\-]{11})/i;
  trx = /(?:https?:\/\/)?(?:(?:(?:www\.)?youtube\.com\/)(?:embed\/|watch\?.*v=)|youtu\.be\/)([0-9a-z_\-]{11})[^\s,'")\]}>]*/i;
  trxg = /(?:https?:\/\/)?(?:(?:(?:www\.)?youtube\.com\/)(?:embed\/|watch\?.*v=)|youtu\.be\/)([0-9a-z_\-]{11})[^\s,'")\]}>]*/gi;
  hrx = /^(?:(?:www\.)?youtube\.com|youtu\.be)$/i;
  lo = convertToLinkOnlyDomains.test(location.hostname);

  (new MutationObserver(function(records) {
    records.forEach(function(record) {
      if (record.type === "childList") {
        record.addedNodes.forEach(processNode);
      } else {
        processNode(record.target);
      }
    });
  })).observe(document.body, {attributes: true, attributeFilter: ["class", "style"], childList: true, subtree: true});

  if (document.body) processNode(document.body);

})();