PH Video links

Linkify video titles on PornHub

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         PH Video links
// @version      1.2.1
// @description  Linkify video titles on PornHub
// @author       salad: https://greasyfork.org/en/users/241444-salad
// @include      https://www.pornhub.com/view_video.php?*
// @grant        none
// @namespace    https://greasyfork.org/users/241444
// ==/UserScript==


(function() {

  const playerWrap = document.getElementById('player');
  if(!playerWrap) {
    console.error('playerWrap element not found');
    return;
  }

  let observer;
  let sourceElement;
  const observerConfig = { attributes: true, childList: true, subtree: true };

  // once source is defined, set title element
  const setLink = () => {

    // video title element
    const title = document.querySelector('h1.title>span');

    // video title
    const titleText = title.innerHTML;

    // username if any
    const username = document.querySelector('.video-info-row .usernameWrap');
    const usernameText = (username !== null) ? username.innerText : 'unknown';

    // video source url
    const sourceUrl = sourceElement.getAttribute('src');

    // wrap in a link
    const link = `<a href="${sourceUrl}">${usernameText} - ${titleText}</a>`;

    title.innerHTML = link;

    // also update the page title
    document.title = `${usernameText} - ${titleText}`;

  }

  // Callback function to execute when mutations are observed
  const checkForSource = () => {

      const source = playerWrap.querySelector('source[src]');
      if(source && source.getAttribute('src')) {
        sourceElement = source;
        setLink();
        observer.disconnect();
      }

  };

  observer = new MutationObserver(checkForSource);
  observer.observe(playerWrap, observerConfig);

  // initial run
  checkForSource();

})();