-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontentScript_cambridge.js
More file actions
125 lines (124 loc) · 4.65 KB
/
Copy pathcontentScript_cambridge.js
File metadata and controls
125 lines (124 loc) · 4.65 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
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
class Word {
constructor(word, date, def, ipaNA = "", ipaUK = "", pos = "") {
this.word = word;
this.date = date;
this.def = def;
this.pos = pos;
this.ipaNA = ipaNA;
this.ipaUK = ipaUK;
}
}
class VocabularyExtension {
constructor() {
this.initialize();
}
initialize() {
this.injectButtonIntoPage();
document.addEventListener("DOMContentLoaded", () => {
chrome.runtime.onMessage.addListener(this.handleMessage.bind(this));
});
}
injectButtonIntoPage() {
try{
const entryElements = document.getElementsByClassName("pr entry-body__el");
if (entryElements.length === 0) {
throw new Error("No elements found.");
}
for(let i = 0; i < entryElements.length; i++){
const entryElement = entryElements[i];
const defElements = entryElement.querySelectorAll(".ddef_h");
defElements.forEach((defElement)=>{
if(defElement){
const button = this.createButton();
defElement.appendChild(button);
button.addEventListener("click",()=>{
const {ipaUK, ipaNA} = this.getIPA();
const word = new Word(
this.getWord(),
this.getDate(),
this.extractDefinition(),
ipaNA,
ipaUK,
this.getPos(),
);
console.log(word);
chrome.runtime.sendMessage(
{ action: "sendToNotion", data: word },
(response) => {
if (response.success) {
alert("Vocabulary sent to Notion successfully!");
} else {
alert(response.error);
}
}
);
});
} else{
throw new Error("No definitions found!");
}
});
}
}
catch (error){
console.error(error.message);
}
}
createButton() {
const button = document.createElement("img");
button.src = chrome.runtime.getURL("icons/send.png");
button.alt = "Send to Notion";
//css for the button layout
button.style.cursor = "pointer";
button.style.verticalAlign = "middle";
button.style.border = "2px solid #a6a6a6";
button.style.background = "#d1d1d1";
button.style.borderRadius = "10%";
button.style.border
button.style.marginLeft = "10px";
button.style.width = "30px";
button.style.height = "30px";
return button;
}
extractDefinition(){
const divElement = document.querySelector(".def.ddef_d.db");
if (divElement) {
return divElement.textContent.trim();
} else {
console.error("Definition Element not found.");
}
}
getPos(){
const spanElement = document.querySelector("span.pos.dpos");
if (spanElement) {
const pos = spanElement.textContent;
return pos;
}
else {
console.error("POS Element not found.");
}
}
getIPA(){
const ipaUKElement = document.querySelector("span.uk.dpron-i span.pron.dpron span.ipa.dipa.lpr-2.lpl-1");
const ipaNAElement = document.querySelector("span.us.dpron-i span.pron.dpron span.ipa.dipa.lpr-2.lpl-1");
const ipaUK = ipaUKElement? "/"+ipaUKElement.textContent.trim() + "/": "";
const ipaNA = ipaNAElement? "/"+ipaNAElement.textContent.trim() + "/": "";
return {ipaUK, ipaNA};
}
getWord(){
const spanElement = document.querySelector("span.hw.dhw");
if (spanElement) {
const word = spanElement.textContent;
return word;
} else {
console.error("Word Element not found.");
}
}
getDate() {
const currentDate = new Date();
const year = currentDate.getFullYear();
const month = (currentDate.getMonth() + 1).toString().padStart(2, "0");
const day = currentDate.getDate().toString().padStart(2, "0");
return `${year}-${month}-${day}`;
}
}
const vocabExtension = new VocabularyExtension();