Skip to content

Commit

Permalink
Add bare bones dev options, isolate context, and add preload.js for e…
Browse files Browse the repository at this point in the history
…xposing specific APIs
  • Loading branch information
vorlie committed Jan 19, 2025
1 parent 266466a commit a36fd65
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 10 deletions.
171 changes: 171 additions & 0 deletions developer-options.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Iota's Notepad - Developer Options</title>
<style>
h1{
margin: 0 0 20px;

}
body {
background-color: #1a1a1a;
color: white;
font-family: Arial, sans-serif;
padding: 5px;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
th, td {
border: 1px solid #333;
padding: 8px;
text-align: left;
}
th {
background-color: #333;
}
tr:nth-child(even) {
background-color: #2a2a2a;
}
button {
background-color: #444;
color: white;
border: none;
padding: 6px 12px;
margin: 2px 0;
cursor: pointer;
}
button:hover {
background-color: #555;
}
#modal {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: #222;
color: white;
padding: 20px;
border: 1px solid #555;
box-shadow: 0 0 10px black;
z-index: 1000;
max-width: 80%;
max-height: 80%;
overflow: auto;
}
#modal h2 {
margin: 0 0 10px;
}
#modal pre {
white-space: pre-wrap; /* Handle line breaks and long text */
word-wrap: break-word; /* Break long words */
}
</style>
</head>
<body>
<h1>Developer Options</h1>
<button onclick="refreshStorage()">Refresh Local Storage</button>
<div id="dev-tools">
<table id="storage-table">
<thead>
<tr>
<th>Key</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</div>

<!-- Modal for displaying key-value -->
<div id="modal">
<h2>Key Details</h2>
<p id="modal-key"><strong>Key:</strong> </p>
<pre id="modal-value"></pre>
<button onclick="closeModal()">Close</button>
</div>

<script>

// Function to refresh the storage view
function refreshStorage() {
console.log("Refreshing local storage...");

const tableBody = document.querySelector("#storage-table tbody");
tableBody.innerHTML = ""; // Clear previous rows

for (let i = 0; i < localStorage.length; i++) {
const key = localStorage.key(i);
const value = localStorage.getItem(key);

const row = document.createElement("tr");
row.innerHTML = `
<td>${key}</td>
<td>
<button onclick="viewKey('${key}')">View</button>
<button onclick="deleteKey('${key}')">Delete</button>
</td>
`;
tableBody.appendChild(row);
}
}

// Function to view key-value in a modal
function viewKey(key) {
console.log(`Viewing key: ${key}`);

const value = localStorage.getItem(key);
const modal = document.getElementById("modal");
const overlay = document.getElementById("overlay");

document.getElementById("modal-key").innerHTML = `<strong>Key:</strong> ${key}`;

// Process the value for JSON or format \n
try {
// Try parsing JSON and pretty-print
console.log("Attempting to parse JSON value...");
document.getElementById("modal-value").textContent = JSON.stringify(JSON.parse(value), null, 2);
} catch {
// Replace \n with actual line breaks for non-JSON
console.log("Value is not JSON, replacing line breaks...");
document.getElementById("modal-value").innerHTML = value.replace(/\n/g, "<br>");
}

modal.style.display = "block";
overlay.style.display = "block";
}

// Function to close the modal
function closeModal() {
console.log("Closing modal...");
document.getElementById("modal").style.display = "none";
}

// Function to delete a key from local storage
function deleteKey(key) {
console.log(`Deleting key: ${key}`);
localStorage.removeItem(key);
refreshStorage();
}

// Handle Escape key to close modal
document.addEventListener("keydown", (e) => {
if (e.key === "Escape") {
console.log("Escape key pressed, closing modal...");
closeModal();
}
});

// Initial load
console.log("Loading initial local storage...");
refreshStorage();
</script>
</body>
</html>

3 changes: 2 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ <h2 class="modal-h2">Settings</h2>
<h2 class="modal-h2">About</h2>
<p id="appVersion">Version: </p>
<p id="electronVersion">Electron Version: </p>
<button class="update-button" onclick="checkForUpdates()">Check for Updates</button>
<button class="about-button" onclick="checkForUpdates()">Check for Updates</button>
<button class="about-button" id="open-dev-options">Developer Options</button>
</div>
</div>
</div>
Expand Down
42 changes: 37 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
const { app, BrowserWindow, Menu } = require('electron');
const { app, BrowserWindow, Menu, ipcMain } = require('electron');

let mainWindow;
let devWindow;

app.on('ready', () => {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: true,
nodeIntegration: false,
preload: __dirname + '/preload.js',
},
menuBarVisible: false,
titleBarStyle: 'hidden',
...(process.platform !== 'darwin' ? { titleBarOverlay: true } : {})
});

mainWindow.setTitleBarOverlay({
color: 'rgba(0, 0, 0, 0)', // 49, 50, 68
symbolColor: 'rgba(205, 214, 244, 1)', // 205, 214, 244
color: 'rgba(0, 0, 0, 0)', // Transparent background
symbolColor: 'rgba(205, 214, 244, 1)', // Symbol color
height: 48
});
mainWindow.setMinimumSize(950, 600);
Expand All @@ -25,10 +28,39 @@ app.on('ready', () => {

mainWindow.loadFile('index.html');

mainWindow.on('closed', () => {
app.quit();
});

ipcMain.on('open-dev-window', () => {
createDevWindow();
});
});

app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
if (process.platform !== 'darwin') app.quit();
});

if (require('electron-squirrel-startup')) app.quit();

function createDevWindow() {
if (devWindow) {
devWindow.focus();
return;
}

devWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
},
title: "Iota's Notepad - Developer Options"
});

devWindow.loadFile('developer-options.html');

devWindow.on('closed', () => {
devWindow = null;
});
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "iotas-notepad",
"version": "1.1.3",
"version": "1.1.4",
"main": "index.js",
"scripts": {
"electron": "electron .",
Expand Down
7 changes: 7 additions & 0 deletions preload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { contextBridge, ipcRenderer } = require('electron');

// Expose only specific APIs to the renderer
contextBridge.exposeInMainWorld('electron', {
openDevWindow: () => ipcRenderer.send('open-dev-window'),
});

4 changes: 2 additions & 2 deletions src/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ body {
margin-bottom: 10px;
}

.settings-button, .update-button{
.settings-button, .about-button{
background-color: var(--button-bg-color);
color: var(--button-color);
border: 1px solid var(--button-border-color);
Expand All @@ -528,7 +528,7 @@ body {
width: 45%; /* Set fixed width for sections */
}

.button:hover, .settings-button:hover, .update-button:hover {
.button:hover, .settings-button:hover, .about-button:hover {
background-color: var(--button-bg-color-hover);
}

Expand Down
6 changes: 5 additions & 1 deletion src/script.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
let saveTimeout;

const version = "1.1.3";
const version = "1.1.4";
const electronVersion = "33.2.1";

const defaultThemes = {
Expand Down Expand Up @@ -60,6 +60,10 @@ const defaultThemes = {
}
};

document.getElementById('open-dev-options').addEventListener('click', () => {
window.electron.openDevWindow();
});

document.addEventListener("DOMContentLoaded", () => {
loadNotes();
displayReleases();
Expand Down

0 comments on commit a36fd65

Please sign in to comment.