Skip to content

Commit 240d19b

Browse files
author
Shane Osbourne
committed
Docs support for special pages & better organisation
1 parent e046eb8 commit 240d19b

29 files changed

+239
-89
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ docs/
33
lib/
44
Sources/ContentScopeScripts/dist/
55
integration-test/extension/contentScope.js
6+
packages/special-pages/pages/**/public

inject/android.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* @module Android integration
3+
* @category Content Scope Scripts Integrations
34
*/
45
/* global contentScopeFeatures */
56
import { processConfig, isGloballyDisabled } from './../src/utils'

inject/apple.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* @module Apple integration
3+
* @category Content Scope Scripts Integrations
34
*/
45
/* global contentScopeFeatures */
56

inject/chrome-mv3.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* @module Chrome MV3 integration
3+
* @category Content Scope Scripts Integrations
34
*/
45
/* global contentScopeFeatures */
56

inject/chrome.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* @module Chrome integration
3+
* @category Content Scope Scripts Integrations
34
*/
45
import { isTrackerOrigin } from '../src/trackers'
56

inject/integration.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/**
2-
* @module Testing integration
3-
*/
4-
51
/* global contentScopeFeatures */
62
function getTopLevelURL () {
73
try {

inject/mozilla.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* @module Mozilla integration
3+
* @category Content Scope Scripts Integrations
34
*/
45
/* global contentScopeFeatures */
56
import { isTrackerOrigin } from '../src/trackers'

inject/windows.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* @module Windows integration
3+
* @category Content Scope Scripts Integrations
34
*/
45
/* global contentScopeFeatures */
56
import { processConfig, isGloballyDisabled, windowsSpecificFeatures } from './../src/utils'

packages/messaging/index.js

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* @module Messaging
3-
*
3+
* @category Libraries
44
* @description
55
*
66
* An abstraction for communications between JavaScript and host platforms.
@@ -9,48 +9,20 @@
99
* 2) Then use that to get an instance of the Messaging utility which allows
1010
* you to send and receive data in a unified way
1111
* 3) Each platform implements {@link MessagingTransport} along with its own Configuration
12-
* - For example, to learn what configuration is required for Webkit, see: {@link "Webkit Messaging".WebkitMessagingConfig}
13-
* - Or, to learn about how messages are sent and received in Webkit, see {@link "Webkit Messaging".WebkitMessagingTransport}
12+
* - For example, to learn what configuration is required for Webkit, see: {@link WebkitMessagingConfig}
13+
* - Or, to learn about how messages are sent and received in Webkit, see {@link WebkitMessagingTransport}
1414
*
1515
* @example Webkit Messaging
1616
*
17-
* ```js
18-
* import { Messaging, WebkitMessagingConfig } from "@duckduckgo/content-scope-scripts/lib/messaging.js"
19-
*
20-
* // This config would be injected into the UserScript
21-
* const injectedConfig = {
22-
* hasModernWebkitAPI: true,
23-
* webkitMessageHandlerNames: ["foo", "bar", "baz"],
24-
* secret: "dax",
25-
* };
26-
*
27-
* // Then use that config to construct platform-specific configuration
28-
* const config = new WebkitMessagingConfig(injectedConfig);
29-
*
30-
* // finally, get an instance of Messaging and start sending messages in a unified way 🚀
31-
* const messaging = new Messaging(config);
32-
* messaging.notify("hello world!", {foo: "bar"})
33-
*
34-
* ```
17+
* ```javascript
18+
* [[include:packages/messaging/lib/examples/webkit.example.js]]```
3519
*
3620
* @example Windows Messaging
3721
*
38-
* ```js
39-
* import { Messaging, WindowsMessagingConfig } from "@duckduckgo/content-scope-scripts/lib/messaging.js"
40-
*
41-
* // Messaging on Windows is namespaced, so you can create multiple messaging instances
42-
* const autofillConfig = new WindowsMessagingConfig({ featureName: "Autofill" });
43-
* const debugConfig = new WindowsMessagingConfig({ featureName: "Debugging" });
44-
*
45-
* const autofillMessaging = new Messaging(autofillConfig);
46-
* const debugMessaging = new Messaging(debugConfig);
47-
*
48-
* // Now send messages to both features as needed 🚀
49-
* autofillMessaging.notify("storeFormData", { "username": "dax" })
50-
* debugMessaging.notify("pageLoad", { time: window.performance.now() })
51-
* ```
22+
* ```javascript
23+
* [[include:packages/messaging/lib/examples/windows.example.js]]```
5224
*/
53-
import { WindowsMessagingConfig, WindowsMessagingTransport } from './lib/windows.js'
25+
import { WindowsMessagingConfig, WindowsMessagingTransport, WindowsInteropMethods } from './lib/windows.js'
5426
import { WebkitMessagingConfig, WebkitMessagingTransport } from './lib/webkit.js'
5527

5628
/**
@@ -176,4 +148,10 @@ export class MissingHandler extends Error {
176148
/**
177149
* Some re-exports for convenience
178150
*/
179-
export { WebkitMessagingConfig, WindowsMessagingConfig }
151+
export {
152+
WebkitMessagingConfig,
153+
WebkitMessagingTransport,
154+
WindowsMessagingConfig,
155+
WindowsMessagingTransport,
156+
WindowsInteropMethods
157+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Messaging, WebkitMessagingConfig } from '../../index.js'
2+
3+
/**
4+
* Webkit messaging involves calling methods on `window.webkit.messageHandlers`.
5+
*
6+
* For catalina support we support encryption which is the bulk of this configuration
7+
*/
8+
const config = new WebkitMessagingConfig({
9+
hasModernWebkitAPI: true,
10+
secret: 'SECRET',
11+
webkitMessageHandlerNames: ['helloWorld', 'sendPixel'],
12+
})
13+
14+
/**
15+
* Send notifications!
16+
*/
17+
const messaging = new Messaging(config)
18+
messaging.notify('sendPixel')
19+
20+
/**
21+
* Or request some data
22+
*/
23+
messaging.request('helloWorld', { foo: 'bar' }).then(console.log).catch(console.error)

packages/messaging/lib/webkit.js

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/**
2-
* @module Webkit Messaging
32
*
43
* @description
54
*
@@ -9,24 +8,8 @@
98
* Note: If you wish to support Catalina then you'll need to implement the native
109
* part of the message handling, see {@link WebkitMessagingTransport} for details.
1110
*
12-
* ```js
13-
* import { Messaging, WebkitMessagingConfig } from "@duckduckgo/content-scope-scripts/lib/messaging.js"
14-
*
15-
* // This config would be injected into the UserScript
16-
* const injectedConfig = {
17-
* hasModernWebkitAPI: true,
18-
* webkitMessageHandlerNames: ["foo", "bar", "baz"],
19-
* secret: "dax",
20-
* };
21-
*
22-
* // Then use that config to construct platform-specific configuration
23-
* const config = new WebkitMessagingConfig(injectedConfig);
24-
*
25-
* // finally, get an instance of Messaging and start sending messages in a unified way 🚀
26-
* const messaging = new Messaging(config);
27-
* messaging.notify("hello world!", {foo: "bar"})
28-
*
29-
* ```
11+
* ```javascript
12+
* [[include:packages/messaging/lib/examples/webkit.example.js]]```
3013
*/
3114
// eslint-disable-next-line @typescript-eslint/no-unused-vars
3215
import { MessagingTransport, MissingHandler } from '../'

packages/messaging/lib/windows.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
/**
2-
* @module Windows Messaging
3-
*
42
* @description
53
*
64
* A wrapper for messaging on Windows.

packages/messaging/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"devDependencies": {
88
},
99
"scripts": {
10-
"test": "echo \"Error: no test specified\" && exit 1"
10+
"test": "echo '✅ No tests yet'"
1111
},
1212
"author": "",
1313
"license": "ISC"

packages/special-pages/index.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
1+
/**
2+
* @module Special Pages
3+
* @category Special Pages
4+
*
5+
* @description
6+
*
7+
* A collection of HTML/CSS/JS pages that can be loaded into privileged environments, like `about: pages`
8+
*
9+
* - {@link "Example Page"}
10+
*/
111
import { join } from 'node:path'
212
import { existsSync, cpSync, rmSync } from 'node:fs'
313
const CWD = new URL('.', import.meta.url).pathname
414
const ROOT = new URL('../../', import.meta.url).pathname
515
const BUILD = join(ROOT, 'build')
616

717
export const support = {
8-
// example: ['windows']
18+
example: ['windows']
919
}
1020

1121
/** @type {{src: string, dest: string}[]} */
1222
const copyJobs = []
1323
const errors = []
1424

1525
for (const [pageName, platforms] of Object.entries(support)) {
16-
const src = join(CWD, 'pages', pageName)
26+
const src = join(CWD, 'pages', pageName, 'public')
1727
if (!existsSync(src)) {
18-
errors.push(`${src} does not exist`)
28+
errors.push(`${src} does not exist. Each page must have a 'public' directory`)
1929
continue
2030
}
2131
for (const platform of platforms) {
@@ -36,7 +46,7 @@ if (errors.length > 0) {
3646

3747
if (errors.length === 0) {
3848
for (const copyJob of copyJobs) {
39-
console.log('DRY RUN COPY: ', copyJob)
49+
console.log('COPY: ', copyJob)
4050
rmSync(copyJob.dest, { force: true, recursive: true })
4151
cpSync(copyJob.src, copyJob.dest, { force: true, recursive: true })
4252
}

packages/special-pages/package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
{
22
"name": "special-pages",
3+
"private": "true",
34
"version": "1.0.0",
45
"description": "A collection of HTML/CSS/JS pages that can be loaded into privileged environments, like about: pages",
56
"main": "index.js",
67
"type": "module",
78
"scripts": {
8-
"build": "node index.js",
9+
"build": "npm run build.pages; node index.js",
10+
"build.pages": "node scripts/build.js",
911
"test": "echo '✅ No tests yet'",
1012
"test.headed": "playwright test --headed",
1113
"test-int-x": "playwright test",
@@ -15,6 +17,8 @@
1517
"license": "ISC",
1618
"devDependencies": {
1719
"@playwright/test": "^1.31.2",
18-
"serve": "^14.2.0"
20+
"serve": "^14.2.0",
21+
"@duckduckgo/messaging": "*",
22+
"esbuild": "^0.17.12"
1923
}
2024
}

packages/special-pages/pages/example/.gitignore

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*

packages/special-pages/pages/example/index.html renamed to packages/special-pages/pages/example/public/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
77
<meta http-equiv="X-UA-Compatible" content="ie=edge">
88
<title>Example special page</title>
9-
<link rel="stylesheet" href="./style.css">
9+
<link rel="stylesheet" href="style.css">
1010
</head>
1111
<body>
12-
<header class="header"><img src="./img/logo-horizontal.svg" class="logo" /></header>
12+
<header class="header"><img src="img/logo-horizontal.svg" class="logo" /></header>
1313
<h1 class="title">Hello world from a special page :)</h1>
14-
<script src="./script.js"></script>
14+
<script type="module" src="generated/js/index.js"></script>
1515
</body>
1616
</html>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
This is an example 'special' page. It contains a `public` folder which will be the output
2+
that native platforms will refer to

packages/special-pages/pages/example/script.js

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* @module Example Page
3+
* @category Special Pages
4+
*
5+
* @description
6+
*
7+
* [[include:packages/special-pages/pages/example/readme.md]]
8+
*/
9+
10+
import { Messaging, WindowsMessagingConfig } from '../../../../messaging/index.js'
11+
import { ExamplePageMessages, } from './messages.js'
12+
import { Pixel, OverlayPixel, PlayPixel, PlayDoNotUse } from './pixels.js'
13+
14+
/**
15+
* An example of initialising Windows messaging
16+
*/
17+
const config = new WindowsMessagingConfig({
18+
featureName: 'ExamplePage',
19+
methods: {
20+
postMessage: (...args) => {
21+
console.log('postMessage', args)
22+
},
23+
addEventListener: (...args) => {
24+
console.log('addEventListener', args)
25+
},
26+
removeEventListener: (...args) => {
27+
console.log('removeEventListener', args)
28+
}
29+
}
30+
})
31+
32+
const messages = new Messaging(config)
33+
console.log(messages)
34+
35+
const h2 = document.createElement('h2')
36+
h2.innerText = 'This is an appended element'
37+
document.body.appendChild(h2)
38+
39+
try {
40+
// @ts-expect-error - this is deliberate to ensure tsc is checking this file
41+
document.body.appendChild('ooops!')
42+
} catch (e) {
43+
console.log('warn: Expected error')
44+
}
45+
46+
export { ExamplePageMessages, Pixel, OverlayPixel, PlayPixel, PlayDoNotUse }
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
export class ExamplePageMessages {
2+
/**
3+
* @param {import("@duckduckgo/messaging").Messaging} messaging
4+
* @internal
5+
*/
6+
constructor (messaging) {
7+
this.messaging = messaging
8+
}
9+
10+
requests = {
11+
/**
12+
* @returns {Promise<boolean>}
13+
*/
14+
getOptions: async () => {
15+
return this.messaging.request('hello', { world: 'here' })
16+
}
17+
}
18+
19+
notifications = {
20+
/**
21+
* @param {import("./pixels.js").Pixel} knownPixel
22+
*/
23+
sendPixel: (knownPixel) => {
24+
this.messaging.notify(knownPixel.name(), knownPixel.params())
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)