mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-28 19:06:19 -05:00
Creating browser extension to facilitate starting downloads. Chrome/Brave and Firefox supported.
The extension is hosted in a sibling repo and
is distributed off-store. Each WROLPi release ships the prebuilt artifacts
under wrolpi/static/extensions/ and serves them at /api/extensions/<file>:
- wrolpi-chrome.zip Load-Unpacked sideload for Chrome / Brave / Edge
- wrolpi-firefox.xpi Mozilla-signed (unlisted self-distribution),
one-click install in any Firefox
- versions.json Per-browser version metadata for the install page
Backend (modules/archive/api.py, wrolpi/root_api.py):
- POST /api/archive/upload accepts an optional repeated `tagNames`
multipart form field; the upload switch handler applies tags to the
new FileGroup via FileGroup.set_tags(). Unknown tag names are auto-
created inside a per-name SAVEPOINT so concurrent inserts on the same
novel name don't abort the transaction (Tag.name has unique=True).
- GET /api/extensions returns metadata (per-file availability + size +
versions.json contents) for the install page.
- GET /api/extensions/<filename> serves the binary, whitelisted by
filename. Firefox .xpi is served as application/x-xpinstall WITHOUT a
Content-Disposition: attachment header, so Firefox triggers its
add-on install handler instead of saving the file.
Frontend (app/src/):
- New /admin/extension page (linked from AdminRoute) detects the user's
browser via navigator.userAgent and renders an install card per
Firefox / Chromium / Other. Pulls metadata from /api/extensions to
show file size and version, and gracefully reports 'Not yet
available' when binaries are missing.
- Dashboard's Getters component supports a deep link from the
extension's 'Configure Download' button:
/?downloader=<archive|video|...>&download_url=<u1>&download_url=<u2>
On mount, the matching downloader form auto-opens with URLs pre-
filled. Query params are cleared on close so a refresh doesn't
re-open the modal.
- DownloadMenu and the per-downloader forms accept an `initialUrls`
seed via the existing `download` prop pattern, joined with newlines
for the `urls` textarea (and first URL for single-URL forms).
Behavior is fully backwards compatible: pre-existing API callers can
still POST to /api/archive/upload without `tagNames`, and the
DashboardPage modal continues to open via the existing button click
when no deep-link query params are present.
This commit is contained in:
parent
ae460af8d6
commit
a9a460c54c
21 changed files with 865 additions and 21 deletions
|
|
@ -5,6 +5,7 @@ import {DownloadsPage} from "./Downloads";
|
|||
import {SettingsPage} from "./Settings";
|
||||
import {StatusPage} from "./Status";
|
||||
import {ControllerPage} from "./ControllerPage";
|
||||
import {ExtensionPage} from "./ExtensionPage";
|
||||
|
||||
export default function AdminRoute() {
|
||||
|
||||
|
|
@ -13,6 +14,7 @@ export default function AdminRoute() {
|
|||
{text: 'Settings', to: '/admin/settings', key: 'settings'},
|
||||
{text: 'Status', to: '/admin/status', key: 'status'},
|
||||
{text: 'Control', to: '/admin/controller', key: 'controller'},
|
||||
{text: 'Extension', to: '/admin/extension', key: 'extension'},
|
||||
];
|
||||
|
||||
return <PageContainer>
|
||||
|
|
@ -22,6 +24,7 @@ export default function AdminRoute() {
|
|||
<Route path='settings' exact element={<SettingsPage/>}/>
|
||||
<Route path='status' exact element={<StatusPage/>}/>
|
||||
<Route path='controller' exact element={<ControllerPage/>}/>
|
||||
<Route path='extension' exact element={<ExtensionPage/>}/>
|
||||
</Routes>
|
||||
</PageContainer>
|
||||
}
|
||||
|
|
|
|||
38
app/src/components/admin/ExtensionInstallSuggestion.js
Normal file
38
app/src/components/admin/ExtensionInstallSuggestion.js
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
import React from "react";
|
||||
import {Link} from "react-router";
|
||||
import {Button, Icon, Message} from "semantic-ui-react";
|
||||
import {useLocalStorage} from "../Common";
|
||||
import {useExtensionInstalled} from "../../hooks/customHooks";
|
||||
|
||||
// Top-of-dashboard nudge for users who don't yet have the WROLPi browser
|
||||
// extension installed (or installed but haven't added this WROLPi as a
|
||||
// destination). Hidden once the user installs+configures, or dismisses it.
|
||||
//
|
||||
// Detection is via a meta tag injected by the extension's content script.
|
||||
// The extension only injects on configured destination origins, so seeing
|
||||
// the marker here means "extension is installed AND knows about this WROLPi".
|
||||
export function ExtensionInstallSuggestion() {
|
||||
const installed = useExtensionInstalled();
|
||||
const [dismissed, setDismissed] = useLocalStorage(
|
||||
'wrolpi-extension-banner-dismissed', false,
|
||||
);
|
||||
|
||||
// installed === null means "still checking" — render nothing rather
|
||||
// than flashing the banner before the content-script race resolves.
|
||||
if (installed !== false || dismissed) return null;
|
||||
|
||||
return <Message info icon onDismiss={() => setDismissed(true)}>
|
||||
<Icon name='puzzle piece'/>
|
||||
<Message.Content>
|
||||
<Message.Header>Install the WROLPi browser extension</Message.Header>
|
||||
<p>
|
||||
Send pages, videos, and feeds to this WROLPi straight from any
|
||||
tab. Open-source and not distributed via the official browser
|
||||
stores.
|
||||
</p>
|
||||
<Button as={Link} to='/admin/extension' primary size='small'>
|
||||
Open install page
|
||||
</Button>
|
||||
</Message.Content>
|
||||
</Message>;
|
||||
}
|
||||
48
app/src/components/admin/ExtensionInstallSuggestion.test.js
Normal file
48
app/src/components/admin/ExtensionInstallSuggestion.test.js
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
import React from 'react';
|
||||
import {render, screen, waitFor} from '../../test-utils';
|
||||
import {ExtensionInstallSuggestion} from './ExtensionInstallSuggestion';
|
||||
|
||||
const setMeta = (version) => {
|
||||
const existing = document.querySelector('meta[name="wrolpi-extension"]');
|
||||
if (existing) existing.remove();
|
||||
if (version === undefined) return;
|
||||
const meta = document.createElement('meta');
|
||||
meta.name = 'wrolpi-extension';
|
||||
meta.content = String(version);
|
||||
document.head.appendChild(meta);
|
||||
};
|
||||
|
||||
describe('ExtensionInstallSuggestion', () => {
|
||||
beforeEach(() => {
|
||||
setMeta(undefined);
|
||||
window.localStorage.removeItem('wrolpi-extension-banner-dismissed');
|
||||
});
|
||||
|
||||
it('renders nothing when the extension marker meta is present', async () => {
|
||||
setMeta('0.2.2');
|
||||
render(<ExtensionInstallSuggestion/>);
|
||||
// Give the hook a moment in case its initial detect() races; should
|
||||
// still find the marker on first render.
|
||||
await waitFor(() => {
|
||||
expect(screen.queryByText(/Install the WROLPi browser extension/i)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
it('renders the install message when the marker is absent', async () => {
|
||||
render(<ExtensionInstallSuggestion/>);
|
||||
// The hook re-checks once at 500ms before deciding "not installed".
|
||||
// Wait the recheck out, then assert.
|
||||
await waitFor(
|
||||
() => expect(screen.getByText(/Install the WROLPi browser extension/i)).toBeInTheDocument(),
|
||||
{timeout: 1500},
|
||||
);
|
||||
expect(screen.getByText(/Open install page/i)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('renders nothing when the user has dismissed the banner', async () => {
|
||||
window.localStorage.setItem('wrolpi-extension-banner-dismissed', 'true');
|
||||
render(<ExtensionInstallSuggestion/>);
|
||||
await new Promise(r => setTimeout(r, 600));
|
||||
expect(screen.queryByText(/Install the WROLPi browser extension/i)).toBeNull();
|
||||
});
|
||||
});
|
||||
202
app/src/components/admin/ExtensionPage.js
Normal file
202
app/src/components/admin/ExtensionPage.js
Normal file
|
|
@ -0,0 +1,202 @@
|
|||
import React from "react";
|
||||
import {Button, Header, Icon, Segment} from "../Theme";
|
||||
import {Input, Message} from "semantic-ui-react";
|
||||
|
||||
// The WROLPi browser extension is distributed off-store: Chrome users
|
||||
// sideload the unpacked .zip; Firefox users get a Mozilla-signed .xpi that
|
||||
// installs in one click. Both binaries are served by the local WROLPi
|
||||
// backend so this page works on a fully air-gapped install.
|
||||
|
||||
const CHROME_ZIP_PATH = '/api/extensions/wrolpi-chrome.zip';
|
||||
const FIREFOX_XPI_PATH = '/api/extensions/wrolpi-firefox.xpi';
|
||||
|
||||
function detectBrowser() {
|
||||
if (typeof navigator === 'undefined') return 'unknown';
|
||||
const ua = navigator.userAgent || '';
|
||||
// Order matters: Edge / Brave / Opera all include 'Chrome' in their UA.
|
||||
if (ua.includes('Firefox')) return 'firefox';
|
||||
if (ua.includes('Edg/')) return 'chromium';
|
||||
if (ua.includes('OPR/') || ua.includes('Opera')) return 'chromium';
|
||||
if (ua.includes('Chrome')) return 'chromium';
|
||||
return 'unknown';
|
||||
}
|
||||
|
||||
function formatSize(bytes) {
|
||||
if (!bytes) return '';
|
||||
const kb = bytes / 1024;
|
||||
if (kb < 1024) return `${kb.toFixed(0)} KB`;
|
||||
return `${(kb / 1024).toFixed(2)} MB`;
|
||||
}
|
||||
|
||||
function DestinationHint() {
|
||||
// The user is currently viewing this WROLPi at exactly window.location.origin.
|
||||
// That's the URL they need to paste into the extension's destination field.
|
||||
const origin = (typeof window !== 'undefined' && window.location && window.location.origin) || '';
|
||||
const [copied, setCopied] = React.useState(false);
|
||||
const onCopy = async () => {
|
||||
try {
|
||||
await navigator.clipboard.writeText(origin);
|
||||
setCopied(true);
|
||||
setTimeout(() => setCopied(false), 1500);
|
||||
} catch {
|
||||
// Older / non-secure contexts may block the clipboard API.
|
||||
// Fall through silently — the user can still copy manually.
|
||||
}
|
||||
};
|
||||
return <Segment style={{marginBottom: '1em'}}>
|
||||
<Header as='h4'>
|
||||
<Icon name='globe'/>
|
||||
This WROLPi's URL
|
||||
</Header>
|
||||
<p style={{color: 'grey'}}>
|
||||
After installing the extension, paste this URL into the
|
||||
destination field on the extension's options page.
|
||||
</p>
|
||||
<Input
|
||||
fluid
|
||||
readOnly
|
||||
value={origin}
|
||||
action={
|
||||
<Button
|
||||
icon={copied ? 'check' : 'copy'}
|
||||
color={copied ? 'green' : null}
|
||||
content={copied ? 'Copied' : 'Copy'}
|
||||
onClick={onCopy}
|
||||
/>
|
||||
}
|
||||
/>
|
||||
</Segment>;
|
||||
}
|
||||
|
||||
function FirefoxCard({metadata, version}) {
|
||||
const file = (metadata && metadata.files && metadata.files['wrolpi-firefox.xpi']) || {};
|
||||
const available = !!file.available;
|
||||
return <Segment>
|
||||
<Header as='h3'>
|
||||
<Icon name='firefox'/>
|
||||
Firefox{version ? ` — v${version}` : ''}
|
||||
</Header>
|
||||
<p>One-click install. Mozilla has signed this build.</p>
|
||||
<Button
|
||||
primary
|
||||
as='a'
|
||||
href={FIREFOX_XPI_PATH}
|
||||
disabled={!available}
|
||||
icon='download'
|
||||
content={available ? `Install for Firefox (${formatSize(file.size_bytes)})` : 'Not yet available'}
|
||||
/>
|
||||
<p style={{marginTop: '1em', color: 'grey'}}>
|
||||
Firefox will ask you to confirm the install. Accept the prompt.
|
||||
</p>
|
||||
<Header as='h4' style={{marginTop: '1.5em'}}>After installing</Header>
|
||||
<ol style={{lineHeight: 1.7, paddingLeft: '1.2em'}}>
|
||||
<li>Open the extension's <strong>Options</strong> page (right-click the toolbar icon → Manage Extension → Options) and add this WROLPi as a destination. Click <strong>Test</strong> to grant API access.</li>
|
||||
<li>Click the extension icon in the toolbar → the permission dropdown → <strong>Always Allow on this site</strong>. Firefox requires this second click in addition to the Test grant — without it, the extension can talk to your WROLPi but can't mark its tabs as connected.</li>
|
||||
</ol>
|
||||
</Segment>;
|
||||
}
|
||||
|
||||
function ChromiumCard({metadata, version}) {
|
||||
const file = (metadata && metadata.files && metadata.files['wrolpi-chrome.zip']) || {};
|
||||
const available = !!file.available;
|
||||
return <Segment>
|
||||
<Header as='h3'>
|
||||
<Icon name='chrome'/>
|
||||
Chrome / Brave / Edge{version ? ` — v${version}` : ''}
|
||||
</Header>
|
||||
<p>
|
||||
Chromium-based browsers don't allow one-click extension installs from
|
||||
outside the Web Store, so we sideload via the built-in developer flow.
|
||||
</p>
|
||||
<Button
|
||||
primary
|
||||
as='a'
|
||||
href={CHROME_ZIP_PATH}
|
||||
disabled={!available}
|
||||
icon='download'
|
||||
content={available ? `Download .zip (${formatSize(file.size_bytes)})` : 'Not yet available'}
|
||||
download
|
||||
/>
|
||||
<Header as='h4' style={{marginTop: '1.5em'}}>Install steps</Header>
|
||||
<ol style={{lineHeight: 1.7, paddingLeft: '1.2em'}}>
|
||||
<li>Download the <code>.zip</code> above and extract it to a folder you'll keep around.</li>
|
||||
<li>Open <code>chrome://extensions</code> (or <code>brave://extensions</code> / <code>edge://extensions</code>).</li>
|
||||
<li>Toggle <strong>Developer mode</strong> on (top-right).</li>
|
||||
<li>Click <strong>Load unpacked</strong> and pick the extracted folder.</li>
|
||||
</ol>
|
||||
<p style={{color: 'grey'}}>
|
||||
The extension stays installed across browser restarts. Don't delete the
|
||||
extracted folder — the browser loads from it directly.
|
||||
</p>
|
||||
</Segment>;
|
||||
}
|
||||
|
||||
function UnknownCard() {
|
||||
return <Segment>
|
||||
<Header as='h3'>Unsupported browser</Header>
|
||||
<p>
|
||||
The WROLPi extension supports Chrome, Brave, Edge, and Firefox. Open
|
||||
this page in one of those browsers to install.
|
||||
</p>
|
||||
</Segment>;
|
||||
}
|
||||
|
||||
export function ExtensionPage() {
|
||||
const [metadata, setMetadata] = React.useState(null);
|
||||
const [error, setError] = React.useState(null);
|
||||
const browser = detectBrowser();
|
||||
|
||||
React.useEffect(() => {
|
||||
let cancelled = false;
|
||||
fetch('/api/extensions')
|
||||
.then(res => res.json())
|
||||
.then(body => { if (!cancelled) setMetadata(body); })
|
||||
.catch(err => { if (!cancelled) setError(String(err)); });
|
||||
return () => { cancelled = true; };
|
||||
}, []);
|
||||
|
||||
if (error) {
|
||||
return <Segment>
|
||||
<Message negative>
|
||||
<Message.Header>Couldn't load extension info</Message.Header>
|
||||
<p>{error}</p>
|
||||
</Message>
|
||||
</Segment>;
|
||||
}
|
||||
|
||||
const versions = (metadata && metadata.versions) || {};
|
||||
const anyAvailable = metadata && Object.values(metadata.files || {}).some(f => f.available);
|
||||
|
||||
return <>
|
||||
<Header as='h1'>Browser Extension</Header>
|
||||
<p>
|
||||
Install the WROLPi browser extension to send pages, videos, and feeds to
|
||||
this WROLPi from any tab. The extension is open-source and not distributed
|
||||
via official browser stores — it ships with each WROLPi release.
|
||||
</p>
|
||||
|
||||
<DestinationHint/>
|
||||
|
||||
{metadata && !anyAvailable && <Message warning>
|
||||
<Message.Header>Extension binaries not yet installed</Message.Header>
|
||||
<p>
|
||||
The extension binaries aren't on this WROLPi yet. They ship with the
|
||||
next release; pull the latest and they'll appear here.
|
||||
</p>
|
||||
</Message>}
|
||||
|
||||
{browser === 'firefox' && <FirefoxCard metadata={metadata} version={versions.firefox}/>}
|
||||
{browser === 'chromium' && <ChromiumCard metadata={metadata} version={versions.chrome}/>}
|
||||
{browser === 'unknown' && <UnknownCard/>}
|
||||
|
||||
<Segment basic style={{marginTop: '2em'}}>
|
||||
<Header as='h4'>Both browsers, just in case</Header>
|
||||
<p style={{color: 'grey'}}>
|
||||
You can install for a different browser too — useful if you sync
|
||||
between machines.
|
||||
</p>
|
||||
{browser !== 'firefox' && <FirefoxCard metadata={metadata} version={versions.firefox}/>}
|
||||
{browser !== 'chromium' && <ChromiumCard metadata={metadata} version={versions.chrome}/>}
|
||||
</Segment>
|
||||
</>;
|
||||
}
|
||||
89
app/src/components/admin/ExtensionPage.test.js
Normal file
89
app/src/components/admin/ExtensionPage.test.js
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
import React from 'react';
|
||||
import {mockFetch, render, screen, waitFor} from '../../test-utils';
|
||||
import {ExtensionPage} from './ExtensionPage';
|
||||
|
||||
const FIREFOX_UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:128.0) Gecko/20100101 Firefox/128.0';
|
||||
const CHROME_UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36';
|
||||
const SAFARI_UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15';
|
||||
|
||||
const setUserAgent = (ua) => {
|
||||
Object.defineProperty(window.navigator, 'userAgent', {value: ua, configurable: true});
|
||||
};
|
||||
|
||||
describe('ExtensionPage', () => {
|
||||
let originalFetch;
|
||||
|
||||
beforeEach(() => {
|
||||
originalFetch = global.fetch;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
global.fetch = originalFetch;
|
||||
});
|
||||
|
||||
it('shows the Firefox install card when the user agent is Firefox', async () => {
|
||||
setUserAgent(FIREFOX_UA);
|
||||
global.fetch = mockFetch({
|
||||
files: {
|
||||
'wrolpi-firefox.xpi': {available: true, size_bytes: 700000},
|
||||
'wrolpi-chrome.zip': {available: true, size_bytes: 700000},
|
||||
},
|
||||
versions: {firefox: '0.1.0', chrome: '0.1.0'},
|
||||
});
|
||||
|
||||
render(<ExtensionPage/>);
|
||||
|
||||
const button = await screen.findByText(/Install for Firefox/i);
|
||||
expect(button).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows the Chromium install card with install steps when the user agent is Chrome', async () => {
|
||||
setUserAgent(CHROME_UA);
|
||||
global.fetch = mockFetch({
|
||||
files: {
|
||||
'wrolpi-firefox.xpi': {available: true, size_bytes: 700000},
|
||||
'wrolpi-chrome.zip': {available: true, size_bytes: 700000},
|
||||
},
|
||||
versions: {chrome: '0.1.0', firefox: '0.1.0'},
|
||||
});
|
||||
|
||||
render(<ExtensionPage/>);
|
||||
|
||||
expect(await screen.findByText(/Download \.zip/i)).toBeInTheDocument();
|
||||
expect(screen.getByText(/Developer mode/i)).toBeInTheDocument();
|
||||
expect(screen.getByText(/Load unpacked/i)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows the not-yet-installed warning when no binaries are available', async () => {
|
||||
// This test exists specifically to render the <Message warning> block,
|
||||
// which catches missing imports from the Theme/Semantic-UI module.
|
||||
setUserAgent(CHROME_UA);
|
||||
global.fetch = mockFetch({
|
||||
files: {
|
||||
'wrolpi-firefox.xpi': {available: false, size_bytes: null},
|
||||
'wrolpi-chrome.zip': {available: false, size_bytes: null},
|
||||
},
|
||||
versions: {},
|
||||
});
|
||||
|
||||
render(<ExtensionPage/>);
|
||||
|
||||
expect(await screen.findByText(/Extension binaries not yet installed/i)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('shows the unsupported-browser message for Safari', async () => {
|
||||
setUserAgent(SAFARI_UA);
|
||||
global.fetch = mockFetch({
|
||||
files: {
|
||||
'wrolpi-firefox.xpi': {available: true, size_bytes: 700000},
|
||||
'wrolpi-chrome.zip': {available: true, size_bytes: 700000},
|
||||
},
|
||||
versions: {},
|
||||
});
|
||||
|
||||
render(<ExtensionPage/>);
|
||||
|
||||
expect(await screen.findByText(/Unsupported browser/i)).toBeInTheDocument();
|
||||
});
|
||||
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue