Skip to content

Commit 026daa1

Browse files
authored
Merge pull request #34 from JOHLC/copilot/add-context-selection-option
Add context selection UI to extension popup
2 parents b945046 + 1eb017a commit 026daa1

File tree

3 files changed

+96
-7
lines changed

3 files changed

+96
-7
lines changed

package/popup.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
</button>
2626
<div class="container">
2727
<div class="icon"><img src="icon-256.png" width="36" height="36" alt="HA"></div>
28-
<div class="msg" id="popupMsg">Sending...</div>
28+
<div class="msg" id="popupMsg">Select context to send:</div>
29+
<div id="contextSelection" class="context-selection"></div>
2930
<button class="ok-btn hidden" id="okBtn">OK</button>
3031
</div>
3132
<script src="utils.js"></script>

package/popup.js

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,76 @@ document.addEventListener('DOMContentLoaded', function() {
2727

2828
const msgDiv = document.getElementById('popupMsg');
2929
const okBtn = document.getElementById('okBtn');
30+
const contextSelection = document.getElementById('contextSelection');
3031

3132
/**
32-
* Main function to send page data to Home Assistant
33+
* Initialize popup by loading context menu items
3334
*/
34-
async function sendToHA() {
35-
updateStatus('Sending...');
36-
hideButton();
37-
35+
async function initPopup() {
3836
try {
3937
const tab = await getActiveTab();
4038
if (!tab) {
4139
throw new Error('No active tab found');
4240
}
4341

42+
// Check for restricted pages
43+
if (ExtensionUtils.isRestrictedPage(tab.url)) {
44+
updateStatus('This extension cannot send data from browser internal pages (settings, extensions, etc.). Please navigate to a regular website and try again.');
45+
showButton();
46+
return;
47+
}
48+
49+
// Load context menu items and display selection UI
50+
chrome.storage.sync.get(['contextMenuItems'], (result) => {
51+
let menuItems = [{ id: 'default', name: 'Default' }]; // Default fallback
52+
53+
if (result.contextMenuItems && Array.isArray(result.contextMenuItems)) {
54+
menuItems = result.contextMenuItems;
55+
}
56+
57+
displayContextSelection(menuItems, tab);
58+
});
59+
} catch (error) {
60+
console.error('Popup initialization failed:', error);
61+
handleError(error);
62+
showButton();
63+
}
64+
}
65+
66+
/**
67+
* Display context selection buttons
68+
* @param {Array} menuItems - Array of menu items
69+
* @param {object} tab - Tab object
70+
*/
71+
function displayContextSelection(menuItems, tab) {
72+
contextSelection.innerHTML = '';
73+
74+
menuItems.forEach((item) => {
75+
const btn = document.createElement('button');
76+
btn.className = 'context-btn';
77+
btn.textContent = item.name;
78+
btn.addEventListener('click', () => {
79+
sendToHA(tab, item.name);
80+
});
81+
contextSelection.appendChild(btn);
82+
});
83+
}
84+
85+
/**
86+
* Main function to send page data to Home Assistant
87+
* @param {object} tab - Tab object
88+
* @param {string} contextName - Name of the context to send with
89+
*/
90+
async function sendToHA(tab, contextName) {
91+
updateStatus('Sending...');
92+
hideButton();
93+
contextSelection.classList.add('hidden');
94+
95+
try {
4496
// Use unified sendToHomeAssistant function with popup-specific callbacks
4597
await ExtensionUtils.sendToHomeAssistant({
4698
tab,
99+
context: contextName,
47100
onProgress: (message) => {
48101
updateStatus(message);
49102
},
@@ -254,7 +307,7 @@ function handleError(error) {
254307
// --- Event Listeners ---
255308

256309
// Initialize popup when page loads
257-
sendToHA();
310+
initPopup();
258311

259312
// OK button closes the popup
260313
okBtn.addEventListener('click', () => {

package/style.css

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,3 +441,38 @@ button:active, .ok-btn:active {
441441
width: 100%;
442442
margin-bottom: 1em;
443443
}
444+
445+
/* Context selection in popup */
446+
.context-selection {
447+
display: flex;
448+
flex-direction: column;
449+
gap: 0.5em;
450+
width: 100%;
451+
max-width: 420px;
452+
margin-top: 1em;
453+
}
454+
.context-btn {
455+
padding: 0.8em 1.6em;
456+
font-size: 1em;
457+
border: none;
458+
border-radius: 10px;
459+
background: linear-gradient(90deg, #00b4fc 0%, #1a8cff 100%);
460+
color: #fff;
461+
font-weight: 700;
462+
cursor: pointer;
463+
box-shadow: 0 2px 12px #00b4fc33;
464+
transition: background 0.18s, color 0.18s, box-shadow 0.18s, transform 0.15s;
465+
letter-spacing: 0.01em;
466+
text-align: center;
467+
}
468+
.context-btn:hover {
469+
background: linear-gradient(90deg, #1a8cff 0%, #00b4fc 100%);
470+
box-shadow: 0 4px 16px #00b4fc55;
471+
transform: translateY(-1px);
472+
}
473+
.context-btn:active {
474+
background: #1a8cff;
475+
color: #eaf6ff;
476+
box-shadow: 0 1px 4px #00b4fc44;
477+
transform: translateY(0);
478+
}

0 commit comments

Comments
 (0)