satosa-ph

Launched an MVP that made it to the front page of Product Hunt, by offering a service that manages agreements on your websites and apps with a low-friction click to accept e-signature alternative.
git clone https://unitehenry.com/git/satosa-ph.git
Log | Files | Refs | README

expand.js (2526B)


      1 /*
      2  * SingleFile user script: expand collapsed text before the page is saved.
      3  *
      4  * Hooked into SingleFile's user-script event API:
      5  *   https://github.com/gildas-lormeau/SingleFile/wiki/How-to-execute-a-user-script-before-a-page-is-saved
      6  *
      7  * Product Hunt renders the product description and long comments with a CSS
      8  * line clamp plus a "see more" / "Show more" button. The text is already in
      9  * the DOM, so this is mostly cosmetic - but it makes the archived page read
     10  * the way a visitor who expanded everything would see it.
     11  *
     12  * The "Show more" button of the Launch Team block (class `min-w-36`) opens a
     13  * modal, so it is skipped on purpose.
     14  *
     15  * Expanding turns the toggle into a dead "see less" / "Show less" button once
     16  * the page scripts are stripped. SingleFile's deferred-content pass makes the
     17  * page re-render those toggles after this handler returns, so they are removed
     18  * from the finished file by tidy.py.
     19  */
     20 (() => {
     21   if (window.top !== window) {
     22     return;
     23   }
     24 
     25   dispatchEvent(new CustomEvent("single-file-user-script-init"));
     26 
     27   const EXPAND_LABELS = new Set(["see more", "show more"]);
     28   const SKIP_CLASS = /(^|\s)min-w-36(\s|$)/;
     29   const clickedNodes = new WeakSet();
     30   const ROUNDS = 8;
     31   const SETTLE_MS = 300;
     32 
     33   const label = element => (element.textContent || "").trim().toLowerCase();
     34 
     35   const isVisible = element => {
     36     const style = getComputedStyle(element);
     37     return style.display !== "none" && style.visibility !== "hidden";
     38   };
     39 
     40   const clickExpanders = () => {
     41     let count = 0;
     42     for (const button of document.querySelectorAll("button")) {
     43       const text = label(button);
     44       if (!EXPAND_LABELS.has(text)) continue;
     45       if (SKIP_CLASS.test(String(button.className || ""))) continue;
     46       if (clickedNodes.has(button)) continue;
     47       if (!isVisible(button)) continue;
     48       clickedNodes.add(button);
     49       button.click();
     50       count++;
     51       console.log(`[ph-archive] expanded: "${text}"`);
     52     }
     53     return count;
     54   };
     55 
     56   addEventListener("single-file-on-before-capture-request", async event => {
     57     event.preventDefault();
     58     try {
     59       for (let round = 0; round < ROUNDS; round++) {
     60         if (!clickExpanders()) break;
     61         await new Promise(resolve => setTimeout(resolve, SETTLE_MS));
     62       }
     63       // let the last expansion re-layout before SingleFile reads the page
     64       await new Promise(resolve => setTimeout(resolve, SETTLE_MS));
     65     } finally {
     66       dispatchEvent(new CustomEvent("single-file-on-before-capture-response"));
     67     }
     68   });
     69 })();