-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
188 lines (159 loc) · 5.65 KB
/
popup.js
File metadata and controls
188 lines (159 loc) · 5.65 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* Initialize the popup UI and attach event listeners
*/
const initPopup = () => {
document.querySelectorAll(".breakpoint-btn").forEach((button) => {
button.addEventListener("click", async () => {
try {
const width = parseInt(button.dataset.width);
const prefix = button.querySelector(".prefix").textContent;
const device = button.dataset.device;
// Get the active tab
const [tab] = await chrome.tabs.query({
active: true,
currentWindow: true,
});
if (!tab) {
console.error("No active tab found");
return;
}
// Execute the breakpoint viewer in the active tab
await chrome.scripting.executeScript({
target: { tabId: tab.id },
func: applyBreakpoint,
args: [{ width, prefix, device }],
});
// Close the popup
window.close();
} catch (error) {
console.error("Failed to apply breakpoint:", error);
showError(error.message);
}
});
});
};
/**
* Show an error message in the popup
* @param {string} message - The error message to display
*/
const showError = (message) => {
const errorElement = document.createElement("div");
errorElement.className = "error";
errorElement.textContent = `Error: ${message}`;
const container = document.querySelector(".container");
container.appendChild(errorElement);
// Remove the error after 3 seconds
setTimeout(() => {
errorElement.remove();
}, 3000);
};
/**
* Apply a breakpoint to the active tab
* @param {Object} breakpoint - The breakpoint to apply
* @param {number} breakpoint.width - The width of the breakpoint
* @param {string} breakpoint.prefix - The prefix of the breakpoint
* @param {string} breakpoint.device - The device name of the breakpoint
*/
function applyBreakpoint({ width, prefix, device }) {
// Helper to preserve scripts and iframes
const preserveElements = (parent) => {
const scripts = Array.from(parent.getElementsByTagName("script"));
const iframes = Array.from(parent.getElementsByTagName("iframe"));
return [...scripts, ...iframes].map((el) => el.cloneNode(true));
};
// Helper to restore preserved elements
const restoreElements = (elements, parent) => {
elements.forEach((el) => parent.appendChild(el));
};
// Clean up existing elements
const existingWrapper = document.querySelector(".tw-breakpoint-wrapper");
const existingContainer = document.querySelector(".tw-breakpoint-container");
const existingIndicator = document.querySelector(".tw-breakpoint-indicator");
const existingOverlay = document.querySelector(".tw-loading-overlay");
if (existingContainer) {
existingContainer.remove();
}
existingIndicator?.remove();
existingOverlay?.remove();
// Show loading overlay
const overlay = document.createElement("div");
overlay.className = "tw-loading-overlay";
document.body.appendChild(overlay);
// Create outer container that centers the content
const container = document.createElement("div");
container.className = "tw-breakpoint-container";
// Create inner wrapper for the content
const wrapper = document.createElement("div");
wrapper.className = "tw-breakpoint-wrapper";
wrapper.style.maxWidth = `${width}px`;
// Store original styles
const originalStyles = {
overflow: document.body.style.overflow,
background: document.body.style.background,
margin: document.body.style.margin,
padding: document.body.style.padding,
height: document.body.style.height,
width: document.body.style.width,
};
// Apply styles to body
Object.assign(document.body.style, {
overflow: "hidden",
margin: "0",
padding: "0",
height: "100%",
width: "100%",
});
// Set CSS variable for background color
document.documentElement.style.setProperty("--tw-bg-color", "#f1f5f9");
// Preserve important elements
const preservedElements = preserveElements(document.body);
// Move all content except our elements into the wrapper
Array.from(document.body.children).forEach((child) => {
if (
!child.classList.contains("tw-breakpoint-container") &&
!child.classList.contains("tw-breakpoint-indicator") &&
!child.classList.contains("tw-loading-overlay")
) {
wrapper.appendChild(child);
}
});
// Restore preserved elements
restoreElements(preservedElements, wrapper);
// Assemble the structure
container.appendChild(wrapper);
document.body.appendChild(container);
// Add breakpoint indicator
const indicator = document.createElement("div");
indicator.className = "tw-breakpoint-indicator";
// Add size info
const sizeInfo = document.createElement("span");
sizeInfo.textContent = `${prefix}: ${width}px • ${device}`;
indicator.appendChild(sizeInfo);
// Add controls
const controls = document.createElement("div");
controls.className = "tw-controls";
// Add reset button
const resetButton = document.createElement("button");
resetButton.className = "tw-control-button";
resetButton.textContent = "✕";
resetButton.title = "Reset view";
resetButton.addEventListener("click", () => {
// Restore original styles
Object.entries(originalStyles).forEach(([prop, value]) => {
document.body.style[prop] = value;
});
// Remove our elements
container.remove();
indicator.remove();
overlay.remove();
});
controls.appendChild(resetButton);
indicator.appendChild(controls);
document.body.appendChild(indicator);
// Remove loading overlay after a short delay
setTimeout(() => {
overlay.remove();
}, 500);
}
// Initialize the popup when the DOM is loaded
document.addEventListener("DOMContentLoaded", initPopup);