Advertisement
anonymous_you

Endchan TT Sauce

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