User:Stumblean/common.js: Difference between revisions

Stumblean (talk | contribs)
No edit summary
Tags: Mobile edit Mobile web edit Advanced mobile edit
Stumblean (talk | contribs)
No edit summary
Tags: Mobile edit Mobile web edit Advanced mobile edit
Line 3: Line 3:


   const menu = document.createElement("div");
   const menu = document.createElement("div");
  menu.id = "mahitoMenu";
   Object.assign(menu.style, {
   Object.assign(menu.style, {
     position: "fixed",
     position: "fixed",
Line 20: Line 19:


   const header = document.createElement("div");
   const header = document.createElement("div");
   header.style.display = "flex";
   Object.assign(header.style, {
  header.style.alignItems = "center";
    display: "flex",
  header.style.gap = "6px";
    alignItems: "center",
  header.style.marginBottom = "8px";
    gap: "6px",
  header.style.cursor = "grab";
    marginBottom: "8px",
    cursor: "grab",
  });


   const img1 = document.createElement("img");
   const img1 = Object.assign(document.createElement("img"), { src: icon, width: 15, height: 15 });
   img1.src = icon;
   const img2 = Object.assign(document.createElement("img"), { src: icon, width: 15, height: 15 });
  img1.width = 15;
  img1.height = 15;


   const title = document.createElement("div");
   const titleDiv = document.createElement("div");
   title.textContent = "Mahito Menu";
   titleDiv.textContent = "Mahito Menu";
   title.style.fontWeight = "bold";
   titleDiv.style.fontWeight = "bold";
   title.style.color = "#fff";
   titleDiv.style.color = "#fff";
 
  const img2 = document.createElement("img");
  img2.src = icon;
  img2.width = 15;
  img2.height = 15;


   header.appendChild(img1);
   header.appendChild(img1);
   header.appendChild(title);
   header.appendChild(titleDiv);
   header.appendChild(img2);
   header.appendChild(img2);


Line 62: Line 56:
   }
   }


  // -- Always add these two
   const btnUndo = makeButton("Mass undo", () => {
   const btn1 = makeButton("Mass undo", () => {
     prompt("Copy this into your common.js:", "mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Alexis_Jazz/Kill-It-With-Fire.js&action=raw&ctype=text/javascript');");
     prompt("Copy this into your common.js:", "mw.loader.load('//en.wikipedia.org/w/index.php?title=User:Alexis_Jazz/Kill-It-With-Fire.js&action=raw&ctype=text/javascript');");
   });
   });


   const btn2 = makeButton("Open Explanation", () => {
   const btnExplain = makeButton("Open Explanation", () => {
     alert("Open Explanation clicked");
     alert("Open Explanation clicked");
   });
   });


   menu.appendChild(header);
   menu.appendChild(header);
   menu.appendChild(btn1);
   menu.appendChild(btnUndo);
   menu.appendChild(btn2);
   menu.appendChild(btnExplain);


   // -- Safely check if it's common.js
   // -- Common.js detection
   const user = mw.config.get("wgUserName") || "";
   try {
  const title = mw.config.get("wgPageName") || "";
    const user = mw.config.get("wgUserName") || "";
  const isCommonJS = title.includes(user + "/common.js");
    const pageTitle = mw.config.get("wgPageName") || "";
 
    if (pageTitle.includes(user + "/common.js")) {
  if (isCommonJS) {
      const btnEZ = makeButton("EZPASTE", async () => {
    const ezBtn = makeButton("EZPASTE", async () => {
        try {
      try {
          const text = await navigator.clipboard.readText();
        const clip = await navigator.clipboard.readText();
          await navigator.clipboard.writeText(text);
        await navigator.clipboard.writeText(clip);
          alert("EZPASTE: Clipboard refreshed with:\n\n" + text);
        alert("EZPASTE: Clipboard refreshed with:\n\n" + clip);
        } catch (err) {
      } catch (e) {
          alert("Clipboard failed: " + err.message);
        alert("Clipboard failed: " + e.message);
        }
       }
       });
     });
      menu.appendChild(btnEZ);
     menu.appendChild(ezBtn);
     }
  } catch (e) {
     console.warn("Could not detect common.js page", e);
   }
   }


   document.body.appendChild(menu);
   document.body.appendChild(menu);


   // === DRAGGING ===
   // === Dragging logic
   let isDragging = false;
   let isDragging = false;
   let offsetX = 0, offsetY = 0;
   let offsetX = 0,
    offsetY = 0;


   function startDrag(x, y) {
   function startDrag(x, y) {
Line 123: Line 119:
     e.preventDefault();
     e.preventDefault();
   });
   });
 
   document.addEventListener("mousemove", (e) => doDrag(e.clientX, e.clientY));
   document.addEventListener("mousemove", (e) => {
    doDrag(e.clientX, e.clientY);
  });
 
   document.addEventListener("mouseup", stopDrag);
   document.addEventListener("mouseup", stopDrag);


Line 135: Line 127:
     e.preventDefault();
     e.preventDefault();
   });
   });
   document.addEventListener("touchmove", (e) => {
   document.addEventListener("touchmove", (e) => {
     if (!isDragging) return;
     if (!isDragging) return;
Line 141: Line 132:
     doDrag(touch.clientX, touch.clientY);
     doDrag(touch.clientX, touch.clientY);
   });
   });
   document.addEventListener("touchend", stopDrag);
   document.addEventListener("touchend", stopDrag);
})();
})();