mirror of
https://github.com/vale981/tridactyl
synced 2025-03-06 01:51:40 -05:00
Minor code cleanups
This commit makes error messages when the native messenger is unavailable easier to read. Since they're easier to read, there's no need for custom errors in setclip/getclip anymore, provided that the errors they throw are correctly logged. In order to make sure of that, we remove the try/catch in excmds.ts:clipboard(), which should let errors bubble up as needed. I also noticed that while {set,get}Clipboard relied on the command line being focused in order to work, they didn't do that themselves and instead expected their callers to have set things up. This didn't make sense to me so I moved the focusing code inside of {set,get}Clipboard. This was all done while chasing the elusive #1135 but probably doesn't change anything about it.
This commit is contained in:
parent
9cf2c2f205
commit
7b5f7ef298
3 changed files with 58 additions and 72 deletions
|
@ -51,7 +51,7 @@ async function sendNativeMsg(
|
||||||
return resp as MessageResp
|
return resp as MessageResp
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (!quiet) {
|
if (!quiet) {
|
||||||
throw e
|
throw new Error("Failed to send message to native messenger. If it is correctly installed (run `:native`), please report this bug on https://github.com/tridactyl/tridactyl/issues .")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -385,6 +385,7 @@ function applyWithTmpTextArea(fn) {
|
||||||
|
|
||||||
/** @hidden **/
|
/** @hidden **/
|
||||||
export async function setClipboard(content: string) {
|
export async function setClipboard(content: string) {
|
||||||
|
await Messaging.messageOwnTab("commandline_content", "focus")
|
||||||
applyWithTmpTextArea(scratchpad => {
|
applyWithTmpTextArea(scratchpad => {
|
||||||
scratchpad.value = content
|
scratchpad.value = content
|
||||||
scratchpad.select()
|
scratchpad.select()
|
||||||
|
@ -394,20 +395,21 @@ export async function setClipboard(content: string) {
|
||||||
} else throw "Failed to copy!"
|
} else throw "Failed to copy!"
|
||||||
})
|
})
|
||||||
// Return focus to the document
|
// Return focus to the document
|
||||||
Messaging.messageOwnTab("commandline_content", "hide")
|
await Messaging.messageOwnTab("commandline_content", "hide")
|
||||||
Messaging.messageOwnTab("commandline_content", "blur")
|
return Messaging.messageOwnTab("commandline_content", "blur")
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @hidden **/
|
/** @hidden **/
|
||||||
export function getClipboard() {
|
export async function getClipboard() {
|
||||||
|
await Messaging.messageOwnTab("commandline_content", "focus")
|
||||||
const result = applyWithTmpTextArea(scratchpad => {
|
const result = applyWithTmpTextArea(scratchpad => {
|
||||||
scratchpad.focus()
|
scratchpad.focus()
|
||||||
document.execCommand("Paste")
|
document.execCommand("Paste")
|
||||||
return scratchpad.textContent
|
return scratchpad.textContent
|
||||||
})
|
})
|
||||||
// Return focus to the document
|
// Return focus to the document
|
||||||
Messaging.messageOwnTab("commandline_content", "hide")
|
await Messaging.messageOwnTab("commandline_content", "hide")
|
||||||
Messaging.messageOwnTab("commandline_content", "blur")
|
await Messaging.messageOwnTab("commandline_content", "blur")
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
116
src/excmds.ts
116
src/excmds.ts
|
@ -2487,8 +2487,8 @@ export async function get_current_url() {
|
||||||
* Copy content to clipboard without feedback. Use `clipboard yank` for interactive use.
|
* Copy content to clipboard without feedback. Use `clipboard yank` for interactive use.
|
||||||
*/
|
*/
|
||||||
//#background
|
//#background
|
||||||
export async function yank(...content: string[]) {
|
export function yank(...content: string[]) {
|
||||||
await setclip(content.join(" "))
|
return setclip(content.join(" "))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2501,16 +2501,8 @@ async function setclip(str) {
|
||||||
// Functions to avoid retyping everything everywhere
|
// Functions to avoid retyping everything everywhere
|
||||||
|
|
||||||
// Note: We're using fillcmdline here because exceptions are somehow not caught. We're rethrowing because otherwise the error message will be overwritten with the "yank successful" message.
|
// Note: We're using fillcmdline here because exceptions are somehow not caught. We're rethrowing because otherwise the error message will be overwritten with the "yank successful" message.
|
||||||
let s = () =>
|
let s = () => Native.clipboard("set", str)
|
||||||
Native.clipboard("set", str).catch(e => {
|
let c = () => messageActiveTab("commandline_frame", "setClipboard", [str])
|
||||||
let msg = "# Failed to set X selection. Is the native messenger installed and is it >=v.0.1.7?"
|
|
||||||
fillcmdline(msg)
|
|
||||||
throw msg
|
|
||||||
})
|
|
||||||
let c = async () => {
|
|
||||||
await messageActiveTab("commandline_content", "focus")
|
|
||||||
await messageActiveTab("commandline_frame", "setClipboard", [str])
|
|
||||||
}
|
|
||||||
|
|
||||||
let promises = []
|
let promises = []
|
||||||
switch (await config.getAsync("yankto")) {
|
switch (await config.getAsync("yankto")) {
|
||||||
|
@ -2524,7 +2516,7 @@ async function setclip(str) {
|
||||||
promises = [s(), c()]
|
promises = [s(), c()]
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
return await Promise.all(promises)
|
return Promise.all(promises)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -2537,11 +2529,7 @@ async function getclip() {
|
||||||
if ((await config.getAsync("putfrom")) == "clipboard") {
|
if ((await config.getAsync("putfrom")) == "clipboard") {
|
||||||
return messageActiveTab("commandline_frame", "getClipboard")
|
return messageActiveTab("commandline_frame", "getClipboard")
|
||||||
} else {
|
} else {
|
||||||
return Native.clipboard("get", "").catch(e => {
|
return Native.clipboard("get", "")
|
||||||
let msg = "# Failed to get X selection. Is the native messenger installed?"
|
|
||||||
fillcmdline(msg)
|
|
||||||
throw msg
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2571,56 +2559,52 @@ export async function clipboard(excmd: "open" | "yank" | "yankshort" | "yankcano
|
||||||
let content = toYank.join(" ")
|
let content = toYank.join(" ")
|
||||||
let url = ""
|
let url = ""
|
||||||
let urls = []
|
let urls = []
|
||||||
try {
|
switch (excmd) {
|
||||||
switch (excmd) {
|
case "yankshort":
|
||||||
case "yankshort":
|
urls = await geturlsforlinks("rel", "shortlink")
|
||||||
urls = await geturlsforlinks("rel", "shortlink")
|
if (urls.length == 0) {
|
||||||
if (urls.length == 0) {
|
urls = await geturlsforlinks("rev", "canonical")
|
||||||
urls = await geturlsforlinks("rev", "canonical")
|
}
|
||||||
}
|
if (urls.length > 0) {
|
||||||
if (urls.length > 0) {
|
await yank(urls[0])
|
||||||
await yank(urls[0])
|
fillcmdline_tmp(3000, "# " + urls[0] + " copied to clipboard.")
|
||||||
fillcmdline_tmp(3000, "# " + urls[0] + " copied to clipboard.")
|
|
||||||
break
|
|
||||||
}
|
|
||||||
// Trying yankcanon if yankshort failed...
|
|
||||||
case "yankcanon":
|
|
||||||
urls = await geturlsforlinks("rel", "canonical")
|
|
||||||
if (urls.length > 0) {
|
|
||||||
await yank(urls[0])
|
|
||||||
fillcmdline_tmp(3000, "# " + urls[0] + " copied to clipboard.")
|
|
||||||
break
|
|
||||||
}
|
|
||||||
// Trying yank if yankcanon failed...
|
|
||||||
case "yank":
|
|
||||||
content = content == "" ? (await activeTab()).url : content
|
|
||||||
await yank(content)
|
|
||||||
fillcmdline_tmp(3000, "# " + content + " copied to clipboard.")
|
|
||||||
break
|
break
|
||||||
case "yanktitle":
|
}
|
||||||
content = (await activeTab()).title
|
// Trying yankcanon if yankshort failed...
|
||||||
await yank(content)
|
case "yankcanon":
|
||||||
fillcmdline_tmp(3000, "# " + content + " copied to clipboard.")
|
urls = await geturlsforlinks("rel", "canonical")
|
||||||
|
if (urls.length > 0) {
|
||||||
|
await yank(urls[0])
|
||||||
|
fillcmdline_tmp(3000, "# " + urls[0] + " copied to clipboard.")
|
||||||
break
|
break
|
||||||
case "yankmd":
|
}
|
||||||
content = "[" + (await activeTab()).title + "](" + (await activeTab()).url + ")"
|
// Trying yank if yankcanon failed...
|
||||||
await yank(content)
|
case "yank":
|
||||||
fillcmdline_tmp(3000, "# " + content + " copied to clipboard.")
|
content = content == "" ? (await activeTab()).url : content
|
||||||
break
|
await yank(content)
|
||||||
case "open":
|
fillcmdline_tmp(3000, "# " + content + " copied to clipboard.")
|
||||||
url = await getclip()
|
break
|
||||||
url && open(url)
|
case "yanktitle":
|
||||||
break
|
content = (await activeTab()).title
|
||||||
case "tabopen":
|
await yank(content)
|
||||||
url = await getclip()
|
fillcmdline_tmp(3000, "# " + content + " copied to clipboard.")
|
||||||
url && tabopen(url)
|
break
|
||||||
break
|
case "yankmd":
|
||||||
default:
|
content = "[" + (await activeTab()).title + "](" + (await activeTab()).url + ")"
|
||||||
// todo: maybe we should have some common error and error handler
|
await yank(content)
|
||||||
throw new Error(`[clipboard] unknown excmd: ${excmd}`)
|
fillcmdline_tmp(3000, "# " + content + " copied to clipboard.")
|
||||||
}
|
break
|
||||||
} catch (e) {
|
case "open":
|
||||||
logger.error(e)
|
url = await getclip()
|
||||||
|
url && open(url)
|
||||||
|
break
|
||||||
|
case "tabopen":
|
||||||
|
url = await getclip()
|
||||||
|
url && tabopen(url)
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
// todo: maybe we should have some common error and error handler
|
||||||
|
throw new Error(`[clipboard] unknown excmd: ${excmd}`)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue