-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathextract.js
134 lines (127 loc) · 3.35 KB
/
extract.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// JS Injected to Extract Links
if (!window.injected) {
console.log('Injected: extract.js')
chrome.runtime.onMessage.addListener(onMessage)
window.injected = true
}
/**
* Handle Messages
* @function onMessage
* @param {String} message
* @param {MessageSender} sender
* @param {Function} sendResponse
*/
function onMessage(message, sender, sendResponse) {
console.debug(`onMessage: message: ${message}`)
if (message === 'all') {
sendResponse(extractAllLinks())
} else if (message === 'selection') {
sendResponse(extractSelection())
} else {
console.warn('Unknown message:', message)
}
}
// /**
// * Extract links
// * @function extractAllLinks
// * @return {Array}
// */
// function extractAllLinks() {
// console.debug('extractAllLinks')
// const links = []
// for (const element of document.links) {
// if (element.href) {
// pushElement(links, element)
// }
// }
// console.debug('links:', links)
// return links
// }
/**
* Extract links
* @function extractAllLinks
* @return {Object[]}
*/
function extractAllLinks() {
console.debug('extractAllLinks')
const links = findLinks(document)
console.debug('links:', links)
return links
}
/**
* Recursively Find Links from shadowRoot
* @function findLinks
* @param {Document|ShadowRoot} root
* @return {Object[]}
*/
function findLinks(root) {
// console.debug('findLinks:', root)
const links = []
if (root.querySelectorAll) {
root.querySelectorAll('a, area').forEach((el) => {
pushElement(links, el)
})
}
const roots = Array.from(root.querySelectorAll('*')).filter(
(el) => el.shadowRoot
)
roots.forEach((el) => {
links.push(...findLinks(el.shadowRoot))
})
return links
}
/**
* A Function
* @function extractSelection
* @return {Object[]}
*/
function extractSelection() {
console.debug('extractSelection')
const links = []
const selection = window.getSelection()
console.debug('selection:', selection)
if (selection?.type !== 'Range') {
console.log('No selection or wrong selection.type')
return links
}
for (let i = 0; i < selection.rangeCount; i++) {
const ancestor = selection.getRangeAt(i).commonAncestorContainer
if (ancestor.nodeName === '#text') {
continue
}
// console.debug('ancestor:', ancestor)
ancestor?.querySelectorAll('a, area')?.forEach((el) => {
if (selection.containsNode(el, true)) {
// console.debug('el:', el)
pushElement(links, el)
}
})
}
console.debug('links:', links)
return links
}
/**
* Add Element to Array
* @function pushElement
* @param {Object[]} array
* @param {HTMLAnchorElement} element
*/
function pushElement(array, element) {
// console.debug('element:', element)
try {
if (element.href) {
const data = {
href: decodeURI(element.href),
text: element.textContent?.trim(),
title: element.title,
label: element.ariaLabel || '',
rel: element.rel,
target: element.target,
origin: element.origin,
}
array.push(data)
}
} catch (e) {
console.log(e)
}
}