e-hentai auto hyperlink

e-hentai auto convert urls in gallery comments into hyperlinks

  1. // ==UserScript==
  2. // @name e-hentai auto hyperlink
  3. // @namespace https://greasyfork.org/scripts/441145
  4. // @version 2.3
  5. // @description e-hentai auto convert urls in gallery comments into hyperlinks
  6. // @author fmnijk
  7. // @match https://e-hentai.org/*
  8. // @icon https://www.google.com/s2/favicons?domain=e-hentai.org
  9. // @grant none
  10. // @run-at document-end
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. // https://e-hentai.org/g/2666870/42d33504ad/
  15.  
  16. /* main function */
  17. (function() {
  18. 'use strict';
  19.  
  20. if (window.location.href === 'https://e-hentai.org/'){
  21. return false;
  22. }
  23.  
  24. const comments = document.querySelectorAll(".c6");
  25.  
  26. comments.forEach(function(comment) {
  27. let before = comment.innerHTML;
  28.  
  29. // 暫時替換 <img> 標籤 因為img標籤裡有url 會被影響
  30. let imgPlaceholders = [];
  31. before = before.replace(/<img\b[^>]*>/gi, function(match) {
  32. imgPlaceholders.push(match);
  33. return `<!--img-placeholder-${imgPlaceholders.length - 1}-->`;
  34. });
  35.  
  36. before = before.replace(/&amp;/g, '&');
  37.  
  38. // 處理 URL
  39. const after = before.replace(/(<a\b[^>]*>.*<\/a>)|((https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)))/gi, function(match, p1, p2) {
  40. // Check if the match is already an anchor tag, if so, return it unchanged.
  41. if (p1) {
  42. return p1;
  43. } else {
  44. // Otherwise, create a new anchor tag.
  45. return '<a href="' + p2 + '">' + p2 + '</a>';
  46. }
  47. });
  48.  
  49. // 還原 <img> 標籤
  50. let finalHTML = after.replace(/<!--img-placeholder-(\d+)-->/g, function(match, index) {
  51. return imgPlaceholders[parseInt(index)];
  52. });
  53.  
  54. comment.innerHTML = finalHTML;
  55. });
  56. })();
  57.