-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
154 lines (138 loc) · 5.98 KB
/
popup.js
File metadata and controls
154 lines (138 loc) · 5.98 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
document.addEventListener('DOMContentLoaded', () => {
const monoToggle = document.getElementById('mono-toggle');
const hideToggle = document.getElementById('hide-toggle');
const addFixedBtn = document.getElementById('add-fixed-btn');
const addFloatingBtn = document.getElementById('add-floating-btn');
const clearAreasBtn = document.getElementById('clear-areas-btn');
const areaListContainer = document.getElementById('area-list');
const pageIndicator = document.getElementById('page-indicator');
let currentPageKey = '';
// Load initial state
chrome.storage.sync.get(['monochrome', 'hideEngagement'], (result) => {
monoToggle.checked = result.monochrome !== false;
hideToggle.checked = result.hideEngagement !== false;
});
// Get current tab info and load areas
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs[0] && tabs[0].url) {
try {
const url = new URL(tabs[0].url);
currentPageKey = url.hostname + url.pathname;
pageIndicator.textContent = url.hostname;
pageIndicator.title = currentPageKey;
} catch (e) {
pageIndicator.textContent = 'Unknown page';
}
}
refreshAreaList();
});
function sendMessage(message, callback) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
if (tabs[0]) {
if (tabs[0].url.startsWith("chrome://") || tabs[0].url.startsWith("edge://")) {
console.log("Cannot run on system pages");
if (pageIndicator) pageIndicator.textContent = 'System page';
return;
}
chrome.tabs.sendMessage(tabs[0].id, message, function (response) {
if (chrome.runtime.lastError) {
console.warn("Could not send message:", chrome.runtime.lastError.message);
chrome.scripting.executeScript({
target: { tabId: tabs[0].id },
files: ['content.js']
}).then(() => {
setTimeout(() => {
chrome.tabs.sendMessage(tabs[0].id, message, callback);
}, 100);
});
} else if (callback) {
callback(response);
}
});
}
});
}
function refreshAreaList() {
sendMessage({ action: "getAreas" }, (response) => {
if (response && response.areas) {
renderAreaList(response.areas);
}
});
}
function renderAreaList(areas) {
if (!areas || areas.length === 0) {
areaListContainer.innerHTML = '<div class="no-areas">No color areas for this page</div>';
return;
}
areaListContainer.innerHTML = areas.map((area, index) => {
const typeLabel = area.type === 'fixed' ? 'Fixed' : 'Floating';
const typeIcon = area.type === 'fixed' ? '📌' : '🔄';
return `
<div class="area-item" data-id="${area.id}">
<div class="area-header">
<span class="area-info">Area ${index + 1} (${Math.round(area.width)} x ${Math.round(area.height)})</span>
<div class="area-actions">
<button class="small edit-btn" data-id="${area.id}">Edit</button>
<button class="small danger delete-btn" data-id="${area.id}">Del</button>
</div>
</div>
<div class="area-type-row">
<span>${typeIcon} ${typeLabel}</span>
<button class="small toggle-type-btn" data-id="${area.id}">Switch Type</button>
</div>
</div>
`}).join('');
areaListContainer.querySelectorAll('.edit-btn').forEach(btn => {
btn.addEventListener('click', () => {
const areaId = parseInt(btn.dataset.id);
sendMessage({ action: "editArea", areaId: areaId });
window.close();
});
});
areaListContainer.querySelectorAll('.delete-btn').forEach(btn => {
btn.addEventListener('click', () => {
const areaId = parseInt(btn.dataset.id);
sendMessage({ action: "deleteArea", areaId: areaId }, () => {
refreshAreaList();
});
});
});
areaListContainer.querySelectorAll('.toggle-type-btn').forEach(btn => {
btn.addEventListener('click', () => {
const areaId = parseInt(btn.dataset.id);
sendMessage({ action: "toggleAreaType", areaId: areaId }, () => {
refreshAreaList();
});
});
});
}
monoToggle.addEventListener('change', () => {
const settings = {
monochrome: monoToggle.checked,
hideEngagement: hideToggle.checked
};
chrome.storage.sync.set(settings);
sendMessage({ action: "updateSettings", settings: settings });
});
hideToggle.addEventListener('change', () => {
const settings = {
monochrome: monoToggle.checked,
hideEngagement: hideToggle.checked
};
chrome.storage.sync.set(settings);
sendMessage({ action: "updateSettings", settings: settings });
});
addFixedBtn.addEventListener('click', () => {
sendMessage({ action: "toggleSelectionMode", areaType: "fixed" });
window.close();
});
addFloatingBtn.addEventListener('click', () => {
sendMessage({ action: "toggleSelectionMode", areaType: "floating" });
window.close();
});
clearAreasBtn.addEventListener('click', () => {
sendMessage({ action: "resetArea" }, () => {
refreshAreaList();
});
});
});