anonymous_you

Endchan TT Sauce

Dec 28th, 2022 (edited)
2,370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name         Endchan TT Sauce
  3. // @namespace    https://endchan.org/
  4. // @version      1.3
  5. // @description  Add TikTok sauce button next to filenames
  6. // @author       Anonymous (You)
  7. // @match        http://endchan.org/*
  8. // @match        https://endchan.org/*
  9. // @match        http://endchan.net/*
  10. // @match        https://endchan.net/*
  11. // @match        http://magrathea.endchan.net/*
  12. // @match        https://magrathea.endchan.net/*
  13. // @grant        none
  14. // @downloadURL  https://pastebin.com/raw/rA2CWZKP
  15. // @updateURL    https://pastebin.com/raw/rA2CWZKP
  16. // ==/UserScript==
  17.  
  18. /*
  19. 1.3 - added endchan.net to @match
  20. 1.2 - support for magrathea
  21. 1.1 - added update urls
  22. */
  23.  
  24. // ----- CONFIG -----
  25.  
  26. const sauceText = "[tiktok]";
  27. const sauceUrl = "https://www.tiktok.com/@share/video/";
  28. const sauceRegex = /(6|7)\d{18}/g;
  29.  
  30. // ----- MAIN -----
  31.  
  32. (function() {
  33.     'use strict';
  34.  
  35.     // parse all pre-existing posts when page loads
  36.     parseNode(document);
  37.  
  38.     // callback function to execute when mutations are observed
  39.     let callback = function(mutationsList) {
  40.         for(let mutation of mutationsList) {
  41.             for (let node of mutation.addedNodes) {
  42.                 // make sure we can get elements by class name on node
  43.                 if (typeof node.getElementsByClassName === "function") {
  44.                     parseNode(node);
  45.                 }
  46.             }
  47.         }
  48.     };
  49.  
  50.     // observe page for changes to sauce new posts (all articles/posts should be inside the a tag with the id "divThreads")
  51.     var observer = new MutationObserver(callback);
  52.     let observedNode = document.getElementById("divThreads");
  53.     if (!observedNode) {
  54.         observedNode = document.getElementById("threadsContainer"); // magrathea
  55.     }
  56.     observer.observe(observedNode, { childList: true, subtree:true });
  57. })();
  58.  
  59. // originalNameLink should contain the filename, find all of those under the given node
  60. function parseNode(node) {
  61.     let links = node.getElementsByClassName("originalNameLink");
  62.     if (!links || links.length == 0) {
  63.         links = node.getElementsByClassName("filename"); // magrathea
  64.     }
  65.     if (links) {
  66.         for (let link of links) {
  67.             // here's the catch: page load can tuncate innerHTML so the value of the 'download' attribute should be used for saucing
  68.             // however, when new posts are added via refresh that doesn't create the element, though it looks like there's no truncation
  69.             // so, 'download' should be used if it's present and otherwise fallback to trying innerHTML
  70.             let sauceLink = null;
  71.             if (sauceLink = parseSauceFromString(link.getAttribute("download"))) {
  72.                 // attach the sauce link to the page if you have it
  73.                 link.parentElement.insertAdjacentElement("afterend", sauceLink);
  74.             } else if (sauceLink = parseSauceFromString(link.innerHTML)) {
  75.                 // attach the sauce link to the page if you have it
  76.                 link.parentElement.insertAdjacentElement("afterend", sauceLink);
  77.             }
  78.         }
  79.     }
  80. }
  81.  
  82. // pass a string that may or may not contain a TT id, returns an <a> element that links to sauce if it found an id, null otherwise
  83. function parseSauceFromString(string) {
  84.     if (string == null) return null;
  85.  
  86.     const found = string.match(sauceRegex);
  87.     if (found != null) {
  88.         var a = document.createElement("a");
  89.         a.setAttribute("href", sauceUrl + found);
  90.         a.setAttribute("target", "_blank"); // new window or tab
  91.         a.innerHTML = sauceText;
  92.         return a;
  93.     }
  94.     return null;
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment