Skip to content

Commit 5f51319

Browse files
authored
Merge pull request quoid#440 from quoid/refactor/popup_dev
Refactor `popup` development experience
2 parents 5e8e08a + 8e5a88f commit 5f51319

5 files changed

Lines changed: 78 additions & 22 deletions

File tree

src/page/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ if (import.meta.env.DEV) { // vite feat that only import in dev mode
88
const modules = import.meta.glob("../shared/dev.js", {eager: true});
99
// eslint-disable-next-line no-global-assign
1010
browser = modules["../shared/dev.js"].browser;
11-
console.log(import.meta.env, modules, browser);
11+
console.info("DEV-ENV", import.meta.env, modules, browser);
1212
}
1313

1414
const app = new App({

src/popup/App.svelte

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -392,10 +392,13 @@
392392
393393
async function installCheck(currentTab) {
394394
// refetch script from URL to avoid tampered DOM content
395-
const res = await fetch(currentTab.url);
396-
if (!res.ok) {
397-
console.error(`Error fetching .user.js url: httpcode-${res.status}`);
398-
errorNotification = `Fetching failed, refresh to retry. (${res.status})`;
395+
let res; // fetch response
396+
try {
397+
res = await fetch(currentTab.url);
398+
if (!res.ok) throw new Error(`httpcode-${res.status}`);
399+
} catch (error) {
400+
console.error("Error fetching .user.js url", error);
401+
errorNotification = "Fetching failed, refresh to retry.";
399402
showInstallPrompt = undefined;
400403
return;
401404
}
@@ -437,11 +440,11 @@
437440
showInstall = false;
438441
// double check before send install message
439442
if (!installUserscript || !installUserscript.content) {
440-
errorNotification = "install failed: userscript missing";
443+
errorNotification = "Install failed: userscript missing";
441444
}
442445
const currentTab = await browser.tabs.getCurrent();
443446
if (currentTab.url !== installUserscript.url) {
444-
errorNotification = "install failed: tab changed unexpectedly";
447+
errorNotification = "Install failed: tab changed unexpectedly";
445448
}
446449
if (errorNotification) {
447450
disabled = false;

src/popup/main.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,30 @@ if (import.meta.env.DEV) { // vite feat that only import in dev mode
77
const modules = import.meta.glob("../shared/dev.js", {eager: true});
88
// eslint-disable-next-line no-global-assign
99
browser = modules["../shared/dev.js"].browser;
10-
console.log(import.meta.env, modules, browser);
10+
console.info("DEV-ENV", import.meta.env, modules, browser);
11+
// macos popup style
12+
const style = document.createElement("style");
13+
style.textContent = `
14+
body {
15+
top: 20px;
16+
left: 20px;
17+
box-sizing: content-box;
18+
border: 2px solid #0c0e0f;
19+
border-radius: 10px;
20+
box-shadow: 2px 2px 20px rgba(0, 0, 0, 0.2);
21+
}
22+
body:before {
23+
content: "";
24+
position: absolute;
25+
top: -1px;
26+
left: -1px;
27+
right: -1px;
28+
bottom: -1px;
29+
border: 1px solid #54575a;
30+
border-radius: 9px;
31+
}
32+
`;
33+
browser.platform === "macos" && document.head.append(style);
1134
}
1235

1336
const app = new App({

src/shared/dev.js

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const files = [
6464

6565
const _browser = {
6666
delay: 200,
67-
platform: "ios",
67+
platform: "macos", // ios || macos
6868
runtime: {
6969
getURL() {
7070
return "https://www.example.com/";
@@ -360,6 +360,20 @@ const _browser = {
360360
]
361361
};
362362
}
363+
} else if (name === "POPUP_INSTALL_CHECK") {
364+
response = random([
365+
{success: "Click to install", installed: false},
366+
{success: "Click to re-install", installed: true}
367+
]);
368+
response.metadata = {
369+
description: "This userscript re-implements the \"View Image\" and \"Search by image\" buttons into google images.",
370+
grant: ["GM.getValue", "GM.setValue", "GM.xmlHttpRequest"],
371+
match: ["https://www.example.com/*", "https://www.example.com/somethingReallylong/goesRightHere"],
372+
name: "Test Install Userscript",
373+
require: ["https://code.jquery.com/jquery-3.5.1.min.js", "https://code.jquery.com/jquery-1.7.1.min.js"],
374+
source: "https://greasyforx.org/scripts/00000-something-something-long-name/code/Something%20something%20long20name.user.js"
375+
};
376+
// response.error = "something went wrong (dev)";
363377
}
364378
if (!responseCallback) {
365379
return new Promise(resolve => {
@@ -376,7 +390,13 @@ const _browser = {
376390
},
377391
tabs: {
378392
getCurrent(/* responseCallback */) {
379-
const response = {url: "https://www.filmgarb.com/foo.user.js", id: 101};
393+
const response = random([
394+
{url: "https://www.filmgarb.com/foo.user.js", id: 101},
395+
{url: `${window.location.origin}/src/shared/dev/DEMO.Alert-URL.user.js`, id: 102},
396+
{url: `${window.location.origin}/src/shared/dev/DEMO.Alert-URL.user.js`, id: 103}, // increase probability
397+
{url: window.location.href, id: 10}
398+
]);
399+
console.info("browser.tabs.getCurrent", response);
380400
return new Promise(resolve => {
381401
setTimeout(() => resolve(response), _browser.delay);
382402
});
@@ -392,18 +412,8 @@ const _browser = {
392412
},
393413
sendMessage(tabId, message, responseCallback) {
394414
let response = {};
395-
if (message.name === "POPUP_INSTALL_CHECK") {
396-
response = {
397-
success: "Click to install (test)",
398-
metadata: {
399-
description: "This userscript re-implements the \"View Image\" and \"Search by image\" buttons into google images.",
400-
grant: ["GM.getValue", "GM.setValue", "GM.xmlHttpRequest"],
401-
match: ["https://www.example.com/*", "https://www.example.com/somethingReallylong/goesRightHere"],
402-
name: "Test Install Userscript",
403-
require: ["https://code.jquery.com/jquery-3.5.1.min.js", "https://code.jquery.com/jquery-1.7.1.min.js"],
404-
source: "https://greasyforx.org/scripts/00000-something-something-long-name/code/Something%20something%20long20name.user.js"
405-
}
406-
};
415+
if (message.name === "DEMO_MSG") {
416+
response = {};
407417
// response.error = "something went wrong (dev)";
408418
}
409419
if (!responseCallback) {
@@ -481,6 +491,10 @@ function saveFile(content, lastMod, newFilename, oldName) {
481491
}
482492
}
483493

494+
function random(array) {
495+
return array[Math.floor(Math.random() * array.length)];
496+
}
497+
484498
export const browser = _browser;
485499

486500
export default {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// ==UserScript==
2+
// @name DEMO.Alert-URL
3+
// @description Demo user script alert URL.
4+
// @author Userscripts
5+
// @version 1.0.0
6+
// @match *://*/*
7+
// @grant none
8+
// @inject-into content
9+
// @run-at document-start
10+
// ==/UserScript==
11+
12+
/* eslint-disable */
13+
(function () {
14+
'use strict';
15+
alert("DEBUG.Alert-URL:\n\n" + location);
16+
})();

0 commit comments

Comments
 (0)