Skip to content

Commit ad0e0ac

Browse files
committed
fix: ask for the necessary promises beforehand; fix creating page screenshot; add more logs
Signed-off-by: Robert Goniszewski <robertgoniszewski@outlook.com>
1 parent 24309df commit ad0e0ac

File tree

3 files changed

+75
-11
lines changed

3 files changed

+75
-11
lines changed

package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "grimoire-companion",
33
"displayName": "grimoire companion",
4-
"version": "0.1.1",
4+
"version": "0.1.2",
55
"description": "Companion extension for Grimoire.",
66
"author": "Robert Goniszewski <robert@goniszewski.com>",
77
"scripts": {
@@ -33,8 +33,7 @@
3333
},
3434
"manifest": {
3535
"host_permissions": [
36-
"http://*/*",
37-
"https://*/*"
36+
"<all_urls>"
3837
],
3938
"permissions": [
4039
"activeTab",

src/popup.svelte

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
saveScreenshot: false
5252
};
5353
let validationInterval: NodeJS.Timeout;
54+
let missingPermissions = false;
5455
5556
$: $updatedUrl = $currentTab.url;
5657
@@ -85,6 +86,15 @@
8586
};
8687
}
8788
89+
async function handleGrimoireApiUrlChange() {
90+
storage.set('configuration', configuration);
91+
92+
logger.debug('handleGrimoireApiUrlChange', 'Configuration changed', configuration);
93+
showToast.success('Configuration saved! 🪶');
94+
95+
return onValidateGrimoireApiUrl();
96+
}
97+
8898
async function fetchUserData() {
8999
const categoriesAndTags = await sendToBackground<
90100
{
@@ -120,9 +130,37 @@
120130
return true;
121131
}
122132
133+
async function requestPermissions() {
134+
return chrome.permissions
135+
.request({
136+
origins: ['<all_urls>']
137+
})
138+
.then((granted) => {
139+
if (granted) {
140+
logger.debug('requestPermissions', 'Necessary permission granted');
141+
missingPermissions = false;
142+
} else {
143+
logger.error('requestPermissions', 'Necessary permission denied');
144+
}
145+
});
146+
}
147+
123148
onMount(async () => {
124149
logger.debug('popup.onMount', 'init');
125150
151+
missingPermissions = !(await chrome.permissions.contains({
152+
origins: ['<all_urls>']
153+
}));
154+
155+
if (missingPermissions) {
156+
$status = {
157+
...$status,
158+
isGrimoireApiReachable: false
159+
};
160+
161+
return;
162+
}
163+
126164
const theme = await storage.get('theme');
127165
token = await storage.get('token');
128166
configuration = (await storage.get('configuration')) || {
@@ -178,13 +216,13 @@
178216
179217
logger.debug('onMount', 'validationInterval', 'initiating');
180218
181-
validationInterval = setInterval(() => {
182-
onValidateGrimoireApiUrl();
183-
}, 5000);
219+
// validationInterval = setInterval(() => {
220+
// onValidateGrimoireApiUrl();
221+
// }, 5000);
184222
});
185223
186224
onDestroy(() => {
187-
clearInterval(validationInterval);
225+
// clearInterval(validationInterval);
188226
});
189227
190228
async function signIn() {
@@ -227,7 +265,21 @@
227265
</script>
228266

229267
<div class="drawer drawer-end min-h-max min-w-80 max-w-80">
230-
{#if !$status.isGrimoireApiReachable}
268+
{#if missingPermissions}
269+
<div class="container flex flex-col min-w-80 max-w-80 min-h-96 p-8">
270+
<h2 class="text-xl font-semibold text-center mt-1 mb-4">Permissions required</h2>
271+
Before we continue, please grant this extension the necessary permissions.
272+
<h3 class="text-lg font-semibold mt-1 mb-4">They will allow us to:</h3>
273+
<ul class="list-disc list-inside">
274+
<li>connect to your Grimoire instance</li>
275+
<li>take screenshots of webpages</li>
276+
</ul>
277+
278+
<button class="btn btn-secondary btn-sm w-full max-w-xs my-4" on:click={requestPermissions}>
279+
Request permissions
280+
</button>
281+
</div>
282+
{:else if !$status.isGrimoireApiReachable}
231283
<div class="container flex flex-col min-w-80 max-w-80 min-h-96 p-8">
232284
<h2 class="text-2xl font-semibold text-center mt-1 mb-4">Sign in</h2>
233285
<p class="text-accent text-center mb-4">
@@ -247,7 +299,7 @@
247299
bind:value={configuration.grimoireApiUrl}
248300
on:keyup={(e) => {
249301
if (e.key === 'Enter') {
250-
onValidateGrimoireApiUrl();
302+
handleGrimoireApiUrlChange();
251303
}
252304
}}
253305
/>
@@ -256,7 +308,7 @@
256308
{/if}
257309
<button
258310
class="btn btn-primary btn-sm w-full max-w-xs my-4"
259-
on:click={() => onValidateGrimoireApiUrl()}>Try to connect</button
311+
on:click={() => handleGrimoireApiUrlChange()}>Try to connect</button
260312
>
261313
</div>
262314
{:else if !token}

src/shared/handlers/on-add-bookmark.handler.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,19 @@ export async function onAddBookmark(
3131

3232
try {
3333
if (capturePageScreenshot) {
34+
logger.debug('onAddBookmark', 'Capturing page screenshot');
3435
await new Promise((resolve) => {
3536
chrome.tabs.captureVisibleTab(function (screenshotDataUrl) {
3637
const screenshotImage = new Image();
3738
screenshotImage.src = screenshotDataUrl;
3839
screenshotImage.onload = function () {
3940
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
4041

42+
logger.debug('onAddBookmark', 'Screenshot loaded', {
43+
width: screenshotImage.width,
44+
height: screenshotImage.height
45+
});
46+
4147
const canvas = document.createElement('canvas');
4248
const ctx = canvas.getContext('2d');
4349

@@ -47,13 +53,20 @@ export async function onAddBookmark(
4753
canvas.width = (screenshotImage.width / screenshotImage.height) * 800;
4854
ctx.drawImage(screenshotImage, 0, 0, canvas.width, canvas.height);
4955

56+
logger.debug('onAddBookmark', 'Screenshot resized', {
57+
width: canvas.width,
58+
height: canvas.height
59+
});
60+
5061
// Safari doesn't currently support converting to webp :(
5162
// https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toDataURL#browser_compatibility
5263
const resizedScreenshotDataUrl = canvas.toDataURL(
5364
isSafari ? 'image/jpeg' : 'image/webp',
5465
0.8
5566
);
5667

68+
logger.debug('onAddBookmark', 'Screenshot converted to data URL');
69+
5770
screenshot = resizedScreenshotDataUrl;
5871

5972
resolve(null);
@@ -115,7 +128,7 @@ export async function onAddBookmark(
115128

116129
updateOnAddBookmarkLoading(!response.bookmark);
117130
} catch (error) {
118-
logger.error('onAddBookmark', 'Error adding bookmark', error);
131+
logger.error('onAddBookmark', 'Error adding bookmark', error?.message);
119132

120133
updateOnAddBookmarkLoading(true);
121134
showToast.error("Couldn't add bookmark. Please try again.");

0 commit comments

Comments
 (0)