PH Video links

Linkify video titles on PornHub

  1. // ==UserScript==
  2. // @name PH Video links
  3. // @version 1.2.1
  4. // @description Linkify video titles on PornHub
  5. // @author salad: https://greasyfork.org/en/users/241444-salad
  6. // @include https://www.pornhub.com/view_video.php?*
  7. // @grant none
  8. // @namespace https://greasyfork.org/users/241444
  9. // ==/UserScript==
  10.  
  11.  
  12. (function() {
  13.  
  14. const playerWrap = document.getElementById('player');
  15. if(!playerWrap) {
  16. console.error('playerWrap element not found');
  17. return;
  18. }
  19.  
  20. let observer;
  21. let sourceElement;
  22. const observerConfig = { attributes: true, childList: true, subtree: true };
  23.  
  24. // once source is defined, set title element
  25. const setLink = () => {
  26.  
  27. // video title element
  28. const title = document.querySelector('h1.title>span');
  29.  
  30. // video title
  31. const titleText = title.innerHTML;
  32.  
  33. // username if any
  34. const username = document.querySelector('.video-info-row .usernameWrap');
  35. const usernameText = (username !== null) ? username.innerText : 'unknown';
  36.  
  37. // video source url
  38. const sourceUrl = sourceElement.getAttribute('src');
  39.  
  40. // wrap in a link
  41. const link = `<a href="${sourceUrl}">${usernameText} - ${titleText}</a>`;
  42.  
  43. title.innerHTML = link;
  44.  
  45. // also update the page title
  46. document.title = `${usernameText} - ${titleText}`;
  47.  
  48. }
  49.  
  50. // Callback function to execute when mutations are observed
  51. const checkForSource = () => {
  52.  
  53. const source = playerWrap.querySelector('source[src]');
  54. if(source && source.getAttribute('src')) {
  55. sourceElement = source;
  56. setLink();
  57. observer.disconnect();
  58. }
  59.  
  60. };
  61.  
  62. observer = new MutationObserver(checkForSource);
  63. observer.observe(playerWrap, observerConfig);
  64.  
  65. // initial run
  66. checkForSource();
  67.  
  68. })();