Skip to content

armgit / Pake

Public Imported from GitHub
0

Turn any website into a lightweight desktop app with one command, on macOS, Windows and Linux.

4.0 KB
Raw Blame
(function () {
  if (window.top && window.top !== window) return;

  window.addEventListener("DOMContentLoaded", (_event) => {
    // Applies to every app.
    const globalContentCSS = `
    html::-webkit-scrollbar {
      display: none !important;
    }
  `;

    // Window drag strip shared by every immersive header.
    const dragBarCSS = `
    #pake-top-dom:active {
      cursor: grabbing;
      cursor: -webkit-grabbing;
    }

    #pake-top-dom {
      position: fixed;
      background: transparent;
      top: 0;
      width: 100%;
      height: 20px;
      cursor: grab;
      -webkit-app-region: drag;
      user-select: none;
      -webkit-user-select: none;
      z-index: 99999;
    }
  `;

    // Site tweaks only load on their own hosts (and subdomains), so a site
    // packaged by a user never inherits rules written for another app.
    // `content` applies on every platform; `immersive` only under the macOS
    // hidden title bar, where the traffic lights overlap the page.
    const siteStyles = [
      {
        hosts: ["chatgpt.com", "chat.openai.com"],
        immersive: `
        #app-shell-sidebar > div {
          padding-top: 24px;
        }
      `,
      },
      {
        hosts: ["grok.com"],
        immersive: `
        #grok-app-root [data-sidebar="sidebar"] {
          padding-top: 24px;
        }
      `,
      },
      {
        hosts: ["gemini.google.com"],
        immersive: `
        .side-nav-menu-button {
          margin-left: 76px !important;
        }
      `,
      },
      {
        hosts: ["notion.so", "notion.com"],
        immersive: `
        #notion-app .notion-sidebar,#notion-app .notion-topbar{
          padding-top: 20px;
          box-sizing: content-box;
        }
      `,
      },
      {
        hosts: ["excalidraw.com"],
        immersive: `
        #root > .excalidraw-app> .excalidraw-container .App-menu.App-menu_top{
          margin-top: 15px;
        }
      `,
      },
      {
        hosts: ["youtube.com"],
        immersive: `
        #layout > ytmusic-nav-bar{
          padding-top: 20px;
        }

        ytd-masthead>#container.style-scope.ytd-masthead {
          padding-top: 12px;
        }

        #background.ytd-masthead {
          height: 68px;
        }
      `,
      },
      {
        hosts: ["xiaohongshu.com"],
        immersive: `
        #app > div.layout > div.header-container.showSearchBoxOrHeaderFixed > header > a {
          padding-top:10px;
        }

        #header-area .logoIcon .user-info{
          padding-top: 20px;
        }

        #global > div.header-container > .mask-paper {
          padding-top: 20px;
        }
      `,
      },
    ];

    const hostname = window.location.hostname;
    const matchedSites = siteStyles.filter(({ hosts }) =>
      hosts.some((host) => hostname === host || hostname.endsWith(`.${host}`)),
    );
    // An app whose extension restyles the site (`site_styles: false`, set by
    // the runtime builder) keeps only the window rules: the content tweaks
    // would fight the extension's own layout.
    const siteStylesOn = window["pakeConfig"]?.site_styles !== false;
    const siteCSS = (key) =>
      matchedSites.map((site) => site[key] || "").join("\n");

    const injectStyle = (css, id) => {
      if (typeof window.__PAKE_INJECT_STYLE__ === "function") {
        window.__PAKE_INJECT_STYLE__(css, id);
      } else {
        const styleElement = document.createElement("style");
        styleElement.id = id;
        styleElement.textContent = css;
        document.head.appendChild(styleElement);
      }
    };

    injectStyle(
      globalContentCSS + (siteStylesOn ? siteCSS("content") : ""),
      "pake-content-style",
    );

    // Top spacing adapts to head-hiding scenarios
    const isMac = /Mac/i.test(navigator.userAgent);
    if (hasImmersiveHeader(window["pakeConfig"])) {
      const topPaddingCSS = isMac
        ? siteCSS("immersive") + dragBarCSS
        : dragBarCSS;
      injectStyle(topPaddingCSS, "pake-top-padding-style");
    }
  });
})();