-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
115 lines (102 loc) · 3.53 KB
/
content.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
(() => {
window.__narrowerLoaded = true;
// Skip execution on restricted browser pages
const isRestrictedUrl =
window.location.protocol === "chrome:" ||
window.location.protocol === "edge:" ||
window.location.protocol === "chrome-extension:" ||
window.location.protocol === "about:" ||
window.location.protocol === "data:";
if (isRestrictedUrl) return;
const style = document.createElement("style");
style.id = "skew-styles";
document.documentElement.appendChild(style);
// Generate CSS classes for different width percentages
const styles = Array.from(
{ length: 11 },
(_, i) =>
`.skew-${i * 10} > * { max-width: ${
100 - i * 10
}% !important; margin-inline: auto !important; }`
).join("\n");
style.textContent = styles;
let currentSkew = 0;
let lastSkewValue = 0;
let isEnabled = true;
const updateSkew = (value, updateStorage = true) => {
currentSkew = Math.max(0, Math.min(100, value));
document.documentElement.className = currentSkew > 0 ? `skew-${currentSkew}` : "";
if (updateStorage) {
chrome.storage.sync.set({ globalSkew: currentSkew });
}
};
const toggleNarrower = async () => {
const hostname = window.location.hostname;
const { disabledSites } = await chrome.storage.sync.get({ disabledSites: [] });
const sites = [...disabledSites];
const index = sites.indexOf(hostname);
if (isEnabled) {
if (index === -1) {
sites.push(hostname);
}
lastSkewValue = currentSkew;
updateSkew(0, false);
} else {
if (index !== -1) {
sites.splice(index, 1);
}
updateSkew(lastSkewValue || currentSkew);
}
isEnabled = !isEnabled;
await chrome.storage.sync.set({ disabledSites: sites });
return { success: true, enabled: isEnabled };
};
try {
chrome.storage.sync.get(
{
globalSkew: 0,
disabledSites: [],
},
(settings) => {
const hostname = window.location.hostname;
isEnabled = !settings.disabledSites.includes(hostname);
if (isEnabled && settings.globalSkew > 0) {
const value = Math.round(settings.globalSkew / 10) * 10;
lastSkewValue = value;
updateSkew(value);
}
}
);
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
try {
if (request.action === "updateSkew") {
const value = Math.round(request.value / 10) * 10;
lastSkewValue = value;
updateSkew(value);
sendResponse({ success: true, value: currentSkew });
} else if (request.action === "increase-width") {
if (isEnabled && currentSkew > 0) {
updateSkew(currentSkew - 10);
sendResponse({ success: true, value: currentSkew });
}
} else if (request.action === "decrease-width") {
if (isEnabled && currentSkew < 100) {
updateSkew(currentSkew + 10);
sendResponse({ success: true, value: currentSkew });
}
} else if (request.action === "toggle-narrower") {
toggleNarrower().then(sendResponse);
return true;
}
return true; // Keep the message channel open for async response
} catch (error) {
console.error('Error handling message:', error);
sendResponse({ success: false, error: error.message });
return true;
}
});
chrome.runtime.sendMessage({ action: "contentScriptReady" });
} catch (error) {
console.debug("Skew: Running in restricted context");
}
})();