Skip to content

Commit

Permalink
Add demo/test extension
Browse files Browse the repository at this point in the history
  • Loading branch information
fregante committed Nov 8, 2024
1 parent 2daea03 commit bf064a8
Show file tree
Hide file tree
Showing 15 changed files with 203 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ logs
*.map
index.js
index.d.ts
distribution
3 changes: 3 additions & 0 deletions .parcelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "@parcel/config-webextension"
}
2 changes: 2 additions & 0 deletions demo/action.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<meta name="color-scheme" content="dark light">
<script type="module" src="main.js"></script>
31 changes: 31 additions & 0 deletions demo/init/context-menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* global chrome */
import mainHtmlUrl from 'url:../main.html';
import {isChrome} from '../../index.ts';

if (globalThis.chrome?.contextMenus?.create) {
chrome.contextMenus.removeAll();
chrome.contextMenus.create({
id: 'OPEN_ALL',
title: 'Open all contexts',
contexts: ['action'],
});

chrome.contextMenus.onClicked.addListener((info, tab) => {
if (info.menuItemId === 'OPEN_ALL') {
chrome.sidePanel?.open({
windowId: tab.windowId,
});
chrome.runtime.openOptionsPage();
chrome.tabs.create({
url: mainHtmlUrl,
});
chrome.tabs.create({
url: 'https://ephiframe.vercel.app/?iframe=' + chrome.runtime.getURL('sandbox.html'),
});

chrome.tabs.create({
url: isChrome() ? 'chrome://extensions/' : 'about:debugging#/runtime/this-firefox',
});
}
});
}
7 changes: 7 additions & 0 deletions demo/init/devtools-tab.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import mainHtmlUrl from 'url:../main.html';

globalThis.chrome?.devtools?.panels?.create(
'WEBEXT-DETECT',
'icon.png',
mainHtmlUrl,
);
8 changes: 8 additions & 0 deletions demo/init/offscreen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/* global chrome */
import mainHtmlUrl from 'url:../main.html';

globalThis.chrome?.offscreen?.createDocument({
url: mainHtmlUrl,
reasons: [chrome.offscreen.Reason.DOM_PARSER],
justification: 'Testing',
}).catch(() => {}); // eslint-disable-line unicorn/prefer-top-level-await -- shh
2 changes: 2 additions & 0 deletions demo/main.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<meta name="color-scheme" content="dark light">
<script type="module" src="main.js"></script>
37 changes: 37 additions & 0 deletions demo/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* eslint-disable import/no-unassigned-import */
import * as detect from '../index.ts';
import './init/context-menu.js';
import './init/devtools-tab.js';
import './init/offscreen.js';

const table = Object.entries(detect)
.filter(([key, exported]) => typeof exported === 'function' && key !== 'disableWebextDetectPageCache')
.map(([key, detection]) => [key, detection()])
.sort(([keyA, resultA], [keyB, resultB]) => {
if (resultA === resultB) {
return keyA.localeCompare(keyB);
}

return resultA === true ? -1 : 1;
});

console.table(table);

if ('document' in globalThis) {
globalThis.document.body.insertAdjacentHTML('beforeend', `
<fieldset>
<legend>${detect.getContextName()}</legend>
<ul>
${table.map(([key, result]) => `
<li>
<span>
${result === true ? '✅' : (result === false ? '❌' : '')}
${key}
${typeof result === 'string' ? `: ${result}` : ''}
</span>
</li>
`).join('')}
</ul>
</fieldset>
`);
}
4 changes: 4 additions & 0 deletions demo/mainworld.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// It needs to be a standalone file or else the browser won't try to inject once per world

// eslint-disable-next-line import/no-unassigned-import
import './main.js';
47 changes: 47 additions & 0 deletions demo/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"$schema": "https://json.schemastore.org/chrome-manifest",
"manifest_version": 3,
"name": "webext-detect",
"version": "0.0.0",
"description": "Demonstrates the webext-detect module",
"permissions": [
"contextMenus",
"sidePanel",
"offscreen"
],
"background": {
"service_worker": "main.js",
"type": "module",
"scripts": ["main.js"]
},
"action": {
"default_popup": "action.html"
},
"options_ui": {
"page": "options.html"
},
"devtools_page": "main.html",
"side_panel": {
"default_path": "sidepanel.html"
},
"sandbox": {
"pages": ["sandbox.html"]
},
"web_accessible_resources": [
{
"resources": [ "sandbox.html" ],
"matches": [ "https://*/*" ]
}
],
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["main.js"]
},
{
"world": "MAIN",
"matches": ["<all_urls>"],
"js": ["mainworld.js"]
}
]
}
2 changes: 2 additions & 0 deletions demo/options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<meta name="color-scheme" content="dark light">
<script type="module" src="main.js"></script>
26 changes: 26 additions & 0 deletions demo/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Demo for webext-detect

1. Run `npm run demo:build` in the project root
2. Load the extension in Chrome

## Available context and how to open them

Click the extension icon in the toolbar:

- Popup

Right-click the icon and select "Open all contexts":

- Options
- SidePanel
- Generic extension page
- Sandboxed iframe

Open the devtools and select the "WEBEXT-DETECT" tab:

- DevTools

In `chrome://extensions`, see the open debuggable contexts:

- Background
- Offscreen
5 changes: 5 additions & 0 deletions demo/sandbox.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<meta name="color-scheme" content="dark light">
<h1>Dedicated sandbox.html page</h1>
<p>This file is treated as a sandboxed page when loaded in an iframe.</p>
<script type="module" src="main.js"></script>

2 changes: 2 additions & 0 deletions demo/sidepanel.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<meta name="color-scheme" content="dark light">
<script type="module" src="main.js"></script>
26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
],
"scripts": {
"build": "tsc",
"demo:build": "NODE_NO_WARNINGS=1 parcel build --no-cache",
"demo:watch": "NODE_NO_WARNINGS=1 parcel watch --no-cache --no-hmr",
"prepare": "tsc --sourceMap false",
"test": "xo && tsc && node index.js",
"watch": "tsc --watch"
Expand All @@ -47,12 +49,36 @@
}
},
"devDependencies": {
"@parcel/config-webextension": "^2.12.1-canary.3358",
"@parcel/resolver-default": "^2.0.0-canary.1735",
"@sindresorhus/tsconfig": "^6.0.0",
"@types/chrome": "^0.0.268",
"parcel": "^2.0.0-canary.1733",
"typescript": "^5.5.2",
"xo": "^0.58.0"
},
"engines": {
"node": ">=18"
},
"@parcel/resolver-default": {
"packageExports": true
},
"targets": {
"main": false,
"default": {
"source": "demo/manifest.json",
"distDir": "./distribution",
"sourceMap": {
"inline": true
}
}
},
"webExt": {
"sourceDir": "distribution",
"run": {
"startUrl": [
"https://example.com"
]
}
}
}

0 comments on commit bf064a8

Please sign in to comment.