-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
132 lines (122 loc) · 5.06 KB
/
popup.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
document.addEventListener('DOMContentLoaded', async () => {
addEventListeners();
await populateUI();
});
function addEventListeners() {
//add open options button event listener
document.querySelector('#options').addEventListener('click', function () {
if (chrome.runtime.openOptionsPage) {
chrome.runtime.openOptionsPage();
} else {
window.open(chrome.runtime.getURL('options.html'));
}
});
document.querySelector('#refresh').addEventListener('click', async function () {
fetch_and_save_boards();
await populateUI();
});
//add get decks button event listener
document.getElementById('sendPage').addEventListener('click', async function (event) {
const sendPageButton = document.getElementById('sendPage');
let [currentTab] = await getCurrentTab();
let tabOrigins = new URL(currentTab.url).origin;
chrome.storage.sync.get(["debug_mode", "defaultBoardId", "defaultStackId", "archive", tabOrigins], async (items) => {
if (items.defaultStackId) // if there's no selected stack to send then there's no point of making call
{
if (items.archive) {
archivePage(currentTab.url);
}
if (items.debug_mode) console.log("current tab is:", currentTab);
let cardTitle = currentTab.title; // these are defaults
let cardDescription = "[" + currentTab.url + "](" + currentTab.url + ")"; // these are defaults
if (tabOrigins in items) {
if (items[tabOrigins].cardTitle) {
cardTitle = await evaluateSelectorOnTab(items[tabOrigins].cardTitle, currentTab);
}
if (items[tabOrigins].cardDescription) {
cardDescription = await evaluateSelectorOnTab(items[tabOrigins].cardDescription, currentTab);
cardDescription += "\n\n[" + currentTab.url + "](" + currentTab.url + ")";
}
}
if (!cardTitle) cardTitle = currentTab.title; // in case of Xpath evaluating to null set the defaults
if (!cardDescription) cardDescription = currentTab.url; // in case of Xpath evaluating to null set the defaults
console.log("cardTitle:", cardTitle, "\n cardDescription:", cardDescription);
create_new_card(items.defaultBoardId, items.defaultStackId, cardTitle, 1, cardDescription, ((data) => {
console.log(data);
sendPageButton.classList.toggle('scale-out');
setTimeout(function () { sendPageButton.parentElement.parentElement.remove(); }, 1000);
}));
}
});
});
document.getElementById('saveSelectorsSettings').addEventListener('click', async function () {
let [currentTab] = await getCurrentTab();
let cardTitleSelector = document.getElementById('cardTitle').value;
let cardTitleSelectorResult = await evaluateSelectorOnTab(cardTitleSelector, currentTab);
let cardDescriptionSelector = document.getElementById('cardDescription').value;
let cardDescriptionSelectorResult = await evaluateSelectorOnTab(cardDescriptionSelector, currentTab);
setPreviews(cardTitleSelectorResult, cardDescriptionSelectorResult);
let settings = makeSettings(currentTab, cardTitleSelector, cardDescriptionSelector);
chrome.storage.sync.set(settings, () => {
if (chrome.runtime.lastError) { console.error(chrome.runtime.lastError.message); } // error using browser local storage
console.log("settings set to:", settings);
});
});
}
function makeSettings(currentTab, cardTitleSelector, cardDescriptionSelector) {
let tabOrigins = new URL(currentTab.url).origin;
let settings = { [tabOrigins]: {} };
if (cardTitleSelector) {
settings[tabOrigins]["cardTitle"] = cardTitleSelector;
}
if (cardDescriptionSelector) {
settings[tabOrigins]["cardDescription"] = cardDescriptionSelector;
}
return settings;
}
function setPreviews(cardTitle, cardDescription) {
document.getElementById('cardTitlePreview').value = cardTitle;
document.getElementById('cardDescriptionPreview').value = cardDescription;
}
async function evaluateSelectorOnTab(selector, tab) {
let injectionResults = null;
try {
injectionResults = await chrome.scripting.executeScript({
target: { tabId: tab.id },
func: evaluateSelector,
args: [selector]
})
}
catch (e) {
console.error('Provided XPath expression is not valid', e)
}
if (injectionResults && injectionResults[0].result) {
return injectionResults[0].result;
}
else {
return null;
}
}
async function getCurrentTab() {
return await chrome.tabs.query({ active: true, currentWindow: true });
}
function archivePage(url) {
console.log("not implemented yet");
alert("not implemented yet");
}
function evaluateSelector(selector) {
let iterator = document.evaluate(selector, document, null, XPathResult.ANY_TYPE, null);
let result = '';
try {
let thisNode = iterator.iterateNext();
while (thisNode) {
console.log(thisNode.textContent);
result += thisNode.textContent + " ";
thisNode = iterator.iterateNext();
}
} catch (e) {
console.error(`Error: Document tree modified during iteration ${e}`);
}
console.log("result:", result);
return result;
}