Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Endchan TT Sauce
- // @namespace https://endchan.org/
- // @version 1.2
- // @description Add TikTok sauce button next to filenames
- // @author Anonymous (You)
- // @match http://endchan.org/*
- // @match https://endchan.org/*
- // @match http://magrathea.endchan.net/*
- // @match https://magrathea.endchan.net/*
- // @grant none
- // @downloadURL https://pastebin.com/raw/rA2CWZKP
- // @updateURL https://pastebin.com/raw/rA2CWZKP
- // ==/UserScript==
- /*
- 1.2 - support for magrathea
- 1.1 - added update urls
- */
- // ----- CONFIG -----
- const sauceText = "[tiktok]";
- const sauceUrl = "https://www.tiktok.com/@share/video/";
- const sauceRegex = /(6|7)\d{18}/g;
- // ----- MAIN -----
- (function() {
- 'use strict';
- // parse all pre-existing posts when page loads
- parseNode(document);
- // callback function to execute when mutations are observed
- let callback = function(mutationsList) {
- for(let mutation of mutationsList) {
- for (let node of mutation.addedNodes) {
- // make sure we can get elements by class name on node
- if (typeof node.getElementsByClassName === "function") {
- parseNode(node);
- }
- }
- }
- };
- // observe page for changes to sauce new posts (all articles/posts should be inside the a tag with the id "divThreads")
- var observer = new MutationObserver(callback);
- let observedNode = document.getElementById("divThreads");
- if (!observedNode) {
- observedNode = document.getElementById("threadsContainer"); // magrathea
- }
- observer.observe(observedNode, { childList: true, subtree:true });
- })();
- // originalNameLink should contain the filename, find all of those under the given node
- function parseNode(node) {
- let links = node.getElementsByClassName("originalNameLink");
- if (!links || links.length == 0) {
- links = node.getElementsByClassName("filename"); // magrathea
- }
- if (links) {
- for (let link of links) {
- // here's the catch: page load can tuncate innerHTML so the value of the 'download' attribute should be used for saucing
- // however, when new posts are added via refresh that doesn't create the element, though it looks like there's no truncation
- // so, 'download' should be used if it's present and otherwise fallback to trying innerHTML
- let sauceLink = null;
- if (sauceLink = parseSauceFromString(link.getAttribute("download"))) {
- // attach the sauce link to the page if you have it
- link.parentElement.insertAdjacentElement("afterend", sauceLink);
- } else if (sauceLink = parseSauceFromString(link.innerHTML)) {
- // attach the sauce link to the page if you have it
- link.parentElement.insertAdjacentElement("afterend", sauceLink);
- }
- }
- }
- }
- // 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
- function parseSauceFromString(string) {
- if (string == null) return null;
- const found = string.match(sauceRegex);
- if (found != null) {
- var a = document.createElement("a");
- a.setAttribute("href", sauceUrl + found);
- a.setAttribute("target", "_blank"); // new window or tab
- a.innerHTML = sauceText;
- return a;
- }
- return null;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement