-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
71 lines (57 loc) · 2.19 KB
/
content.js
File metadata and controls
71 lines (57 loc) · 2.19 KB
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
let tooltip;
const displayedUrls = new Set();
const displayTimeout = 60000; // 60秒
function showTooltip(element, text) {
if (tooltip) {
document.body.removeChild(tooltip);
}
tooltip = document.createElement('div');
tooltip.textContent = text;
tooltip.style.position = 'absolute';
tooltip.style.backgroundColor = 'white';
tooltip.style.border = '1px solid black';
tooltip.style.padding = '10px';
tooltip.style.zIndex = '1000';
tooltip.style.maxWidth = '300px';
let rect = element.getBoundingClientRect();
tooltip.style.top = `${rect.bottom + window.scrollY}px`;
tooltip.style.left = `${rect.left + window.scrollX}px`;
document.body.appendChild(tooltip);
element.addEventListener('mouseout', function handleMouseOut() {
if (tooltip) {
document.body.removeChild(tooltip);
tooltip = null;
}
element.removeEventListener('mouseout', handleMouseOut);
});
}
document.addEventListener('mouseover', function(event) {
if ((event.target.tagName === 'H1' || event.target.tagName === 'H2') && !displayedUrls.has(window.location.href)) {
let url = window.location.href;
displayedUrls.add(url);
setTimeout(() => {
displayedUrls.delete(url);
}, displayTimeout);
fetch(url)
.then(response => response.text())
.then(html => {
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
const allWrapper = doc.querySelector('.allWrapper');
if (!allWrapper) return;
const mainWrapper = allWrapper.querySelector('.mainWrapper');
if (!mainWrapper) return;
const personalArticle = mainWrapper.querySelector('[id^="PersonalArticlePage"]');
if (!personalArticle) return;
const pItemsMain = personalArticle.querySelector('.p-items_main');
if (!pItemsMain) return;
const paragraphs = pItemsMain.querySelectorAll('p');
const textContent = Array.from(paragraphs).map(p => p.textContent).join(' ');
chrome.runtime.sendMessage({action: "summarize", text: textContent}, function(response) {
if (response.summary) {
showTooltip(event.target, response.summary);
}
});
});
}
});