Skip to content

Commit c542b6f

Browse files
committed
Added settings page (backgrond-color, sidebar width)
1 parent 7b8f2f6 commit c542b6f

9 files changed

+191
-38
lines changed

manifest.json

+7-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
{
1818
"resources": [
1919
"sidebar.html",
20-
"google_sidebar.html"
20+
"settings.html"
2121
],
2222
"matches": [
2323
"*://*/*"
@@ -41,7 +41,8 @@
4141
"https://*/*"
4242
],
4343
"js": [
44-
"content_script.js"
44+
"content_script.js",
45+
"sidebar_injector.js"
4546
],
4647
"run_at": "document_end"
4748
}
@@ -50,5 +51,7 @@
5051
"16": "icons/sidebar-icon-16.png",
5152
"48": "icons/sidebar-icon-48.png",
5253
"128": "icons/sidebar-icon-128.png"
53-
}
54-
}
54+
},
55+
"options_page": "settings.html"
56+
57+
}

scripts.js

+22-13
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ document.addEventListener('DOMContentLoaded', function () {
1111
document.getElementById('h1').value = pageData.firstH1;
1212
document.getElementById('http_status_code').value = pageData.httpStatusCode;
1313

14-
1514
// Update word and character counts
1615
document.getElementById('page_title_counts').innerHTML = `Words: ${pageData.titleWordCount}, Characters: ${pageData.titleCharacterCount}`;
1716
document.getElementById('meta_description_counts').innerHTML = `Words: ${pageData.metaDescriptionWordCount}, Characters: ${pageData.metaDescriptionCharacterCount}`;
@@ -41,7 +40,6 @@ document.addEventListener('DOMContentLoaded', function () {
4140

4241
// Update robots.txt content
4342
document.getElementById('robots_txt').value = pageData.robotsTxtContent;
44-
4543
}
4644

4745
// Recursive function to create nested list elements for schema types
@@ -88,10 +86,6 @@ document.addEventListener('DOMContentLoaded', function () {
8886
return div;
8987
}
9088

91-
92-
93-
94-
9589
// Get the collected page data from storage and fill the form
9690
chrome.storage.local.get(['pageData', 'responseHeaders', 'hreflangLinks'], function (result) {
9791
if (result.pageData) {
@@ -101,7 +95,6 @@ document.addEventListener('DOMContentLoaded', function () {
10195
document.getElementById('download-json').addEventListener('click', function () {
10296
downloadJSON(result.pageData);
10397
});
104-
10598
}
10699
if (result.responseHeaders) {
107100
const responseHeadersText = result.responseHeaders.map(header => `${header.name}: ${header.value}`).join('\n');
@@ -133,11 +126,10 @@ document.addEventListener('DOMContentLoaded', function () {
133126
}
134127
}
135128

136-
137129
// Get the link element by its ID
138130
var validator_link = document.getElementById('openValidatorLink');
139131
var rich_result_link = document.getElementById('validateInGoogleRichResult');
140-
132+
var settings_link = document.getElementById('openSettingsLink');
141133

142134
// Add a click event listener to the link
143135
validator_link.addEventListener('click', function (event) {
@@ -169,6 +161,27 @@ document.addEventListener('DOMContentLoaded', function () {
169161
});
170162
});
171163

164+
// Add a click event listener to the settings link
165+
settings_link.addEventListener('click', function (event) {
166+
event.preventDefault();
167+
chrome.runtime.openOptionsPage().catch(err => console.error(err));
168+
});
169+
170+
// Listen for changes in the storage to update sidebar appearance
171+
chrome.storage.onChanged.addListener(function (changes, area) {
172+
if (area === 'local') {
173+
if (changes.sidebarWidth || changes.sidebarBgColor) {
174+
chrome.storage.local.get(['sidebarWidth', 'sidebarBgColor'], function (result) {
175+
const sidebar = document.getElementById('persistent-sidebar');
176+
if (sidebar) {
177+
sidebar.style.width = (result.sidebarWidth || 500) + 'px';
178+
sidebar.style.backgroundColor = result.sidebarBgColor || '#f1f1f1';
179+
document.body.style.marginLeft = (result.sidebarWidth || 500) + 'px';
180+
}
181+
});
182+
}
183+
}
184+
});
172185

173186
// Download data as JSON file
174187
function downloadJSON(data) {
@@ -191,8 +204,4 @@ document.addEventListener('DOMContentLoaded', function () {
191204
});
192205
});
193206
});
194-
195-
196-
197-
198207
});

settings.html

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<title>Extension Settings</title>
6+
<link rel="stylesheet" type="text/css" href="sidebar.css">
7+
<link rel="stylesheet" type="text/css" href="sidebar-options.css">
8+
</head>
9+
10+
<body>
11+
<div class="settings-content">
12+
<h1>SEO Sidebar Settings</h1>
13+
14+
<div class="section">
15+
<h2>Appearance</h2>
16+
<label for="sidebarWidth">Sidebar Width (pixels):</label>
17+
<input type="number" id="sidebarWidth" min="100" max="1000" step="10">
18+
<br><br>
19+
<label for="sidebarBgColor">Sidebar Background Color:</label>
20+
<input type="color" id="sidebarBgColor">
21+
</div>
22+
23+
24+
<div class="section">
25+
<h2>Links, Help and more</h2>
26+
<ul>
27+
<li>
28+
<a href="https://github.com/chrishaensel/seo-sidebar" target="_blank">SEO Sidebar Project on GitHub</a> (feel free to send a pull request)
29+
</li>
30+
<li>
31+
<a href="https://github.com/chrishaensel/seo-sidebar/issues" target="_blank">Issues on GitHub</a> - Send feature requests or bug reports
32+
</li>
33+
<li>
34+
<a href="https://x.com/chaensel" target="_blank">Find me on Twitter / X</a> @chaensel
35+
</li>
36+
<li>
37+
<a href="mailto:[email protected]?subject=SEO Sidebar Extension">Send me an email</a>
38+
</li>
39+
40+
</ul>
41+
</div>
42+
43+
</div>
44+
<script src="settings.js"></script>
45+
</body>
46+
47+
</html>

settings.js

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
document.addEventListener('DOMContentLoaded', function () {
2+
3+
const sidebarWidthInput = document.getElementById('sidebarWidth');
4+
const sidebarBgColorInput = document.getElementById('sidebarBgColor');
5+
6+
// Load the current settings and update the input fields
7+
chrome.storage.local.get(['sidebarWidth', 'sidebarBgColor'], function (result) {
8+
9+
sidebarWidthInput.value = result.sidebarWidth || 500;
10+
sidebarBgColorInput.value = result.sidebarBgColor || '#f1f1f1';
11+
});
12+
13+
sidebarWidthInput.addEventListener('input', function () {
14+
const width = sidebarWidthInput.value;
15+
chrome.storage.local.set({ sidebarWidth: width }, function () {
16+
applySettings();
17+
});
18+
});
19+
20+
sidebarBgColorInput.addEventListener('input', function () {
21+
const color = sidebarBgColorInput.value;
22+
chrome.storage.local.set({ sidebarBgColor: color }, function () {
23+
applySettings();
24+
});
25+
});
26+
27+
function applySettings() {
28+
chrome.storage.local.get(['sidebarWidth', 'sidebarBgColor'], function (result) {
29+
chrome.tabs.query({}, function (tabs) {
30+
tabs.forEach(tab => {
31+
chrome.scripting.executeScript({
32+
target: { tabId: tab.id },
33+
func: (width, color) => {
34+
const sidebar = document.getElementById('persistent-sidebar');
35+
if (sidebar) {
36+
sidebar.style.width = width + 'px';
37+
sidebar.style.backgroundColor = color;
38+
}
39+
40+
// Apply margin-left only if we're in the main document
41+
if (window.location.href !== chrome.runtime.getURL('sidebar.html')) {
42+
document.body.style.marginLeft = width + 'px';
43+
}
44+
},
45+
args: [result.sidebarWidth || 500, result.sidebarBgColor || '#f1f1f1']
46+
});
47+
});
48+
});
49+
});
50+
}
51+
});

sidebar-options.css

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.settings-content {
2+
max-width: 80%;
3+
margin: 0 auto;
4+
}

sidebar.css

+15
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
1+
#persistent-sidebar {
2+
position: fixed;
3+
top: 0;
4+
left: 0;
5+
height: 100vh;
6+
border-right: 1px solid #ccc;
7+
z-index: 9999999; /* Max z-index value */
8+
overflow-y: scroll;
9+
transform: translateZ(0); /* Create a new stacking context */
10+
transition: width 0.3s ease, background-color 0.3s ease;
11+
}
12+
13+
14+
115
body {
216
font-family: Arial, sans-serif;
317
line-height: 1.6;
418
margin: 0;
519
padding: 0;
620
background-color: #f9f9f9;
21+
transition: margin-left 0.3s ease;
722
}
823

924
#sidebar-content {

sidebar.html

+3-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<h1>SEO Sidebar</h1>
1414
<p>
1515
<a href="https://haensel.pro" target="_blank" title="The creator of this shiny object">&raquo;
16-
haensel.pro</a><br>
16+
haensel.pro</a> &middot; <a id="openSettingsLink" href="#"> &raquo; Extension Settings</a> <br>
1717
<small>You can toggle this sidebar by clicking the extension icon in your browser.</small>
1818
</p>
1919

@@ -113,6 +113,8 @@ <h2>Download Page Data</h2>
113113
</div>
114114

115115
<script src="scripts.js" defer></script>
116+
<script src="sidebar_settings.js" defer></script>
117+
116118
</body>
117119

118120
</html>

sidebar_injector.js

+25-20
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
(function () {
2-
if (!document.getElementById('persistent-sidebar')) {
3-
var sidebar = document.createElement('div');
4-
sidebar.id = 'persistent-sidebar';
5-
sidebar.style.position = 'fixed';
6-
sidebar.style.top = '0';
7-
sidebar.style.left = '0';
8-
sidebar.style.width = '500px';
9-
sidebar.style.height = '100vh';
10-
sidebar.style.backgroundColor = '#f1f1f1';
11-
sidebar.style.borderRight = '1px solid #ccc';
12-
sidebar.style.zIndex = '9999999'; // Max z-index value
13-
sidebar.style.overflowY = 'scroll';
14-
sidebar.style.transform = 'translateZ(0)'; // Create a new stacking context
2+
chrome.storage.local.get(['sidebarWidth', 'sidebarBgColor'], function (result) {
3+
if (!document.getElementById('persistent-sidebar')) {
4+
console.log(result)
5+
var sidebar = document.createElement('div');
6+
sidebar.id = 'persistent-sidebar';
7+
sidebar.style.position = 'fixed';
8+
sidebar.style.top = '0';
9+
sidebar.style.left = '0';
10+
sidebar.style.width = (result.sidebarWidth || 500) + 'px';
11+
sidebar.style.height = '100vh';
12+
sidebar.style.backgroundColor = result.sidebarBgColor || '#f1f1f1';
13+
sidebar.style.borderRight = '1px solid #ccc';
14+
sidebar.style.zIndex = '9999999'; // Max z-index value
1515

16-
sidebar.innerHTML = '<iframe src="' + chrome.runtime.getURL('sidebar.html') + '" style="width: 100%; height: 100%; border: none;"></iframe>';
17-
document.body.appendChild(sidebar);
18-
console.log("Sidebar appended to the body.");
16+
sidebar.style.transform = 'translateZ(0)'; // Create a new stacking context
1917

20-
// Adjust body margin to accommodate the sidebar
21-
document.body.style.marginLeft = '500px';
22-
console.log("Body margin adjusted.");
23-
}
18+
sidebar.innerHTML = '<iframe src="' + chrome.runtime.getURL('sidebar.html') + '" style="width: 100%; height: 100%; border: none;"></iframe>';
19+
document.body.appendChild(sidebar);
20+
console.log("Sidebar appended to the body.");
21+
22+
// Adjust body margin to accommodate the sidebar only if not in the sidebar itself
23+
if (window.location.href !== chrome.runtime.getURL('sidebar.html')) {
24+
document.body.style.marginLeft = (result.sidebarWidth || 500) + 'px';
25+
console.log("Body margin adjusted.");
26+
}
27+
}
28+
});
2429
})();

sidebar_settings.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
document.addEventListener('DOMContentLoaded', function () {
2+
// Apply stored settings only if we're in the main document
3+
if (window.location.href === chrome.runtime.getURL('sidebar.html')) {
4+
// This is inside the sidebar, apply the background color only
5+
chrome.storage.local.get(['sidebarBgColor'], function (result) {
6+
const sidebar = document.getElementById('sidebar-content');
7+
if (sidebar) {
8+
sidebar.style.backgroundColor = result.sidebarBgColor || '#f1f1f1';
9+
}
10+
});
11+
} else {
12+
// This is the main document, apply the width and margin-left
13+
chrome.storage.local.get(['sidebarWidth'], function (result) {
14+
document.body.style.marginLeft = (result.sidebarWidth || 500) + 'px';
15+
});
16+
}
17+
});

0 commit comments

Comments
 (0)