Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat(chrome-ext): request that users review the extension (#2359)

Elijah Potter52fcd2dab79c
diff --git a/packages/chrome-plugin/src/ProtocolClient.ts b/packages/chrome-plugin/src/ProtocolClient.ts
index d038d4b0e..f97ed57b9 100644
--- a/packages/chrome-plugin/src/ProtocolClient.ts
+++ b/packages/chrome-plugin/src/ProtocolClient.ts
@@ -108,6 +108,18 @@ export default class ProtocolClient {
return (await chrome.runtime.sendMessage({ kind: 'getUserDictionary' })).words;
}
+ public static async getInstalledOn(): Promise<string | null> {
+ return (await chrome.runtime.sendMessage({ kind: 'getInstalledOn' })).installedOn;
+ }
+
+ public static async getReviewed(): Promise<boolean> {
+ return (await chrome.runtime.sendMessage({ kind: 'getReviewed' })).reviewed;
+ }
+
+ public static async setReviewed(reviewed: boolean): Promise<void> {
+ await chrome.runtime.sendMessage({ kind: 'setReviewed', reviewed });
+ }
+
public static async ignoreHash(hash: string): Promise<void> {
await chrome.runtime.sendMessage({ kind: 'ignoreLint', contextHash: hash });
this.lintCache.clear();
diff --git a/packages/chrome-plugin/src/background/index.ts b/packages/chrome-plugin/src/background/index.ts
index 6cf18fecf..8e8f76a2b 100644
--- a/packages/chrome-plugin/src/background/index.ts
+++ b/packages/chrome-plugin/src/background/index.ts
@@ -14,8 +14,12 @@ import {
type GetDomainStatusRequest,
type GetDomainStatusResponse,
type GetEnabledDomainsResponse,
+ type GetInstalledOnRequest,
+ type GetInstalledOnResponse,
type GetLintDescriptionsRequest,
type GetLintDescriptionsResponse,
+ type GetReviewedRequest,
+ type GetReviewedResponse,
type GetUserDictionaryResponse,
type IgnoreLintRequest,
type LintRequest,
@@ -30,6 +34,7 @@ import {
type SetDefaultStatusRequest,
type SetDialectRequest,
type SetDomainStatusRequest,
+ type SetReviewedRequest,
type SetUserDictionaryRequest,
type UnitResponse,
} from '../protocol';
@@ -52,6 +57,7 @@ chrome.runtime.onMessage.addListener((request, _sender, sendResponse) => {
let linter: LocalLinter;
getDialect().then(setDialect);
+setInstalledOnIfMissing();
async function enableDefaultDomains() {
const defaultEnabledDomains = [
@@ -154,6 +160,12 @@ function handleRequest(message: Request): Promise<Response> {
return Promise.resolve(createUnitResponse());
case 'postFormData':
return handlePostFormData(message);
+ case 'getInstalledOn':
+ return handleGetInstalledOn(message);
+ case 'getReviewed':
+ return handleGetReviewed(message);
+ case 'setReviewed':
+ return handleSetReviewed(message);
}
}
@@ -321,6 +333,19 @@ async function handlePostFormData(req: PostFormDataRequest): Promise<PostFormDat
}
}
+async function handleGetInstalledOn(_req: GetInstalledOnRequest): Promise<GetInstalledOnResponse> {
+ return { kind: 'getInstalledOn', installedOn: await getInstalledOn() };
+}
+
+async function handleGetReviewed(_req: GetReviewedRequest): Promise<GetReviewedResponse> {
+ return { kind: 'getReviewed', reviewed: await getReviewed() };
+}
+
+async function handleSetReviewed(req: SetReviewedRequest): Promise<UnitResponse> {
+ await setReviewed(req.reviewed);
+ return createUnitResponse();
+}
+
/** Set the lint configuration inside the global `linter` and in permanent storage. */
async function setLintConfig(lintConfig: LintConfig): Promise<void> {
await linter.setLintConfig(lintConfig);
@@ -453,3 +478,28 @@ async function getUserDictionary(): Promise<string[]> {
const resp = await chrome.storage.local.get({ userDictionary: [] });
return resp.userDictionary;
}
+
+/** Record the date the extension was installed, if it's missing. */
+async function setInstalledOnIfMissing(): Promise<void> {
+ const current = await getInstalledOn();
+ if (current !== null) {
+ return;
+ }
+
+ const installedOn = new Date().toISOString();
+ await chrome.storage.local.set({ installedOn });
+}
+
+async function getInstalledOn(): Promise<string | null> {
+ const resp = await chrome.storage.local.get({ installedOn: null });
+ return resp.installedOn;
+}
+
+async function getReviewed(): Promise<boolean> {
+ const resp = await chrome.storage.local.get({ reviewed: false });
+ return Boolean(resp.reviewed);
+}
+
+async function setReviewed(reviewed: boolean): Promise<void> {
+ await chrome.storage.local.set({ reviewed });
+}
diff --git a/packages/chrome-plugin/src/generateGreeting.ts b/packages/chrome-plugin/src/generateGreeting.ts
new file mode 100644
index 000000000..7148fecf9
--- /dev/null
+++ b/packages/chrome-plugin/src/generateGreeting.ts
@@ -0,0 +1,26 @@
+export default function generateGreeting(): string {
+ const timeOfDay = getTimeOfDay();
+
+ switch (timeOfDay) {
+ case TimeOfDay.Morning:
+ return 'Good morning!';
+ case TimeOfDay.Afternoon:
+ return 'Good afternoon!';
+ case TimeOfDay.Evening:
+ return 'Good evening!';
+ }
+}
+
+enum TimeOfDay {
+ Morning = 0,
+ Afternoon = 1,
+ Evening = 2,
+}
+
+function getTimeOfDay(date: Date = new Date()): TimeOfDay {
+ const hour = date.getHours();
+
+ if (hour >= 5 && hour < 12) return TimeOfDay.Morning;
+ if (hour >= 12 && hour < 18) return TimeOfDay.Afternoon;
+ return TimeOfDay.Evening;
+}
diff --git a/packages/chrome-plugin/src/popup/Main.svelte b/packages/chrome-plugin/src/popup/Main.svelte
index b85cbe6f0..add8ab8cb 100644
--- a/packages/chrome-plugin/src/popup/Main.svelte
+++ b/packages/chrome-plugin/src/popup/Main.svelte
@@ -1,10 +1,32 @@
<script lang="ts">
-import { Button, Select, Toggle } from 'components';
+import { Button } from 'components';
+import generateGreeting from '../generateGreeting';
import ProtocolClient from '../ProtocolClient';
let enabled = $state(true);
let domain = $state('');
+let installDate: Date | null = $state(null);
+let hasBeenReviewed: boolean | null = $state(null);
+const REVIEW_URL =
+ 'https://chromewebstore.google.com/detail/private-grammar-checker-h/lodbfhdipoipcjmlebjbgmmgekckhpfb/reviews';
+
+const isFirefox = isFirefoxExtension();
+
+if (!isFirefox) {
+ ProtocolClient.getInstalledOn().then((d) => {
+ if (d == null) {
+ return;
+ }
+
+ installDate = new Date(d);
+ });
+
+ ProtocolClient.getReviewed().then((r) => {
+ hasBeenReviewed = r;
+ });
+}
+
getCurrentTabDomain().then((d) => {
domain = d ?? '';
});
@@ -38,36 +60,75 @@ function toggleDomainEnabled() {
enabled = !enabled;
ProtocolClient.setDomainEnabled(domain, enabled);
}
+
+function openReviewPage() {
+ ProtocolClient.setReviewed(true);
+ chrome.tabs.create({ url: REVIEW_URL });
+}
+
+function isFirefoxExtension(): boolean {
+ try {
+ return new URL(chrome.runtime.getURL('')).protocol === 'moz-extension:';
+ } catch {
+ return false;
+ }
+}
+
+/** Get the number of days since a given Date. */
+function daysSince(date: Date): number {
+ let now = Date.now();
+ let then = date.getTime();
+
+ let msDiff = now - then;
+ return msDiff / 86400000;
+}
</script>
-<main class="p-6 space-y-5 text-gray-800">
- <!-- power button section -->
- <section class="flex flex-row items-center gap-3 py-6">
- <Button
- size="lg"
- class="rounded-full! aspect-square h-16 w-16 p-0 shadow-lg transition-colors flex! flex-row justify-center"
- color={enabled ? 'var(--color-primary)' : 'var(--color-cream-50)'}
- on:click={toggleDomainEnabled}
- >
- <svg
- xmlns="http://www.w3.org/2000/svg"
- class="h-9 w-9"
- fill="none"
- viewBox="0 0 24 24"
- stroke="currentColor"
- stroke-width="2"
+<main>
+ <section class="p-6 space-y-5 text-gray-800 flex flex-row">
+ <Button
+ size="lg"
+ class="rounded-full! aspect-square h-24 w-24 p-0 shadow-lg transition-colors flex! flex-row justify-center"
+ color={enabled ? 'var(--color-primary)' : 'var(--color-cream-50)'}
+ on:click={toggleDomainEnabled}
>
- <path
- color={enabled ? 'var(--color-cream-50)' : 'var(--color-primary)'}
- stroke-linecap="round"
- stroke-linejoin="round"
- d="M12 5v7m5.657-4.657a8 8 0 11-11.314 0"
- />
- </svg>
- </Button>
-
- <p class="text-sm font-medium dark:text-white">
- {enabled ? 'Enabled on ' : 'Disabled on '}{domain}
- </p>
+ <svg
+ xmlns="http://www.w3.org/2000/svg"
+ class="h-9 w-9"
+ fill="none"
+ viewBox="0 0 24 24"
+ stroke="currentColor"
+ stroke-width="2"
+ >
+ <path
+ color={enabled ? 'var(--color-cream-50)' : 'var(--color-primary)'}
+ stroke-linecap="round"
+ stroke-linejoin="round"
+ d="M12 5v7m5.657-4.657a8 8 0 11-11.314 0"
+ />
+ </svg>
+ </Button>
+
+ <section class="items-end p-2">
+ <h1 class="text-2xl dark:text-white text-right">
+ {generateGreeting()}
+ </h1>
+
+ <p class="text-sm font-medium font-sans dark:text-white text-right">
+ Harper is {enabled ? 'enabled on ' : 'disabled on '}{domain}
+ </p>
+ </section>
</section>
+
+ {#if !isFirefox && installDate != null && daysSince(installDate) > 7 && hasBeenReviewed === false}
+ <section class="bg-primary flex flex-row justify-between p-4">
+ <div class="font-bold">
+ It looks like you're enjoying Harper.<br>
+ Would you mind giving us a review?
+ </div>
+ <Button on:click={openReviewPage}>
+ Review
+ </Button>
+ </section>
+ {/if}
</main>
diff --git a/packages/chrome-plugin/src/protocol.ts b/packages/chrome-plugin/src/protocol.ts
index e0be9a77b..56f732ce4 100644
--- a/packages/chrome-plugin/src/protocol.ts
+++ b/packages/chrome-plugin/src/protocol.ts
@@ -20,6 +20,9 @@ export type Request =
| GetActivationKeyRequest
| SetActivationKeyRequest
| OpenOptionsRequest
+ | GetInstalledOnRequest
+ | GetReviewedRequest
+ | SetReviewedRequest
| OpenReportErrorRequest
| PostFormDataRequest;
@@ -34,6 +37,8 @@ export type Response =
| GetEnabledDomainsResponse
| GetUserDictionaryResponse
| GetActivationKeyResponse
+ | GetInstalledOnResponse
+ | GetReviewedResponse
| PostFormDataResponse;
export type LintRequest = {
@@ -146,6 +151,29 @@ export type GetUserDictionaryResponse = {
words: string[];
};
+export type GetInstalledOnRequest = {
+ kind: 'getInstalledOn';
+};
+
+export type GetInstalledOnResponse = {
+ kind: 'getInstalledOn';
+ installedOn: string | null;
+};
+
+export type GetReviewedRequest = {
+ kind: 'getReviewed';
+};
+
+export type GetReviewedResponse = {
+ kind: 'getReviewed';
+ reviewed: boolean;
+};
+
+export type SetReviewedRequest = {
+ kind: 'setReviewed';
+ reviewed: boolean;
+};
+
export type IgnoreLintRequest = {
kind: 'ignoreLint';
contextHash: string;
diff --git a/packages/chrome-plugin/tests/review_banner.spec.ts b/packages/chrome-plugin/tests/review_banner.spec.ts
new file mode 100644
index 000000000..218d5d153
--- /dev/null
+++ b/packages/chrome-plugin/tests/review_banner.spec.ts
@@ -0,0 +1,35 @@
+import { expect, test } from './fixtures';
+
+test.describe('review banner', () => {
+ test.skip(({ browserName }) => browserName === 'firefox', 'Review prompt disabled in Firefox');
+
+ test('review request hidden before 7 days', async ({ context, page }) => {
+ const background = context.serviceWorkers()[0] ?? (await context.waitForEvent('serviceworker'));
+ const extensionId = background.url().split('/')[2];
+
+ const popupUrl = `chrome-extension://${extensionId}/popup.html`;
+ await page.goto(popupUrl);
+
+ await page.getByText("Let's start writing").click();
+
+ await expect(page.getByText('Harper is')).toBeVisible();
+ await expect(page.getByText('Would you mind giving us a review?')).toHaveCount(0);
+ });
+
+ test('review request shown after 7 days', async ({ context, page }) => {
+ const background = context.serviceWorkers()[0] ?? (await context.waitForEvent('serviceworker'));
+ const extensionId = background.url().split('/')[2];
+
+ const popupUrl = `chrome-extension://${extensionId}/popup.html`;
+ await page.goto(popupUrl);
+
+ // 8 days
+ await page.clock.install();
+ await page.clock.fastForward(8 * 1000 * 60 * 60 * 24);
+
+ await page.getByText("Let's start writing").click();
+
+ await expect(page.getByText('Harper is')).toBeVisible();
+ await expect(page.getByText('Would you mind giving us a review?')).toHaveCount(1);
+ });
+});