-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
44 lines (40 loc) · 1.91 KB
/
content.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
function initializeNotes() {
function extractUserHash() {
const regex = /user\/([^\/]+)/;
const match = window.location.href.match(regex);
return match ? match[1] : null;
}
const userHash = extractUserHash();
if (userHash) {
chrome.storage.local.get([userHash], function(result) {
const existingNote = result[userHash] || '';
const noteContainer = document.createElement('div');
noteContainer.style.cssText = 'margin-top: 20px; padding: 10px; background-color: #f0f0f0;';
noteContainer.innerHTML = `
<textarea id='noteArea' style='width: 100%; height: 100px;'>${existingNote}</textarea>
<button id='saveNote' style='margin-top: 10px;'>Save Note</button>
`;
// Check every 500ms if the target element is available
const intervalId = setInterval(() => {
const targetElement = document.querySelector('.sc-6c491854-0');
if (targetElement) {
clearInterval(intervalId); // Clear interval once the target element is found
targetElement.appendChild(noteContainer);
document.getElementById('saveNote').addEventListener('click', function() {
const noteText = document.getElementById('noteArea').value;
chrome.storage.local.set({[userHash]: noteText}, function() {
alert('Note saved!');
});
});
}
}, 500);
});
} else {
console.log("User hash not found in URL.");
}
}
if (document.readyState !== "complete") {
window.addEventListener('load', initializeNotes); // Use window 'load' to ensure all scripts and content are fully loaded
} else {
initializeNotes(); // Initialize immediately if the page is already fully loaded
}