From 4243e0196af0a30bd007249bc8628caf788a5099 Mon Sep 17 00:00:00 2001 From: SwingTheVine Date: Fri, 25 Jul 2025 23:31:51 -0400 Subject: [PATCH] Finished(?) pin icon functionality --- dist/BlueMarble.user.css | 2 +- dist/BlueMarble.user.js | 6 ++-- docs/README.md | 2 +- package-lock.json | 4 +-- package.json | 2 +- src/BlueMarble.meta.js | 2 +- src/apiHandler.js | 15 +++++++-- src/main.js | 6 ++-- src/overlay.css | 12 +++++-- src/overlay.js | 72 ++++++++++++++++++++++++++++++++-------- 10 files changed, 95 insertions(+), 28 deletions(-) diff --git a/dist/BlueMarble.user.css b/dist/BlueMarble.user.css index 67c175f..ffbbb76 100644 --- a/dist/BlueMarble.user.css +++ b/dist/BlueMarble.user.css @@ -1 +1 @@ -#bm-overlay{position:fixed;background-color:#153063e6;color:#fff;padding:10px;border-radius:8px;z-index:9000}div#bm-overlay{font-family:Roboto Mono,Courier New,Monaco,DejaVu Sans Mono,monospace,Arial;letter-spacing:.05em}#bm-bar-drag{margin-bottom:.5em;background:url('data:image/svg+xml;utf8,') repeat;cursor:grab;width:100%;height:1em}#bm-bar-drag.dragging{cursor:grabbing}#bm-contain-header{margin-bottom:.5em}#bm-overlay img{display:inline-block;height:2.5em;margin-right:1ch;vertical-align:middle}#bm-overlay h1{display:inline-block;font-size:x-large;font-weight:700;vertical-align:middle}#bm-contain-automation input[type=checkbox]{vertical-align:middle;margin-right:.5ch}#bm-contain-automation label{margin-right:.5ch}.bm-help{border:white 1px solid;height:1.25em;width:1.25em;margin-top:2px;text-align:center;line-height:1.25em;padding:0!important}#bm-button-coords{vertical-align:middle}#bm-button-coords svg{width:50%;margin:0 auto;fill:#111}#bm-contain-coords input[type=text]{width:5.5ch;margin-left:1ch;background-color:#0003;padding:0 .5ch;font-size:small}#bm-contain-buttons{display:flex;flex-direction:row;flex-wrap:wrap;align-content:center;justify-content:center;align-items:center;gap:1ch}div:has(>#bm-input-file)>button{width:100%;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}#bm-output-status{font-size:small;background-color:#0003;padding:0 .5ch;height:3.75em;width:100%}#bm-contain-userinfo,#bm-contain-automation,#bm-contain-coords,#bm-contain-buttons,div:has(>#bm-input-file),#bm-output-status{margin-top:.5em}#bm-overlay button{background-color:#144eb9;border-radius:1em;padding:0 .75ch}#bm-overlay button:hover,#bm-overlay button:focus{background-color:#1061e5}#bm-overlay button:active,#bm-overlay button:disabled{background-color:#2e97ff}#bm-overlay button:disabled{text-decoration:line-through} +#bm-overlay{position:fixed;background-color:#153063e6;color:#fff;padding:10px;border-radius:8px;z-index:9000}div#bm-overlay{font-family:Roboto Mono,Courier New,Monaco,DejaVu Sans Mono,monospace,Arial;letter-spacing:.05em}#bm-bar-drag{margin-bottom:.5em;background:url('data:image/svg+xml;utf8,') repeat;cursor:grab;width:100%;height:1em}#bm-bar-drag.dragging{cursor:grabbing}#bm-contain-header{margin-bottom:.5em}#bm-overlay img{display:inline-block;height:2.5em;margin-right:1ch;vertical-align:middle}#bm-overlay h1{display:inline-block;font-size:x-large;font-weight:700;vertical-align:middle}#bm-contain-automation input[type=checkbox]{vertical-align:middle;margin-right:.5ch}#bm-contain-automation label{margin-right:.5ch}.bm-help{border:white 1px solid;height:1.25em;width:1.25em;margin-top:2px;text-align:center;line-height:1.25em;padding:0!important}#bm-button-coords{vertical-align:middle}#bm-button-coords svg{width:50%;margin:0 auto;fill:#111}#bm-contain-coords input[type=number]{width:5.5ch;margin-left:1ch;background-color:#0003;padding:0 .5ch;font-size:small}#bm-contain-buttons{display:flex;flex-direction:row;flex-wrap:wrap;align-content:center;justify-content:center;align-items:center;gap:1ch}div:has(>#bm-input-file)>button{width:100%;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}#bm-output-status{font-size:small;background-color:#0003;padding:0 .5ch;height:3.75em;width:100%}#bm-contain-userinfo,#bm-contain-automation,#bm-contain-coords,#bm-contain-buttons,div:has(>#bm-input-file),#bm-output-status{margin-top:.5em}#bm-overlay button{background-color:#144eb9;border-radius:1em;padding:0 .75ch}#bm-overlay button:hover,#bm-overlay button:focus{background-color:#1061e5}#bm-overlay button:active,#bm-overlay button:disabled{background-color:#2e97ff}#bm-overlay button:disabled{text-decoration:line-through} diff --git a/dist/BlueMarble.user.js b/dist/BlueMarble.user.js index 7fb3cd6..a72768e 100644 --- a/dist/BlueMarble.user.js +++ b/dist/BlueMarble.user.js @@ -1,7 +1,7 @@ // ==UserScript== // @name Blue Marble // @namespace https://github.com/SwingTheVine/ -// @version 0.41.0 +// @version 0.41.11 // @description A userscript to automate and/or enhance the user experience on Wplace.live. Make sure to comply with the site's Terms of Service, and rules! This script is not affiliated with Wplace.live in any way, use at your own risk. This script is not affiliated with TamperMonkey. The author of this userscript is not responsible for any damages, issues, loss of data, or punishment that may occur as a result of using this script. This script is provided "as is" under the MPL-2.0 license. The "Blue Marble" icon is licensed under CC0 1.0 Universal (CC0 1.0) Public Domain Dedication. The image is owned by NASA. // @author SwingTheVine // @license MPL-2.0 @@ -20,5 +20,5 @@ // Wplace --> https://wplace.live // License --> https://www.mozilla.org/en-US/MPL/2.0/ -(()=>{var h=class{constructor(o,t){this.name=o,this.version=t}create(){let o="bm-output-status",t=document.createElement("div");t.id="bm-overlay",t.style.top="10px",t.style.right="75px";let e=document.createElement("div");e.id="bm-contain-header";let n=document.createElement("div");n.id="bm-bar-drag",e.appendChild(n);let i=document.createElement("img");i.src="https://raw.githubusercontent.com/SwingTheVine/Wplace-BlueMarble/main/src/assets/Favicon.png",i.alt="Blue Marble Icon",e.appendChild(i);let s=document.createElement("h1");s.textContent=this.name,e.appendChild(s);let c=document.createElement("div");c.id="bm-contain-userinfo";let f=document.createElement("p");f.id="bm-user-name",f.textContent="Username:",c.appendChild(f);let C=document.createElement("p");C.id="bm-user-droplets",C.textContent="Droplets:",c.appendChild(C);let g=document.createElement("p");g.id="bm-user-nextlevel",g.textContent="Next level in...",c.appendChild(g);let a=document.createElement("div");a.id="bm-contain-automation",a.appendChild(this.createInputCheckbox("Stealth","bm-input-stealth",!0)),a.appendChild(this.createButtonQuestion("bm-help-stealth","Help: Waits for the website to make requests, instead of sending requests.",o)),a.appendChild(document.createElement("br")),a.appendChild(this.createInputCheckbox("Possessed","bm-input-possessed",!0)),a.appendChild(this.createButtonQuestion("bm-help-possessed","Help: Controls the website as if it were possessed.",o)),a.appendChild(document.createElement("br")),a.appendChild(this.createInputCheckbox("Panic","bm-input-panic")),a.appendChild(this.createButtonQuestion("bm-help-panic","Help: Stops placing for a while if it detects a user nearby.",o)),a.appendChild(document.createElement("br"));let l=document.createElement("div");l.id="bm-contain-coords";let d=document.createElement("button");d.id="bm-button-coords",d.className="bm-help",d.style="margin-top: 0",d.innerHTML='',d.onclick=()=>{},l.appendChild(d),l.appendChild(this.createInputText("bm-input-tx","Tl X","","4")),l.appendChild(this.createInputText("bm-input-ty","Tl Y","","4")),l.appendChild(this.createInputText("bm-input-px","Px X","","4")),l.appendChild(this.createInputText("bm-input-py","Px Y","","4")),a.appendChild(l),a.appendChild(this.createInputFile("bm-input-file"));let u=document.createElement("div");u.id="bm-contain-buttons",u.appendChild(this.createButton("bm-button-enable","Enable")),u.appendChild(this.createButton("bm-button-disable","Disable")),a.appendChild(u);let m=document.createElement("textarea");m.id=o,m.readOnly=!0,m.placeholder=`Status: Sleeping... -Version: ${this.version}`,a.appendChild(m),t.appendChild(e),t.appendChild(document.createElement("hr")),t.appendChild(c),t.appendChild(document.createElement("hr")),t.appendChild(a),document.body.appendChild(t),this.handleDrag(t,n)}updateInnerHTML(o,t,e=!1){let n=document.getElementById(o);if(n){if(n instanceof HTMLInputElement){n.value=t;return}e?n.textContent=t:n.innerHTML=t}}createButtonQuestion(o,t,e){let n=this.createButton(o,"?");return n.className="bm-help",n.title=t,n.onclick=()=>{this.updateInnerHTML(e,t)},n}createButton(o,t,e=!0){let n=document.createElement("button");return n.id=o,n.textContent=t,n.disabled=!e,n}createInputText(o,t="",e="",n="",i=!1){let s=document.createElement("input");return s.id=o,s.type="text",s.placeholder=t,s.value=e,s.readOnly=i,s.maxLength=n,s}createInputCheckbox(o,t,e=!1){let n=document.createElement("label");n.textContent=o;let i=document.createElement("input");return i.type="checkbox",i.id=t,i.checked=e,n.prepend(i),n}createInputFile(o){let t=document.createElement("div"),e=document.createElement("input");e.id=o,e.type="file",e.style="display: none";let n=document.createElement("button");return n.textContent="Upload File",n.addEventListener("click",()=>{e.click()}),e.addEventListener("change",()=>{n.style.maxWidth=`${n.offsetWidth}px`,e.files.length>0?n.textContent=e.files[0].name:n.textContent="Upload File"}),t.appendChild(e),t.appendChild(n),t}handleDrag(o,t){let e=!1,n,i=0;t.addEventListener("mousedown",function(s){e=!0,n=s.clientX-o.getBoundingClientRect().left,i=s.clientY-o.getBoundingClientRect().top,document.body.style.userSelect="none",t.classList.add("dragging")}),t.addEventListener("touchstart",function(s){e=!0;let c=s?.touches?.[0];c&&(n=c.clientX-o.getBoundingClientRect().left,i=c.clientY-o.getBoundingClientRect().top,document.body.style.userSelect="none",t.classList.add("dragging"))},{passive:!1}),document.addEventListener("mousemove",function(s){e&&(o.style.left=s.clientX-n+"px",o.style.top=s.clientY-i+"px",o.style.right="")}),document.addEventListener("touchmove",function(s){if(e){let c=s?.touches?.[0];if(!c)return;o.style.left=c.clientX-n+"px",o.style.top=c.clientY-i+"px",s.preventDefault()}},{passive:!1}),document.addEventListener("mouseup",function(){e=!1,document.body.style.userSelect="",t.classList.remove("dragging")}),document.addEventListener("touchend",function(){e=!1,document.body.style.userSelect="",t.classList.remove("dragging")}),document.addEventListener("touchcancel",function(){e=!1,document.body.style.userSelect="",t.classList.remove("dragging")})}};var b=class{constructor(){this.disableAll=!1}spontaneousResponseListener(o){window.addEventListener("message",t=>{let e=t.data;if(!(e&&e.source==="blue-marble"))return;let n=e.endpoint.split("?")[0].split("/").filter(i=>i&&isNaN(Number(i))).pop();switch(console.log(`Recieved message about "${n}"`),n){case"me":let i=Math.ceil(Math.pow(Math.floor(e.jsonData?.level)*Math.pow(30,.65),1.5384615384615383)-e.jsonData?.pixelsPainted);o.updateInnerHTML("bm-user-name",`Username: ${e.jsonData?.name}`),o.updateInnerHTML("bm-user-droplets",`Droplets: ${new Intl.NumberFormat().format(e.jsonData?.droplets)}`),o.updateInnerHTML("bm-user-nextlevel",`Next level in ${new Intl.NumberFormat().format(i)} pixel${i==1?"":"s"}`);break;case"robots":this.disableAll=e.jsonData?.userscript?.toString().toLowerCase()=="false"}})}};var x=GM_info.script.name.toString(),E=GM_info.script.version.toString();function y(r){let o=document.createElement("script");o.textContent=`(${r})();`,document.documentElement.appendChild(o),o.remove()}y(()=>{let r=window.fetch;window.fetch=async function(...o){let t=await r.apply(this,o),e=t.clone();if((e.headers.get("content-type")||"").includes("application/json")){let i=(o[0]instanceof Request?o[0]?.url:o[0])||"ignore";console.log(`Sending JSON message about endpoint "${i}"`),e.json().then(s=>{window.postMessage({source:"blue-marble",endpoint:i,jsonData:s},"*")}).catch(s=>{console.error("BM - Failed to parse JSON:",s)})}return t}});var w=GM_getResourceText("CSS-BM-File");GM_addStyle(w);var p=document.createElement("link");p.href="https://fonts.googleapis.com/css2?family=Roboto+Mono:ital,wght@0,100..700;1,100..700&display=swap";p.rel="preload";p.as="style";p.onload="this.onload=null;this.rel='stylesheet'";document.head.appendChild(p);var v=new h(x,E);v.create();var L=new b;L.spontaneousResponseListener(v);console.log(`${x} (${E}) userscript has loaded!`);})(); +(()=>{var g=class{constructor(e,o){this.name=e,this.version=o,this.apiHandler=null,this.outputStatusId="bm-output-status"}setApiHandler(e){this.apiHandler=e}create(){let e=document.createElement("div");e.id="bm-overlay",e.style.top="10px",e.style.right="75px";let o=document.createElement("div");o.id="bm-contain-header";let t=document.createElement("div");t.id="bm-bar-drag",o.appendChild(t);let n=document.createElement("img");n.src="https://raw.githubusercontent.com/SwingTheVine/Wplace-BlueMarble/main/src/assets/Favicon.png",n.alt="Blue Marble Icon",o.appendChild(n);let i=document.createElement("h1");i.textContent=this.name,o.appendChild(i);let s=document.createElement("div");s.id="bm-contain-userinfo";let a=document.createElement("p");a.id="bm-user-name",a.textContent="Username:",s.appendChild(a);let r=document.createElement("p");r.id="bm-user-droplets",r.textContent="Droplets:",s.appendChild(r);let c=document.createElement("p");c.id="bm-user-nextlevel",c.textContent="Next level in...",s.appendChild(c);let l=document.createElement("div");l.id="bm-contain-automation",l.appendChild(this.createInputCheckbox("Stealth","bm-input-stealth",!0)),l.appendChild(this.createButtonQuestion("bm-help-stealth","Help: Waits for the website to make requests, instead of sending requests.",this.outputStatusId)),l.appendChild(document.createElement("br")),l.appendChild(this.createInputCheckbox("Possessed","bm-input-possessed",!0)),l.appendChild(this.createButtonQuestion("bm-help-possessed","Help: Controls the website as if it were possessed.",this.outputStatusId)),l.appendChild(document.createElement("br"));let d=document.createElement("div");d.id="bm-contain-coords";let u=document.createElement("button");u.id="bm-button-coords",u.className="bm-help",u.style="margin-top: 0",u.innerHTML='',u.onclick=()=>{let m=this.apiHandler?.coordsTilePixel;if(m?.[0]){this.handleDisplayError("Coordinates are malformed! Did you try clicking on the canvas first?");return}this.updateInnerHTML("bm-input-tx",m?.[0]||"0000"),this.updateInnerHTML("bm-input-ty",m?.[1]||"0000"),this.updateInnerHTML("bm-input-px",m?.[2]||"0000"),this.updateInnerHTML("bm-input-py",m?.[3]||"0000")},d.appendChild(u),d.appendChild(this.createInputNumber("bm-input-tx","Tl X","","3","0","999","1")),d.appendChild(this.createInputNumber("bm-input-ty","Tl Y","","3","0","999","1")),d.appendChild(this.createInputNumber("bm-input-px","Px X","","3","0","999","1")),d.appendChild(this.createInputNumber("bm-input-py","Px Y","","3","0","999","1")),l.appendChild(d),l.appendChild(this.createInputFile("bm-input-file"));let b=document.createElement("div");b.id="bm-contain-buttons",b.appendChild(this.createButton("bm-button-enable","Enable")),b.appendChild(this.createButton("bm-button-disable","Disable")),l.appendChild(b);let f=document.createElement("textarea");f.id=this.outputStatusId,f.readOnly=!0,f.placeholder=`Status: Sleeping... +Version: ${this.version}`,l.appendChild(f),e.appendChild(o),e.appendChild(document.createElement("hr")),e.appendChild(s),e.appendChild(document.createElement("hr")),e.appendChild(l),document.body.appendChild(e),this.handleDrag(e,t)}updateInnerHTML(e,o,t=!1){let n=document.getElementById(e);if(n){if(n instanceof HTMLInputElement){n.value=o;return}t?n.textContent=o:n.innerHTML=o}}createButtonQuestion(e,o,t){let n=this.createButton(e,"?");return n.className="bm-help",n.title=o,n.onclick=()=>{this.updateInnerHTML(t,o)},n}createButton(e,o,t=!0){let n=document.createElement("button");return n.id=e,n.textContent=o,n.disabled=!t,n}createInputText(e,o="",t="",n="",i=!1){let s=document.createElement("input");return s.id=e,s.type="text",s.placeholder=o,s.value=t,s.readOnly=i,s.maxLength=n,s}createInputNumber(e,o="",t="",n="",i="",s="",a="",r=!1){let c=this.createInputText(e,o,t,n,r);return c.type="number",c.min=i,c.max=s,c.step=a,c}createInputCheckbox(e,o,t=!1){let n=document.createElement("label");n.textContent=e;let i=document.createElement("input");return i.type="checkbox",i.id=o,i.checked=t,n.prepend(i),n}createInputFile(e){let o=document.createElement("div"),t=document.createElement("input");t.id=e,t.type="file",t.style="display: none";let n=document.createElement("button");return n.textContent="Upload File",n.addEventListener("click",()=>{t.click()}),t.addEventListener("change",()=>{n.style.maxWidth=`${n.offsetWidth}px`,t.files.length>0?n.textContent=t.files[0].name:n.textContent="Upload File"}),o.appendChild(t),o.appendChild(n),o}handleDrag(e,o){let t=!1,n,i=0;o.addEventListener("mousedown",function(s){t=!0,n=s.clientX-e.getBoundingClientRect().left,i=s.clientY-e.getBoundingClientRect().top,document.body.style.userSelect="none",o.classList.add("dragging")}),o.addEventListener("touchstart",function(s){t=!0;let a=s?.touches?.[0];a&&(n=a.clientX-e.getBoundingClientRect().left,i=a.clientY-e.getBoundingClientRect().top,document.body.style.userSelect="none",o.classList.add("dragging"))},{passive:!1}),document.addEventListener("mousemove",function(s){t&&(e.style.left=s.clientX-n+"px",e.style.top=s.clientY-i+"px",e.style.right="")}),document.addEventListener("touchmove",function(s){if(t){let a=s?.touches?.[0];if(!a)return;e.style.left=a.clientX-n+"px",e.style.top=a.clientY-i+"px",s.preventDefault()}},{passive:!1}),document.addEventListener("mouseup",function(){t=!1,document.body.style.userSelect="",o.classList.remove("dragging")}),document.addEventListener("touchend",function(){t=!1,document.body.style.userSelect="",o.classList.remove("dragging")}),document.addEventListener("touchcancel",function(){t=!1,document.body.style.userSelect="",o.classList.remove("dragging")})}handleDisplayError(e){console.error(`${this.name}: ${e}`),this.updateInnerHTML(this.outputStatusId,"Error: "+e,!0)}};var x=class{constructor(){this.disableAll=!1,this.coordsTilePixel=[]}spontaneousResponseListener(e){window.addEventListener("message",o=>{let t=o.data;if(!(t&&t.source==="blue-marble"))return;let n=t.endpoint.split("?")[0].split("/").filter(i=>i&&isNaN(Number(i))).pop();switch(console.log(`Recieved message about "${n}"`),n){case"me":let i=Math.ceil(Math.pow(Math.floor(t.jsonData?.level)*Math.pow(30,.65),1.5384615384615383)-t.jsonData?.pixelsPainted);e.updateInnerHTML("bm-user-name",`Username: ${t.jsonData?.name}`),e.updateInnerHTML("bm-user-droplets",`Droplets: ${new Intl.NumberFormat().format(t.jsonData?.droplets)}`),e.updateInnerHTML("bm-user-nextlevel",`Next level in ${new Intl.NumberFormat().format(i)} pixel${i==1?"":"s"}`);break;case"pixel":let s=t.endpoint.split("?")[0].split("/").filter(c=>c&&!isNaN(Number(c))),a=new URLSearchParams(t.endpoint.split("?")[1]),r=[a.get("x"),a.get("y")];this.coordsTilePixel=[...s,...r];break;case"robots":this.disableAll=t.jsonData?.userscript?.toString().toLowerCase()=="false";break}})}};var y=GM_info.script.name.toString(),E=GM_info.script.version.toString();function L(p){let e=document.createElement("script");e.textContent=`(${p})();`,document.documentElement.appendChild(e),e.remove()}L(()=>{let p=window.fetch;window.fetch=async function(...e){let o=await p.apply(this,e),t=o.clone();if((t.headers.get("content-type")||"").includes("application/json")){let i=(e[0]instanceof Request?e[0]?.url:e[0])||"ignore";console.log(`Sending JSON message about endpoint "${i}"`),t.json().then(s=>{window.postMessage({source:"blue-marble",endpoint:i,jsonData:s},"*")}).catch(s=>{console.error("BM - Failed to parse JSON:",s)})}return o}});var I=GM_getResourceText("CSS-BM-File");GM_addStyle(I);var h=document.createElement("link");h.href="https://fonts.googleapis.com/css2?family=Roboto+Mono:ital,wght@0,100..700;1,100..700&display=swap";h.rel="preload";h.as="style";h.onload="this.onload=null;this.rel='stylesheet'";document.head.appendChild(h);var C=new g(y,E),v=new x;C.setApiHandler(v);C.create();v.spontaneousResponseListener(C);console.log(`${y} (${E}) userscript has loaded!`);})(); diff --git a/docs/README.md b/docs/README.md index e69c966..99bf077 100644 --- a/docs/README.md +++ b/docs/README.md @@ -35,7 +35,7 @@ Software License: MPL-2.0 Contact Me WakaTime -Total Patches +Total Patches Total Lines of Code Total Comments Build diff --git a/package-lock.json b/package-lock.json index 1423792..b9e5c24 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7,7 +7,7 @@ "devDependencies": { "esbuild": "^0.25.0" }, - "version": "0.39.2" + "version": "0.41.11" }, "node_modules/@esbuild/aix-ppc64": { "version": "0.25.8", @@ -467,5 +467,5 @@ } } }, - "version": "0.39.2" + "version": "0.41.11" } diff --git a/package.json b/package.json index df7b93f..e87e194 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "wplace-bluemarble", - "version": "0.41.0", + "version": "0.41.11", "type": "module", "scripts": { "build": "node build/build.js", diff --git a/src/BlueMarble.meta.js b/src/BlueMarble.meta.js index 2603893..d13cc9c 100644 --- a/src/BlueMarble.meta.js +++ b/src/BlueMarble.meta.js @@ -1,7 +1,7 @@ // ==UserScript== // @name Blue Marble // @namespace https://github.com/SwingTheVine/ -// @version 0.41.0 +// @version 0.41.11 // @description A userscript to automate and/or enhance the user experience on Wplace.live. Make sure to comply with the site's Terms of Service, and rules! This script is not affiliated with Wplace.live in any way, use at your own risk. This script is not affiliated with TamperMonkey. The author of this userscript is not responsible for any damages, issues, loss of data, or punishment that may occur as a result of using this script. This script is provided "as is" under the MPL-2.0 license. The "Blue Marble" icon is licensed under CC0 1.0 Universal (CC0 1.0) Public Domain Dedication. The image is owned by NASA. // @author SwingTheVine // @license MPL-2.0 diff --git a/src/apiHandler.js b/src/apiHandler.js index b97dd1f..270c5ec 100644 --- a/src/apiHandler.js +++ b/src/apiHandler.js @@ -9,6 +9,7 @@ export class ApiHandler { */ constructor() { this.disableAll = false; // Should the entire userscript be disabled? + this.coordsTilePixel = []; // Contains the last detected tile/pixel coordinate pair requested } /** Determines if the spontaneously recieved response is something we want. @@ -35,15 +36,25 @@ export class ApiHandler { // Each case is something that Blue Marble can use from the fetch. // For instance, if the fetch was for "me", we can update the overlay stats switch (endpointText) { - case 'me': + + case 'me': // Request to retrieve user data const nextLevelPixels = Math.ceil(Math.pow(Math.floor(data.jsonData?.level) * Math.pow(30, 0.65), (1/0.65)) - data.jsonData?.pixelsPainted); // Calculates pixels to the next level overlay.updateInnerHTML('bm-user-name', `Username: ${data.jsonData?.name}`); // Updates the text content of the username field overlay.updateInnerHTML('bm-user-droplets', `Droplets: ${new Intl.NumberFormat().format(data.jsonData?.droplets)}`); // Updates the text content of the droplets field overlay.updateInnerHTML('bm-user-nextlevel', `Next level in ${new Intl.NumberFormat().format(nextLevelPixels)} pixel${nextLevelPixels == 1 ? '' : 's'}`); // Updates the text content of the next level field break; - case 'robots': + + case 'pixel': // Request to retrieve pixel data + const coordsTile = data.endpoint.split('?')[0].split('/').filter(s => s && !isNaN(Number(s))); // Retrieves the tile coords as [x, y] + const payloadExtractor = new URLSearchParams(data.endpoint.split('?')[1]); // Declares a new payload deconstructor and passes in the fetch request payload + const coordsPixel = [payloadExtractor.get('x'), payloadExtractor.get('y')]; // Retrieves the deconstructed pixel coords from the payload + this.coordsTilePixel = [...coordsTile, ...coordsPixel]; // Combines the two arrays such that [x, y, x, y] + break; + + case 'robots': // Request to retrieve what script types are allowed this.disableAll = data.jsonData?.userscript?.toString().toLowerCase() == 'false'; // Disables Blue Marble if site owner wants userscripts disabled + break; } }); diff --git a/src/main.js b/src/main.js index e895376..2ff752f 100644 --- a/src/main.js +++ b/src/main.js @@ -72,9 +72,11 @@ stylesheetLink.onload = "this.onload=null;this.rel='stylesheet'"; document.head.appendChild(stylesheetLink); const overlay = new Overlay(name, version); // Constructs a new Overlay object -overlay.create(); // Deploys the overlay to the page - const apiHandler = new ApiHandler(); // Constructs a new ApiHandler object + +overlay.setApiHandler(apiHandler); // Sets the API handler + +overlay.create(); // Deploys the overlay to the page apiHandler.spontaneousResponseListener(overlay); // Reads spontaneous fetch responces console.log(`${name} (${version}) userscript has loaded!`); \ No newline at end of file diff --git a/src/overlay.css b/src/overlay.css index be363b0..6c210db 100644 --- a/src/overlay.css +++ b/src/overlay.css @@ -94,14 +94,22 @@ div#bm-overlay { } /* Tile (x, y) & Pixel (x, y) input fields */ -#bm-contain-coords input[type="text"] { - width: 5.5ch; +#bm-contain-coords input[type="number"] { + -moz-appearance: textfield; + width: 4.5ch; margin-left: 1ch; background-color: rgba(0, 0, 0, 0.2); padding: 0 0.5ch; font-size: small; } +/* Removes scroll bar on tile & pixel input fields */ +#bm-contain-coords input[type="number"]::-webkit-outer-spin-button, +#bm-contain-coords input[type="number"]::-webkit-inner-spin-button { + -webkit-appearance: none; + margin: 0; +} + /* Automation button container */ #bm-contain-buttons { display: flex; diff --git a/src/overlay.js b/src/overlay.js index 567bc4b..6be63f4 100644 --- a/src/overlay.js +++ b/src/overlay.js @@ -6,22 +6,28 @@ export class Overlay { /** Constructor for the Overlay class. * @param {string} name - The name of the userscript - * @param {string} version - The version of the userscript + * @param {string} version - The version of the userscript * @since 0.0.2 * @see {@link Overlay} */ constructor(name, version) { - this.name = name; - this.version = version; + this.name = name; // Name of userscript + this.version = version; // Version of userscript + this.apiHandler = null; // The API handler instance. Later populated when setApiHandler is called + this.outputStatusId = 'bm-output-status'; // ID for status element } + /** Populates the apiHandler variable with the apiHandler class. + * @param {apiHandler} apiHandler - The apiHandler class instance + * @since 0.41.4 + */ + setApiHandler(apiHandler) {this.apiHandler = apiHandler;} + /** Creates and deploys the overlay element * @since 0.0.2 */ create() { - const outputStatusId = 'bm-output-status'; // ID for status element - const overlay = document.createElement('div'); // Creates a new
element for the overlay overlay.id = 'bm-overlay'; overlay.style.top = '10px'; // Position from top of viewport @@ -75,7 +81,7 @@ export class Overlay { containerAutomation.appendChild(this.createButtonQuestion( 'bm-help-stealth', 'Help: Waits for the website to make requests, instead of sending requests.', - outputStatusId + this.outputStatusId )); containerAutomation.appendChild(document.createElement('br')); // Line break @@ -91,7 +97,7 @@ export class Overlay { containerAutomation.appendChild(this.createButtonQuestion( 'bm-help-possessed', 'Help: Controls the website as if it were possessed.', - outputStatusId + this.outputStatusId )); containerAutomation.appendChild(document.createElement('br')); // Line break @@ -105,15 +111,23 @@ export class Overlay { buttonCoords.style = 'margin-top: 0'; buttonCoords.innerHTML = ''; buttonCoords.onclick = () => { - //this.updateInnerHTML(outputId, tooltip); // Update output element text with tooltip on click + const coords = this.apiHandler?.coordsTilePixel; // Retrieves the coords from the API handler + if (!coords?.[0]) { + this.handleDisplayError('Coordinates are malformed! Did you try clicking on the canvas first?'); + return; + } + this.updateInnerHTML('bm-input-tx', coords?.[0] || '000'); + this.updateInnerHTML('bm-input-ty', coords?.[1] || '000'); + this.updateInnerHTML('bm-input-px', coords?.[2] || '000'); + this.updateInnerHTML('bm-input-py', coords?.[3] || '000'); } containerAutomationCoords.appendChild(buttonCoords); // Adds the coordinate button to the automation container // Tile (x,y) and Pixel (x,y) input fields - containerAutomationCoords.appendChild(this.createInputText('bm-input-tx', 'Tl X', '', '4')); - containerAutomationCoords.appendChild(this.createInputText('bm-input-ty', 'Tl Y', '', '4')); - containerAutomationCoords.appendChild(this.createInputText('bm-input-px', 'Px X', '', '4')); - containerAutomationCoords.appendChild(this.createInputText('bm-input-py', 'Px Y', '', '4')); + containerAutomationCoords.appendChild(this.createInputNumber('bm-input-tx', 'Tl X', '', '3', '0', '999', '1')); + containerAutomationCoords.appendChild(this.createInputNumber('bm-input-ty', 'Tl Y', '', '3', '0', '999', '1')); + containerAutomationCoords.appendChild(this.createInputNumber('bm-input-px', 'Px X', '', '3', '0', '999', '1')); + containerAutomationCoords.appendChild(this.createInputNumber('bm-input-py', 'Px Y', '', '3', '0', '999', '1')); containerAutomation.appendChild(containerAutomationCoords); // Adds coord container to automation container @@ -129,7 +143,7 @@ export class Overlay { containerAutomation.appendChild(containerAutomationButtons); // Adds button container to automation container const outputStatus = document.createElement('textarea'); // Outputs script status - outputStatus.id = outputStatusId; + outputStatus.id = this.outputStatusId; outputStatus.readOnly = true; // Read-only input field outputStatus.placeholder = `Status: Sleeping...\nVersion: ${this.version}`; // Default text value containerAutomation.appendChild(outputStatus); @@ -225,6 +239,27 @@ export class Overlay { return input; } + /** Creates a number input field + * @param {string} inputId - The ID for the number input + * @param {string} [placeholder] - (Optional) The placeholder text of the input + * @param {string} [text] - (Optional) The text content of the input + * @param {string} [maxLength] - (Optional) The maximum character amount of the input + * @param {string} [numMin] - (Optional) The minimum possible number the user can input + * @param {string} [numMax] - (Optional) The maximum possible number the user can input + * @param {string} [numStep] - (Optional) The increment that numbers are considered valid inputs. E.g. a step of "1" will only allow integers + * @param {boolean} [isReadOnly] - (Optional) Should the input be readOnly? False by default + * @returns {HTMLInputElement} HTML Input Element + * @since 0.41.9 + */ + createInputNumber(inputId, placeholder='', text='', maxLength='', numMin='', numMax='', numStep='', isReadOnly=false) { + const input = this.createInputText(inputId, placeholder, text, maxLength, isReadOnly); // A shortcut :P + input.type = 'number'; + input.min = numMin; + input.max = numMax; + input.step = numStep; + return input; + } + /** Creates the checkbox input * @param {string} labelText - The text for the label * @param {string} checkboxId - The ID for the checkbox input @@ -348,4 +383,15 @@ export class Overlay { barDrag.classList.remove('dragging'); // Removes the dragging class }); } + + /** Handles error display. + * This will output plain text into the output Status box. + * Additionally, this will output an error to the console. + * @param {string} text - The error text to display. + * @since 0.41.6 + */ + handleDisplayError(text) { + console.error(`${this.name}: ${text}`); // Outputs something like "ScriptName: text" as an error to the console + this.updateInnerHTML(this.outputStatusId, 'Error: ' + text, true); + } } \ No newline at end of file