Skip to content
This repository was archived by the owner on Oct 25, 2023. It is now read-only.

Commit 2428f75

Browse files
authored
add POC msg-based truthsayer->archaeologist comms (#381)
1 parent f60cf3a commit 2428f75

File tree

24 files changed

+354
-78
lines changed

24 files changed

+354
-78
lines changed

archaeologist/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"react-helmet": "^6.1.0",
6464
"react-shadow": "^19.0.3",
6565
"smuggler-api": "0.0.1",
66+
"truthsayer-archaeologist-communication": "0.0.1",
6667
"ts-loader": "^8.3.0",
6768
"ts-node": "^9.1.1",
6869
"typescript": "4.1.3",

archaeologist/public/manifest.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,9 @@
5757
],
5858
"omnibox": {
5959
"keyword": "//"
60+
},
61+
"externally_connectable": {
62+
"ids": [],
63+
"matches": []
6064
}
6165
}

archaeologist/src/background.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import {
1717
import * as badge from './badge/badge'
1818

1919
import browser, { Tabs } from 'webextension-polyfill'
20+
import {
21+
FromTruthsayer,
22+
ToTruthsayer,
23+
} from 'truthsayer-archaeologist-communication'
2024
import { log, isAbortError, genOriginId, unixtime } from 'armoury'
2125
import {
2226
TNode,
@@ -707,6 +711,26 @@ browser.runtime.onMessage.addListener(
707711
}
708712
)
709713

714+
browser.runtime.onMessageExternal.addListener(
715+
async (
716+
message: FromTruthsayer.Request,
717+
_: browser.Runtime.MessageSender
718+
): Promise<ToTruthsayer.Response> => {
719+
switch (message.type) {
720+
case 'DUMMY_REQUEST': {
721+
return { type: 'VOID_RESPONSE' }
722+
}
723+
default: {
724+
throw new Error(
725+
`background received msg from truthsayer of unknown type, message: ${JSON.stringify(
726+
message
727+
)}`
728+
)
729+
}
730+
}
731+
}
732+
)
733+
710734
// NOTE: on more complex web-pages onUpdated may be invoked multiple times
711735
// with exactly the same input parameters. So the handling code has to
712736
// be able to handle that.

archaeologist/src/content/App.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { v4 as uuidv4 } from 'uuid'
77

88
import { TNode, TNodeJson, NodeType } from 'smuggler-api'
99
import { genOriginId, OriginIdentity, log, productanalytics } from 'armoury'
10-
import { truthsayer_archaeologist_communication } from 'elementary'
10+
import * as truthsayer_archaeologist_communication from 'truthsayer-archaeologist-communication'
1111

1212
import { mazed } from '../util/mazed'
1313

@@ -390,7 +390,7 @@ const App = () => {
390390
<BrowserHistoryImportControlPortal
391391
progress={state.browserHistoryUploadProgress}
392392
/>
393-
<truthsayer_archaeologist_communication.ArchaeologistVersion
393+
<truthsayer_archaeologist_communication.archaeologist.ArchaeologistVersion
394394
version={{
395395
version: browser.runtime.getManifest().version,
396396
}}

archaeologist/src/content/BrowserHistoryImportControl.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,8 @@ import ReactDOM from 'react-dom'
33
import styled from '@emotion/styled'
44
import lodash from 'lodash'
55

6-
import {
7-
truthsayer_archaeologist_communication,
8-
MdiCancel,
9-
MdiCloudUpload,
10-
MdiDelete,
11-
Spinner,
12-
} from 'elementary'
6+
import { MdiCancel, MdiCloudUpload, MdiDelete, Spinner } from 'elementary'
7+
import * as truthsayer_archaeologist_communication from 'truthsayer-archaeologist-communication'
138
import {
149
FromContent,
1510
BrowserHistoryUploadProgress,

archaeologist/src/message/types.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { OriginHash, TNodeJson } from 'smuggler-api'
55
import { OriginIdentity } from 'armoury'
66

77
/**
8-
* There are 3 kind of message senders/receivers:
8+
* There are 3 kinds of message senders/receivers:
99
* - popup
1010
* - content (all browser tabs)
1111
* - background
@@ -17,11 +17,20 @@ import { OriginIdentity } from 'armoury'
1717
* - `ToContent` - from background to content (any of the tabs)
1818
* - `FromContent` - from content (any tab) to background
1919
*
20+
* Additionally, truthsayer can communicate with background.
21+
* @see FromTruthsayer outside of this module for that.
22+
*
2023
* ┌───────┐ ┌────────────┐ ┌─────────┐
2124
* │ popup │ ──▷ │ background │ ──▷ │ content │─┐
2225
* └───────┘ ◁── └────────────┘ ◁── └───(#1)──┘ │─┐
23-
* └───(#2)──┘ │
24-
* └───(#3)──┘
26+
* ^ └───(#2)──┘ │
27+
* : └───(#3)──┘
28+
* :
29+
* ◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦
30+
* ◦ truthsayer ◦
31+
* ◦ (outside this ◦
32+
* ◦ module) ◦
33+
* ◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦◦
2534
*/
2635

2736
export interface VoidResponse {

archaeologist/webpack.config.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ const path = require("path");
33
const CopyPlugin = require("copy-webpack-plugin");
44
const TerserPlugin = require('terser-webpack-plugin');
55

6+
const _getTruthsayerUrl = (mode) => {
7+
return mode === 'development' ? 'http://localhost:3000' : 'https://mazed.se/'
8+
}
9+
10+
const _getTruthsayerUrlMask = (mode) => {
11+
const url = new URL(_getTruthsayerUrl(mode))
12+
url.pathname = '*'
13+
return url.toString()
14+
}
15+
616
const _getSmugglerApiUrl = (mode) => {
717
return mode === 'development'
818
? "http://localhost:3000"
@@ -47,6 +57,11 @@ const _manifestTransform = (buffer, mode, env) => {
4757
// Add Mazed URL to host_permissions to grant access to mazed cookies
4858
const smugglerApiUrlMask = _getSmugglerApiUrlMask(mode)
4959
manifest.host_permissions.push(smugglerApiUrlMask)
60+
// Add Mazed URL to externally_connectable to allow to send messages
61+
// from truthsayer to archaeologist.
62+
// See https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/manifest.json/externally_connectable
63+
// for more details.
64+
manifest.externally_connectable.matches.push(_getTruthsayerUrlMask(mode))
5065
if (firefox) {
5166
manifest = _manifestTransformDowngradeToV2(manifest)
5267
}

elementary/src/index.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,3 @@ export {
5858
kSlateBlockTypeQuote,
5959
kSlateBlockTypeUnorderedList,
6060
} from './editor/types'
61-
62-
// The name is ugly on purpose - use with extra care
63-
export * as truthsayer_archaeologist_communication from './truthsayer-archaeologist-communication/index'

elementary/src/truthsayer-archaeologist-communication/Version.tsx

Lines changed: 0 additions & 52 deletions
This file was deleted.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"scripts": {
66
"archaeologist": "yarn workspace archaeologist",
77
"elementary": "yarn workspace elementary",
8+
"truthsayer-archaeologist-communication": "yarn workspace truthsayer-archaeologist-communication",
89
"armoury": "yarn workspace armoury",
910
"fix": "yarn syncpack fix-mismatches && yarn workspaces foreach run fix",
1011
"fix:prettier": "yarn workspaces foreach run fix:prettier",
@@ -19,6 +20,7 @@
1920
"armoury",
2021
"elementary",
2122
"librarius",
23+
"truthsayer-archaeologist-communication",
2224
"smuggler-api",
2325
"truthsayer"
2426
],

0 commit comments

Comments
 (0)