Skip to content

Commit 41d2d6f

Browse files
committed
fix: locate frame under shadow dom
Fixes #2349
1 parent 22fe304 commit 41d2d6f

3 files changed

Lines changed: 27 additions & 9 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"build:mv3": "webpack --config webpack.mv3.config.js --mode production",
1111
"build": "yarn build:mv2 && yarn build:mv3",
1212
"postbuild": "node scripts/firefox-fix.js",
13-
"devbuild": "webpack --mode development",
13+
"devbuild:mv3": "webpack --config webpack.mv3.config.js --mode production --debug",
1414
"type-check": "tsc --noEmit",
1515
"test": "jest --testTimeout 20000",
1616
"test:watch": "jest --watch",

src/selection/message.ts

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,36 @@ interface PostMessageEvent extends MessageEvent {
88
}
99
}
1010

11+
function findFrameBySource(source: MessageEventSource | null) {
12+
const roots: Array<Document | ShadowRoot> = [document]
13+
14+
while (roots.length) {
15+
const root = roots.pop()!
16+
const frame = Array.from(
17+
root.querySelectorAll<HTMLIFrameElement | HTMLFrameElement>(
18+
'iframe, frame'
19+
)
20+
).find(({ contentWindow }) => contentWindow === source)
21+
22+
if (frame) {
23+
return frame
24+
}
25+
26+
for (const element of Array.from(root.querySelectorAll('*'))) {
27+
if (element.shadowRoot) {
28+
roots.push(element.shadowRoot)
29+
}
30+
}
31+
}
32+
}
33+
1134
export function postMessageHandler({ data, source }: PostMessageEvent) {
1235
if (!data || data.type !== 'SALADICT_SELECTION') {
1336
return
1437
}
1538

16-
// get the souce iframe
17-
const matchSrc = ({ contentWindow }: HTMLIFrameElement | HTMLFrameElement) =>
18-
contentWindow === source
19-
20-
const frame =
21-
Array.from(document.querySelectorAll('iframe')).find(matchSrc) ||
22-
Array.from(document.querySelectorAll('frame')).find(matchSrc)
39+
// Search open shadow roots because reader frames may be nested inside one.
40+
const frame = findFrameBySource(source)
2341

2442
if (!frame) {
2543
return

src/selection/quick-search.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { whenKeyPressed, isQSKey } from './helper'
77
/**
88
* Listen to triple-ctrl shortcut which opens quick search panel.
99
* Pressing ctrl/command key more than three times within 500ms
10-
* trigers triple-ctrl.
10+
* triggers triple-ctrl.
1111
*/
1212
export function createQuickSearchStream(config: AppConfig | null) {
1313
if (!config || !config.tripleCtrl || isStandalonePage() || isOptionsPage()) {

0 commit comments

Comments
 (0)