-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Escape key shortcut. Add Firefox permission prompt.
- Loading branch information
Showing
10 changed files
with
173 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,55 @@ | ||
import * as browser from 'webextension-polyfill' | ||
|
||
// @ts-expect-error: Parcel mini-DSL is unsupported. | ||
import requestPermUrl from 'url:./request-perm.html' | ||
|
||
const invokeCmd = 'invoke' | ||
const invokeMenuId = 'iucInvoke' | ||
|
||
async function getInvokeShortcut(): Promise<string | null> { | ||
const invokeCmd = [...(await browser.commands.getAll())].filter(c => c.name === 'invoke')[0] | ||
return invokeCmd.shortcut && !!invokeCmd.shortcut.length ? invokeCmd.shortcut : null | ||
const cmd = [...(await browser.commands.getAll())].filter(c => c.name === invokeCmd)[0] | ||
return cmd.shortcut && !!cmd.shortcut.length ? cmd.shortcut : null | ||
} | ||
|
||
async function invoke(tab: browser.Tabs.Tab, showShortcut: boolean) { | ||
const shortcut = showShortcut ? await getInvokeShortcut() : null | ||
try { | ||
await browser.tabs.sendMessage(tab.id, { cmd: 'invoke', shortcut }) | ||
await browser.tabs.sendMessage(tab.id, { cmd: invokeCmd, shortcut }) | ||
} | ||
catch (e) { } | ||
} | ||
|
||
browser.runtime.onInstalled.addListener(async () => { | ||
async function checkAndMaybeRequestPermission(url: string) { | ||
const contentScriptAllOrigins: browser.Permissions.AnyPermissions = { | ||
permissions: [], | ||
origins: ['*://*/*'], | ||
} | ||
const hasPerm = await browser.permissions.contains(contentScriptAllOrigins) | ||
if (!hasPerm) | ||
browser.tabs.create({ url }) | ||
} | ||
|
||
browser.runtime.onInstalled.addListener(async (_info) => { | ||
browser.contextMenus.create({ | ||
id: 'iucInvoke', | ||
id: invokeMenuId, | ||
title: browser.i18n.getMessage('invoke_cmd_desc'), | ||
contexts: ['all'], | ||
}) | ||
|
||
const permUrl = browser.runtime.getURL(requestPermUrl) | ||
const isFirefox = permUrl.startsWith('moz-extension://') | ||
if (isFirefox) | ||
await checkAndMaybeRequestPermission(permUrl) | ||
}) | ||
|
||
browser.commands.onCommand.addListener(async (command) => { | ||
if (command === 'invoke') { | ||
if (command === invokeCmd) { | ||
const [tab] = await browser.tabs.query({ active: true, lastFocusedWindow: true }) | ||
await invoke(tab, false) | ||
} | ||
}) | ||
|
||
browser.contextMenus.onClicked.addListener(async (info, tab) => { | ||
if (info.menuItemId === 'iucInvoke') | ||
if (info.menuItemId === invokeMenuId) | ||
await invoke(tab, true) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<link rel="stylesheet" href="./request-perm.sass" /> | ||
<link rel="icon" type="image/png" href="img/icon-128.png" /> | ||
</head> | ||
|
||
<body> | ||
<main></main> | ||
<script src="./request-perm.tsx" type="module"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
body | ||
font-family: sans-serif | ||
margin: 0 | ||
padding: 0 | ||
display: flex | ||
justify-content: center | ||
align-self: flex-start | ||
width: 100vw | ||
height: 100vh | ||
|
||
main | ||
display: flex | ||
flex-direction: column | ||
margin: 0 | ||
padding: 2rem | ||
max-width: 650px | ||
gap: 2rem | ||
|
||
h1, p, button | ||
margin: 0 | ||
padding: 0 | ||
|
||
h1 | ||
font-size: 1.5rem | ||
font-weight: bold | ||
display: flex | ||
flex-direction: column | ||
align-items: center | ||
|
||
&::before | ||
content: '' | ||
display: block | ||
width: 70px | ||
height: 70px | ||
background: url('data-url:./img/icon.svg') no-repeat | ||
|
||
p | ||
text-align: justify | ||
|
||
&.denied | ||
color: #933333 | ||
|
||
button | ||
font: inherit | ||
color: white | ||
padding: 0.8rem | ||
font-weight: bold | ||
background: #336693 | ||
border-radius: 3px | ||
border: none | ||
|
||
&:hover | ||
background: lighten(#336693, 10%) | ||
box-shadow: 0 0 4px rgba(0,0,0,0.75) | ||
|
||
&:active | ||
opacity: 0.75 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import * as browser from 'webextension-polyfill' | ||
|
||
import { render } from 'preact' | ||
import { useState } from 'preact/hooks' | ||
|
||
(async function () { | ||
function Page() { | ||
const [denied, setDenied] = useState(false) | ||
|
||
async function requestPerm() { | ||
setDenied(false) | ||
const contentScriptAllOrigins: browser.Permissions.Permissions = { | ||
permissions: [], | ||
origins: ['*://*/*'], | ||
} | ||
const gotPermission = await browser.permissions.request(contentScriptAllOrigins) | ||
if (gotPermission) | ||
window.close() | ||
else | ||
setDenied(true) | ||
} | ||
|
||
return ( | ||
<> | ||
<h1>{browser.i18n.getMessage('req_perm_title')}</h1> | ||
<p>{browser.i18n.getMessage('req_perm_text')}</p> | ||
<button onClick={requestPerm}>{browser.i18n.getMessage('req_perm_btn')}</button> | ||
{denied ? <p className="denied">{browser.i18n.getMessage('req_perm_denied')}</p> : null} | ||
</> | ||
) | ||
} | ||
|
||
document.title = browser.i18n.getMessage('req_perm_title') | ||
render(<Page />, document.querySelector('main'), | ||
) | ||
})() |