Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions src/ext/extension-page/Components/Sidebar/Sidebar.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@
scrollToEl(active.filename);
}

/**
* @param {"js"|"css"} type
* @param {string=} content
* @param {string=} remote
*/
async function newItem(type, content, remote) {
if (!["js", "css", "dnr"].includes(type)) return;
// warn if there are unsaved changes or another temp script
const temp = $items.find((i) => i.temp);
if ((cmChanged() || temp) && !warn()) return;
Expand Down Expand Up @@ -60,7 +66,10 @@
};
// if it's a remotely added script add prop to item object
// is used to display url in editor and will be removed on save
if (remote) item.remote = remote;
if (remote) {
item.remote = remote;
item.name = remote;
}
items.update((i) => [item, ...i]);
await tick(); // if omitted invalid arg in activate function
activate(item);
Expand Down Expand Up @@ -111,17 +120,36 @@
const temp = $items.find((i) => i.temp);
if ((cmChanged() || temp) && !warn()) return;
// prompt user for url
const url = prompt("Enter remote url:");
const input = prompt("Enter remote url:");
// stop execution is user cancels prompt
if (!url) return;
state.add("fetching");
const message = { name: "PAGE_NEW_REMOTE", url };
const response = await sendNativeMessage(message);
if (response.error) {
log.add(response.error, "error", true);
if (input === null) return;
/** @type {URL} */
let url;
/** @type {"js"|"css"} */
let type;
try {
url = new URL(input.trim());
} catch {
log.add("Failed to get remote content, invalid url", "error", true);
return;
}
// check file type
if (url.pathname.endsWith(".js")) {
type = "js";
} else if (url.pathname.endsWith(".css")) {
type = "css";
} else {
const type = url.substring(url.lastIndexOf(".") + 1);
newItem(type, response, url);
log.add("Failed to get remote content, invalid file type", "error", true);
return;
}
state.add("fetching");
try {
const res = await fetch(url);
if (!res.ok) throw new Error(`httpcode-${res.status}`);
const content = await res.text();
newItem(type, content, url.href);
} catch (error) {
log.add(`Failed to get remote content - ${error}`, "error", true);
}
state.remove("fetching");
}
Expand Down
15 changes: 0 additions & 15 deletions xcode/Ext-Safari/SafariWebExtensionHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -258,21 +258,6 @@ class SafariWebExtensionHandler: NSObject, NSExtensionRequestHandling {
}
response.userInfo = [SFExtensionMessageKey: ["success": true]]
}
else if name == "PAGE_NEW_REMOTE" {
#if os(macOS)
if let url = message?["url"] as? String {
if !validateUrl(url) {
response.userInfo = [SFExtensionMessageKey: ["error": "Failed to get remote content, invalid url"]]
} else if let content = getRemoteFileContents(url) {
response.userInfo = [SFExtensionMessageKey: content]
} else {
response.userInfo = [SFExtensionMessageKey: ["error": "Failed to get remote content"]]
}
} else {
inBoundError = true
}
#endif
}
else if name == "PAGE_UPDATE_SETTINGS" {
#if os(macOS)
if let settings = message?["settings"] as? [String: String] {
Expand Down