Skip to content

Commit 997f3df

Browse files
author
Shane Osbourne
committed
better docs and examples
1 parent 1a373e2 commit 997f3df

File tree

7 files changed

+53
-70
lines changed

7 files changed

+53
-70
lines changed

package-lock.json

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/**
2-
* @module Webkit Messaging
32
*
43
* @description
54
*
@@ -9,27 +8,11 @@
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
32-
import { MessagingTransport, MissingHandler } from '../'
15+
import { MessagingTransport, MissingHandler } from '../index.js'
3316

3417
/**
3518
* @example
@@ -354,7 +337,7 @@ export class SecureMessagingParams {
354337
* scripts from tampering with this
355338
*/
356339
function captureGlobals() {
357-
// Creat base with null prototype
340+
// Create base with null prototype
358341
return {
359342
window,
360343
// Methods must be bound to their interface, otherwise they throw Illegal invocation

packages/messaging/lib/windows.js

Lines changed: 1 addition & 3 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.
@@ -15,7 +13,7 @@
1513
*/
1614

1715
// eslint-disable-next-line @typescript-eslint/no-unused-vars
18-
import { MessagingTransport } from '../'
16+
import { MessagingTransport } from '../index.js'
1917

2018
/**
2119
* @implements {MessagingTransport}

packages/messaging/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
"version": "1.0.0",
55
"description": "",
66
"main": "index.js",
7-
"devDependencies": {
8-
},
7+
"type": "module",
98
"scripts": {
10-
"test": "echo \"Error: no test specified\" && exit 1"
9+
"test": "echo '✅ No tests yet'"
1110
},
1211
"author": "",
1312
"license": "ISC"

typedoc.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
{
22
"name": "@duckduckgo/workspaces-demo",
33
"entryPoints": [
4-
"inject/*",
5-
"packages/*",
6-
"packages/messaging/lib/webkit.js",
7-
"packages/messaging/lib/windows.js"
4+
"inject/android.js",
5+
"inject/apple.js",
6+
"inject/chrome.js",
7+
"inject/chrome-mv3.js",
8+
"inject/mozilla.js",
9+
"inject/windows.js",
10+
"packages/*"
811
],
912
"out": "docs",
1013
"excludeExternals": true,

0 commit comments

Comments
 (0)