A fully open source & end-to-end encrypted note taking alternative to Evernote.
TypeScript asp-net-coredotnet-coreelectronfosshacktoberfestnote-managmentnote-takingnote-taking-appnotebooknotesnotes-appopen-sourceopensourceprivacyproductivityreactreact-nativereactjsself-hosted
clipper: load content scripts on demand to reduce host permissions
Abdullah Atta153ee4bce383
diff --git a/extensions/web-clipper/build-utils/manifest.js b/extensions/web-clipper/build-utils/manifest.jsindex 10ccad224..ed53c5aa8 100644--- a/extensions/web-clipper/build-utils/manifest.js+++ b/extensions/web-clipper/build-utils/manifest.js@@ -31,37 +31,27 @@ const ACTION = {default_popup: "popup.html"};+const nnHost =+ process.env.NODE_ENV === "production"+ ? "*://app.notesnook.com/*"+ : "*://localhost/*";+const corsHost = "https://cors.notesnook.com/*";const common = {name: "Notesnook Web Clipper",version: "0.2",description:"Clip web pages & save interesting things you find on the web directly into Notesnook in a private & secure way.",permissions: ["activeTab", "tabs", "storage", "notifications"],- content_scripts: [- {- js: ["nnContentScript.bundle.js"],- matches:- process.env.NODE_ENV === "production"- ? ["*://app.notesnook.com/*"]- : ["*://localhost/*"]- },- {- js: ["contentScript.bundle.js"],- matches: ["http://*/*", "https://*/*"],- exclude_matches:- process.env.NODE_ENV === "production"- ? ["*://app.notesnook.com/*"]- : ["*://localhost/*"]- }- ],icons: ICONS};const v2 = {...common,+ permissions: [...common.permissions, corsHost, nnHost],optional_permissions: ["http://*/*", "https://*/*"],browser_specific_settings: {gecko: {+ id: "[email protected]",strict_min_version: "105.0"}},@@ -74,6 +64,7 @@ const v2 = {const v3 = {...common,+ host_permissions: [corsHost, nnHost],optional_host_permissions: ["http://*/*", "https://*/*"],manifest_version: 3,background: {diff --git a/extensions/web-clipper/src/api.ts b/extensions/web-clipper/src/api.tsindex ea6a16b1d..7d0276f22 100644--- a/extensions/web-clipper/src/api.ts+++ b/extensions/web-clipper/src/api.ts@@ -49,16 +49,27 @@ function timeout(ms: number) {return new Promise((resolve) => setTimeout(resolve, ms, false));}-function connectToTab(tab: Tabs.Tab, onDisconnect?: () => void) {- return new Promise<Remote<Server> | false>(function connect(resolve) {- if (!tab.id) return resolve(false);+async function connectToTab(tab: Tabs.Tab, onDisconnect?: () => void) {+ if (!tab.id) return false;- const port = browser.tabs.connect(tab.id);- port.onDisconnect.addListener(() => {- api = undefined;- onDisconnect?.();+ if (browser.scripting) {+ await browser.scripting.executeScript({+ files: ["nnContentScript.bundle.js"],+ target: { tabId: tab.id }+ });+ } else {+ await browser.tabs.executeScript(tab.id, {+ file: "nnContentScript.bundle.js"});+ }+ const port = browser.tabs.connect(tab.id);+ port.onDisconnect.addListener(() => {+ api = undefined;+ onDisconnect?.();+ });++ return new Promise<Remote<Server> | false>(function connect(resolve) {async function onMessage(message: WebExtensionChannelMessage,port: Runtime.Portdiff --git a/extensions/web-clipper/src/views/main.tsx b/extensions/web-clipper/src/views/main.tsxindex 13e991c5e..a6b6902e1 100644--- a/extensions/web-clipper/src/views/main.tsx+++ b/extensions/web-clipper/src/views/main.tsx@@ -162,42 +162,44 @@ export function Main() {})();}, [settings]);- if (!hasPermission) {- <FlexScrollContainer style={{ maxHeight: 560 }}>- <Flex- sx={{- flexDirection: "column",- p: 2,- width: 320,- backgroundColor: "background"- }}- >- <Text- variant="subtitle"- sx={{ mt: 2, mb: 1, color: "icon", fontSize: "body" }}- >- Permission required- </Text>- <Button- onClick={async () => {- if (settings?.corsProxy) {- if (- !(await browser.permissions.request({- origins: [`${settings.corsProxy}/*`]- }))- )- throw new Error(- "You must give the required permission in order to use the web clipper."- );-- setHasPermission(true);- }+ if (!hasPermission && !!settings?.corsProxy) {+ return (+ <FlexScrollContainer style={{ maxHeight: 560 }}>+ <Flex+ sx={{+ flexDirection: "column",+ p: 2,+ width: 320,+ backgroundColor: "background"}}>- Grant permission- </Button>- </Flex>- </FlexScrollContainer>;+ <Text+ variant="subtitle"+ sx={{ mt: 2, mb: 1, color: "icon", fontSize: "body" }}+ >+ Permission required+ </Text>+ <Text+ variant="body"+ sx={{ mt: 2, mb: 1, color: "icon", fontSize: "body" }}+ >+ Permission is required to use the CORS proxy: {settings.corsProxy}+ </Text>+ <Button+ onClick={async () => {+ if (!settings.corsProxy) return;+ setHasPermission(+ await browser.permissions.request({+ origins: [`${settings.corsProxy}/*`]+ })+ );+ }}+ >+ Grant permission+ </Button>+ </Flex>+ </FlexScrollContainer>+ );}return (@@ -473,6 +475,17 @@ export async function clip(});if (!tab || !tab.id) return;+ if (browser.scripting) {+ await browser.scripting.executeScript({+ files: ["contentScript.bundle.js"],+ target: { tabId: tab.id }+ });+ } else {+ await browser.tabs.executeScript(tab.id, {+ file: "contentScript.bundle.js"+ });+ }+if (area === "visible" && mode === "screenshot") {if (!tab.height || !tab.width) return;diff --git a/extensions/web-clipper/src/views/settings.tsx b/extensions/web-clipper/src/views/settings.tsxindex 067bafc77..e9464822b 100644--- a/extensions/web-clipper/src/views/settings.tsx+++ b/extensions/web-clipper/src/views/settings.tsx@@ -45,7 +45,7 @@ export function Settings() {backgroundColor: "background"}}as="form"- onSubmit={(e) => {+ onSubmit={async (e) => {e.preventDefault();const form = new FormData(e.target as HTMLFormElement);@@ -56,6 +56,10 @@ export function Settings() {corsProxy = `${url.protocol}//${url.hostname}`;}+ await browser.permissions.request({+ origins: [`${corsProxy}/*`]+ });+saveSettings({corsProxy});