From c3248b78bf3ea654838f9a5dac73bd99a51a8f2a Mon Sep 17 00:00:00 2001 From: tzoug Date: Sat, 2 Sep 2023 23:22:30 -0400 Subject: [PATCH 1/5] Adjustments for url pasting support --- thingizip/manifest.json | 6 +- thingizip/src/components/body.svelte | 16 ++- thingizip/src/components/home.svelte | 78 ++++++++---- thingizip/src/components/info.svelte | 97 +++++++++------ thingizip/src/components/main.svelte | 8 +- thingizip/src/components/welcome.svelte | 159 ++++++++++++++---------- thingizip/src/utils/details.ts | 15 ++- thingizip/src/utils/helpers.ts | 18 ++- 8 files changed, 247 insertions(+), 150 deletions(-) diff --git a/thingizip/manifest.json b/thingizip/manifest.json index 739b9b5..df7c20a 100644 --- a/thingizip/manifest.json +++ b/thingizip/manifest.json @@ -1,13 +1,15 @@ { "name": "ThingiZIP", "description": "A browser extension to easily download files from Thingiverseā„¢", - "version": "3.2", + "version": "3.3", "manifest_version": 3, "action": { "default_popup": "src/popup/index.html" }, "permissions": [ - "storage", + "storage" + ], + "optional_permissions": [ "activeTab" ], "icons": { diff --git a/thingizip/src/components/body.svelte b/thingizip/src/components/body.svelte index d289099..ab7e1b2 100644 --- a/thingizip/src/components/body.svelte +++ b/thingizip/src/components/body.svelte @@ -5,12 +5,10 @@ import { valueStore } from '../utils/valueStore'; -
- {#if $valueStore == 'Home'} - - {:else if $valueStore == 'Recents'} - - {:else if $valueStore == 'Info'} - - {/if} -
+{#if $valueStore == 'Home'} + +{:else if $valueStore == 'Recents'} + +{:else if $valueStore == 'Info'} + +{/if} \ No newline at end of file diff --git a/thingizip/src/components/home.svelte b/thingizip/src/components/home.svelte index 1d51090..be7b3dc 100644 --- a/thingizip/src/components/home.svelte +++ b/thingizip/src/components/home.svelte @@ -6,11 +6,12 @@ import Badge from './badge.svelte'; import Welcome from './welcome.svelte'; import Download from './download.svelte'; + import { BASE_URL } from '../utils/constants'; let isFirstLaunch = undefined; + let manualInputVal = undefined; let name = undefined; - let fileCount = undefined; let creatorName = undefined; let creatorCover = undefined; let addedDate = undefined; @@ -22,46 +23,69 @@ let commentCount = undefined; let viewCount = undefined; - displayValues(); + getCurrentDetails(); - function displayValues() { - const promise = fetchDetails(); + function getCurrentDetails() { + let promise; + + if(manualInputVal != undefined && manualInputVal.startsWith(BASE_URL)){ + promise = fetchDetails(manualInputVal); + } + else{ + promise = fetchDetails(undefined); + } promise .then((details) => { - if (details != null && details != undefined) { - isFirstLaunch = false; - - name = details['name']; - fileCount = details['file_count']; - likeCount = details['like_count'] == undefined ? '-' : details['like_count']; - collectCount = details['collect_count'] == undefined ? '-' : details['collect_count']; - commentCount = details['comment_count'] == undefined ? '-' : details['comment_count']; - viewCount = details['view_count'] == undefined ? '-' : details['view_count']; - creatorName = details['creator']['name']; - creatorUrl = details['creator']['public_url']; - creatorCover = details['creator']['thumbnail']; - thumbnail = details['thumbnail']; - publicUrl = details['public_url']; - - let added = details['added']; - let dateOptions = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }; - addedDate = new Date(added).toLocaleDateString(); - } else { - isFirstLaunch = true; - } + displayDetails(details) }) .catch((error) => { console.error('Error:', error); }); } + + function displayDetails(details){ + if (details != null && details != undefined) { + isFirstLaunch = false; + + name = details['name']; + likeCount = details['like_count'] == undefined ? '-' : details['like_count']; + collectCount = details['collect_count'] == undefined ? '-' : details['collect_count']; + commentCount = details['comment_count'] == undefined ? '-' : details['comment_count']; + viewCount = details['view_count'] == undefined ? '-' : details['view_count']; + creatorName = details['creator']['name']; + creatorUrl = details['creator']['public_url']; + creatorCover = details['creator']['thumbnail']; + thumbnail = details['thumbnail']; + publicUrl = details['public_url']; + + let added = details['added']; + addedDate = new Date(added).toLocaleDateString(); + } else { + isFirstLaunch = true; + } + } {#if isFirstLaunch} {:else} -
-
+
+
+
+
+
+ + +
+
+
+
+
diff --git a/thingizip/src/components/info.svelte b/thingizip/src/components/info.svelte index 522f221..dd9a739 100644 --- a/thingizip/src/components/info.svelte +++ b/thingizip/src/components/info.svelte @@ -1,6 +1,63 @@ -
-
Quick Access
-
+ + +
Permissions
+
Automatically detect when on a Thingiverse page.
+ +{#if isAccessGranted} + +{:else} + +{/if} + +
Quick Access
- -
-
Ressources
-
-
diff --git a/thingizip/src/components/main.svelte b/thingizip/src/components/main.svelte index 2cf394b..ed162c5 100644 --- a/thingizip/src/components/main.svelte +++ b/thingizip/src/components/main.svelte @@ -4,8 +4,10 @@ import Navbar from './navbar.svelte'; -
+
- - +
+ + +
diff --git a/thingizip/src/components/welcome.svelte b/thingizip/src/components/welcome.svelte index a31a4e1..0ae3c9a 100644 --- a/thingizip/src/components/welcome.svelte +++ b/thingizip/src/components/welcome.svelte @@ -1,66 +1,97 @@ -
Welcome!
-

- To start downloading files with ThingiZip, you must visit a valid Thingiverse page.
- A valid page looks like:
- - https://www.thingiverse.com/thing:6145551 - -

+ + + +
+ +
+
Welcome!
+

There are two ways to download files from Thingiverse.

+
+ + +
+
Automatic
+

Whenever the extension is opened on a valid Thingiverse page, it will automatically retrieve the required information needed for the download.

+

Automatic mode requires access to detect when you're on a valid Thingiverse page.

+ +

A valid page looks like:

+ https://www.thingiverse.com/thing:6145551 + + +
+ + +
+
Manual
+

Paste the link of the Thingiverse page below.

+ +
+
+
+ + +
+
+
+
+ + + Visit Thingiverse +
\ No newline at end of file diff --git a/thingizip/src/utils/details.ts b/thingizip/src/utils/details.ts index 28c8563..520075a 100644 --- a/thingizip/src/utils/details.ts +++ b/thingizip/src/utils/details.ts @@ -9,12 +9,18 @@ import { } from './helpers'; import { DETAILS_KEY, BASE_URL, BASE_API_URL, RECENTS_KEY, MAX_RECENTS } from './constants'; -export async function fetchDetails() { +export async function fetchDetails(link?: string | undefined) { let currentDetails = await getFromStorage(DETAILS_KEY); - let activeUrl = await getActiveUrl(); + let url = undefined; - if (activeUrl.startsWith(BASE_URL)) { - let id = getIdFromUrl(activeUrl); + if(link != undefined){ + url = link; + }else{ + url = await getActiveUrl(); + } + + if (url != undefined && url.startsWith(BASE_URL)) { + let id = getIdFromUrl(url); let storageHasValue = doesStorageHaveValue(currentDetails); @@ -47,6 +53,7 @@ export async function fetchDetails() { return newDetails; } else { + // No need to re-fetch data if we're already on the last page that was visited if (currentDetails != undefined) { return currentDetails; diff --git a/thingizip/src/utils/helpers.ts b/thingizip/src/utils/helpers.ts index e830409..c051377 100644 --- a/thingizip/src/utils/helpers.ts +++ b/thingizip/src/utils/helpers.ts @@ -46,11 +46,21 @@ export function setToStorage(key: string, value: object | string) { export function getActiveUrl(): Promise { return new Promise((resolve) => { - chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { - if (tabs.length > 0) { - let activeTab = tabs[0]; - resolve(activeTab.url); + chrome.permissions.contains({ + permissions: ['activeTab'], + }, (result) => { + if (result) { + console.log("has perm"); + chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => { + if (tabs.length > 0) { + let activeTab = tabs[0]; + resolve(activeTab.url); + } else { + resolve(undefined); + } + }); } else { + console.log("no perm"); resolve(undefined); } }); From 23ee95f467e680c297ba2cbdf4c871332f97e739 Mon Sep 17 00:00:00 2001 From: tzoug Date: Sun, 3 Sep 2023 00:45:03 -0400 Subject: [PATCH 2/5] Finalized permission granting --- thingizip/src/components/info.svelte | 35 ++++++----- thingizip/src/components/welcome.svelte | 82 +++++++++++++++++-------- 2 files changed, 76 insertions(+), 41 deletions(-) diff --git a/thingizip/src/components/info.svelte b/thingizip/src/components/info.svelte index dd9a739..8965759 100644 --- a/thingizip/src/components/info.svelte +++ b/thingizip/src/components/info.svelte @@ -1,23 +1,30 @@ diff --git a/thingizip/src/components/welcome.svelte b/thingizip/src/components/welcome.svelte index 0ae3c9a..9202989 100644 --- a/thingizip/src/components/welcome.svelte +++ b/thingizip/src/components/welcome.svelte @@ -3,28 +3,58 @@ import { BASE_URL } from '../utils/constants'; let manualInputVal; - let isAccessGranted; - - chrome.permissions.contains({ - permissions: ['activeTab'], - }, (result) => { - if (result) { - isAccessGranted = true; - } else { - isAccessGranted = false; - } - }); + let isAccessGranted: boolean; + + getPermission(); + + function getPermission(){ + getActiveTabPermission().then((result) => { + isAccessGranted = result; + }) + } - function requestPermission(){ - chrome.permissions.request({ + function getActiveTabPermission(): Promise{ + return new Promise((resolve) =>{ + chrome.permissions.contains({ permissions: ['activeTab'] - }, (granted) => { - if (granted) { - isAccessGranted = true; - } else { - isAccessGranted = false; - } - }); + }, (result) => { + if (result) { + resolve(true); + } else { + resolve(false); + } + }); + }); + } + + function toggleActiveTabPermission(){ + getPermission(); + + if(isAccessGranted){ + chrome.permissions.remove({ + permissions: ['activeTab'] + }, (removed) => { + if (removed) { + // The permissions have been removed. + } else { + // The permissions have not been removed (e.g., you tried to remove + // required permissions). + } + }); + } + else{ + chrome.permissions.request({ + permissions: ['activeTab'] + }, (granted) => { + if (granted) { + // Granted + } else { + // Not granted + } + }); + } + + getPermission(); } function search(){ @@ -64,13 +94,11 @@

A valid page looks like:

https://www.thingiverse.com/thing:6145551 - + {#if isAccessGranted} + + {:else} + + {/if}
From 2195133c221aad053a13b0cd61584ff93cc54c77 Mon Sep 17 00:00:00 2001 From: tzoug Date: Sun, 3 Sep 2023 00:58:36 -0400 Subject: [PATCH 3/5] Fixed indentation --- thingizip/src/components/info.svelte | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/thingizip/src/components/info.svelte b/thingizip/src/components/info.svelte index 8965759..e20c80e 100644 --- a/thingizip/src/components/info.svelte +++ b/thingizip/src/components/info.svelte @@ -12,7 +12,7 @@ function getActiveTabPermission(): Promise{ return new Promise((resolve) =>{ chrome.permissions.contains({ - permissions: ['activeTab'] + permissions: ['activeTab'] }, (result) => { if (result) { resolve(true); @@ -40,7 +40,7 @@ } else{ chrome.permissions.request({ - permissions: ['activeTab'] + permissions: ['activeTab'] }, (granted) => { if (granted) { // Granted From 2bdb315a88ae4ec825c65a14d9d488c67e82d069 Mon Sep 17 00:00:00 2001 From: tzoug Date: Sun, 3 Sep 2023 23:54:20 -0400 Subject: [PATCH 4/5] Reduced duplication --- thingizip/src/components/home.svelte | 4 +-- thingizip/src/components/info.svelte | 37 +++------------------ thingizip/src/components/welcome.svelte | 40 +++-------------------- thingizip/src/utils/helpers.ts | 43 +++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 70 deletions(-) diff --git a/thingizip/src/components/home.svelte b/thingizip/src/components/home.svelte index be7b3dc..03dde04 100644 --- a/thingizip/src/components/home.svelte +++ b/thingizip/src/components/home.svelte @@ -75,8 +75,8 @@
- -