lxy-yz / Use Vite

// ==UserScript==
// @name         Use Vite
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://customer-management.hotpads.com/customer-management/app*
// @match        https://localhost:8443/customer-management/app*
// @icon         https://www.google.com/s2/favicons?domain=tampermonkey.net
// @grant        none
// @license MIT
// ==/UserScript==

(function () {
  'use strict';

  const html = `
<script type="module" src="http://localhost:3001/@vite/client"></script>
<script type="module">
import RefreshRuntime from "http://localhost:3001/@react-refresh"
RefreshRuntime.injectIntoGlobalHook(window)
window.$RefreshReg$ = () => {}
window.$RefreshSig$ = () => (type) => type
window.__vite_plugin_react_preamble_installed__ = true
</script>

<div id="app" />
<script type="module" src="http://localhost:3001/src/index.tsx"></script>
`;
  document.body.innerHTML = html;
  let styles = [...document.querySelectorAll('link[rel="stylesheet"]')]
  styles.forEach(style => document.head.removeChild(style))
  replace(document.body);

  function replace(node) {
    if (node.tagName === "SCRIPT") {
      node.parentNode.replaceChild(clone(node), node);
    }
    else {
      let i = -1,
        children = node.childNodes;
      while (++i < children.length) {
        replace(children[i]);
      }
    }

    return node;
  }

  function clone(node) {
    let script = document.createElement("script");
    script.text = node.innerHTML;

    let i = -1,
      attrs = node.attributes,
      attr;
    while (++i < attrs.length) {
      script.setAttribute((attr = attrs[i]).name, attr.value);
    }
    return script;
  }

  console.log('✅ userscript hooked')
})();