-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackground.js
More file actions
95 lines (84 loc) · 3.25 KB
/
Background.js
File metadata and controls
95 lines (84 loc) · 3.25 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
document.addEventListener('DOMContentLoaded', () => {
// Load query history and set up event listeners
loadQueryHistory();
document.getElementById('runQuery').addEventListener('click', () => {
const query = document.getElementById('query').value;
if (query) {
saveQueryToHistory(query);
runQuery(query);
}
});
document.getElementById('queryHistory').addEventListener('change', (event) => {
const selectedQuery = event.target.value;
if (selectedQuery) {
document.getElementById('query').value = selectedQuery;
}
});
document.getElementById('runScript').addEventListener('click', () => {
const script = document.getElementById('scriptSelect').value;
if (script) {
if (script === 'checkUnappliedPayments') {
runUnappliedPaymentsCheck(); // Run the specific script
}
}
});
});
function runQuery(query) {
console.log('Sending query:', query);
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const activeTab = tabs[0];
chrome.scripting.executeScript({
target: { tabId: activeTab.id },
func: (query) => {
window.postMessage({ type: 'RUN_QUERY', query: query }, '*');
},
args: [query]
});
});
}
function runUnappliedPaymentsCheck() {
console.log('Running unapplied payments check...');
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const activeTab = tabs[0];
chrome.scripting.executeScript({
target: { tabId: activeTab.id },
func: () => {
window.postMessage({ type: 'RUN_UNAPPLIED_PAYMENTS_CHECK' }, '*');
}
});
});
}
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.type === 'QUERY_RESULTS') {
console.log('Received query results:', message.data);
const results = JSON.parse(message.data);
openResultsInNewWindow(results);
} else if (message.type === 'UNAPPLIED_PAYMENTS_RESULT') {
console.log('Received unapplied payments result:', message.data);
document.getElementById('output').textContent = message.data;
}
});
// Function to save the query to history
function saveQueryToHistory(query) {
const history = JSON.parse(localStorage.getItem('queryHistory')) || [];
if (!history.includes(query)) {
history.unshift(query);
if (history.length > 10) {
history.pop();
}
localStorage.setItem('queryHistory', JSON.stringify(history));
loadQueryHistory();
}
}
// Load query history into the dropdown
function loadQueryHistory() {
const history = JSON.parse(localStorage.getItem('queryHistory')) || [];
const historyDropdown = document.getElementById('queryHistory');
historyDropdown.innerHTML = '<option value="" disabled selected>Select a previous query...</option>'; // Reset options
history.forEach(query => {
const option = document.createElement('option');
option.value = query;
option.textContent = query.length > 50 ? query.substring(0, 47) + '...' : query;
historyDropdown.appendChild(option);
});
}