Skip to content

Commit

Permalink
fix: optimized sub-optimal code
Browse files Browse the repository at this point in the history
  • Loading branch information
FahadBinHussain committed Dec 22, 2023
1 parent 46c75c9 commit 03020a0
Show file tree
Hide file tree
Showing 5 changed files with 332 additions and 207 deletions.
12 changes: 11 additions & 1 deletion src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
defaultAlgorithm,
} from "./utils.js";

// Listener for when the extension is installed
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.sync.set({
highlightSheet: `font-weight: ${defaultHighlightSheet};`,
Expand All @@ -16,12 +17,17 @@ chrome.runtime.onInstalled.addListener(() => {
});
});

// Listener for when a tab is updated
chrome.tabs.onUpdated.addListener((tabId, changeInfo) => {
if (changeInfo.status == "complete") {
// Get the autoApply and excludedPatterns data from storage
chrome.storage.sync.get(["autoApply", "excludedPatterns"], async (data) => {
if (data.autoApply) {
// Get the tab details
let tab = await chrome.tabs.get(tabId);
// Check if the tab URL is not in the excluded patterns
if (!patternsInclude(data.excludedPatterns, tab.url)) {
// Execute the notation function in the tab
chrome.scripting.executeScript({
target: { tabId: tabId },
function: notation,
Expand All @@ -32,8 +38,10 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo) => {
}
});

// Listener for when a command is triggered
chrome.commands.onCommand.addListener(async (command) => {
if (command === "toggle-auto-notation") {
// Toggle the autoApply value in storage and update the command status
chrome.storage.sync.get(["autoApply"], (data) => {
let autoApply = !data.autoApply;
chrome.storage.sync.set({ autoApply: autoApply }, () => {
Expand All @@ -45,11 +53,13 @@ chrome.commands.onCommand.addListener(async (command) => {
});
}
if (command === "toggle-notation") {
// Get the active tab
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });

// Execute the notation function in the tab
chrome.scripting.executeScript({
target: { tabId: tab.id },
function: notation,
});
}
});
});
55 changes: 30 additions & 25 deletions src/popup/popup.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="style.css">
</head>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="style.css">
</head>

<body class="text-bg-dark" style="width: 200px;">
<body class="text-bg-dark" style="width: 200px;">
<div class="container">

<div class="card text-bg-dark pb-0">
<img src="..\icons\marquee.png" class="card-img-top" alt="Notation">
Expand Down Expand Up @@ -48,27 +49,31 @@ <h3 class="text-center text-info small" id="v2">90%</h3>
</div>
</div>

<div class="container">
<div class="row pt-2">
<div class="text-center pb-1">
<div class="row pt-2">
<div class="text-center pb-1">

<!-- SVG Anchor content here -->
<a href="https://github.com/numanzamandipuu/Notation" target="_blank">

<a href="https://github.com/numanzamandipuu/Notation" target="_blank">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 496 512" width="30" height="30" fill="currentColor" >
<path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"/></svg>
</a>
<!-- SVG code here -->
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 496 512" width="30" height="30" fill="currentColor" >
<path d="M165.9 397.4c0 2-2.3 3.6-5.2 3.6-3.3.3-5.6-1.3-5.6-3.6 0-2 2.3-3.6 5.2-3.6 3-.3 5.6 1.3 5.6 3.6zm-31.1-4.5c-.7 2 1.3 4.3 4.3 4.9 2.6 1 5.6 0 6.2-2s-1.3-4.3-4.3-5.2c-2.6-.7-5.5.3-6.2 2.3zm44.2-1.7c-2.9.7-4.9 2.6-4.6 4.9.3 2 2.9 3.3 5.9 2.6 2.9-.7 4.9-2.6 4.6-4.6-.3-1.9-3-3.2-5.9-2.9zM244.8 8C106.1 8 0 113.3 0 252c0 110.9 69.8 205.8 169.5 239.2 12.8 2.3 17.3-5.6 17.3-12.1 0-6.2-.3-40.4-.3-61.4 0 0-70 15-84.7-29.8 0 0-11.4-29.1-27.8-36.6 0 0-22.9-15.7 1.6-15.4 0 0 24.9 2 38.6 25.8 21.9 38.6 58.6 27.5 72.9 20.9 2.3-16 8.8-27.1 16-33.7-55.9-6.2-112.3-14.3-112.3-110.5 0-27.5 7.6-41.3 23.6-58.9-2.6-6.5-11.1-33.3 2.6-67.9 20.9-6.5 69 27 69 27 20-5.6 41.5-8.5 62.8-8.5s42.8 2.9 62.8 8.5c0 0 48.1-33.6 69-27 13.7 34.7 5.2 61.4 2.6 67.9 16 17.7 25.8 31.5 25.8 58.9 0 96.5-58.9 104.2-114.8 110.5 9.2 7.9 17 22.9 17 46.4 0 33.7-.3 75.4-.3 83.6 0 6.5 4.6 14.4 17.3 12.1C428.2 457.8 496 362.9 496 252 496 113.3 383.5 8 244.8 8zM97.2 352.9c-1.3 1-1 3.3.7 5.2 1.6 1.6 3.9 2.3 5.2 1 1.3-1 1-3.3-.7-5.2-1.6-1.6-3.9-2.3-5.2-1zm-10.8-8.1c-.7 1.3.3 2.9 2.3 3.9 1.6 1 3.6.7 4.3-.7.7-1.3-.3-2.9-2.3-3.9-2-.6-3.6-.3-4.3.7zm32.4 35.6c-1.6 1.3-1 4.3 1.3 6.2 2.3 2.3 5.2 2.6 6.5 1 1.3-1.3.7-4.3-1.3-6.2-2.2-2.3-5.2-2.6-6.5-1zm-11.4-14.7c-1.6 1-1.6 3.6 0 5.9 1.6 2.3 4.3 3.3 5.6 2.3 1.6-1.3 1.6-3.9 0-6.2-1.4-2.3-4-3.3-5.6-2z"/></svg>
</a>

</div>
</div>
</div>
</div>

<div class="row">
<div class="text-center pb-1" style="font-size: 11px; opacity: 0.5;">
&#169; 2023 Notation <br> All Rights Reserved
</div>
<!-- Footer content -->
<footer class="row">
<div class="text-center pb-1" style="font-size: 11px; opacity: 0.5;">
&#169; 2023 Notation <br> All Rights Reserved
</div>
</footer>

</div>

</div>

<script src="popup.js" type="module"></script>
<script src="bootstrap.bundle.min.js" type="module"></script>
</body>
<script src="popup.js" type="module"></script>
<script src="bootstrap.bundle.min.js" type="module"></script>
</body>
</html>
174 changes: 95 additions & 79 deletions src/popup/popup.js
Original file line number Diff line number Diff line change
@@ -1,114 +1,130 @@
import {
notation,
defaultHighlightSheet,
defaultRestSheet,
} from "../utils.js";

let applyButton = document.getElementById("applyButton");
let autoButton = document.getElementById("autoButton");
let restoreButton = document.getElementById("restore-button");
let highlightSheetInput = document.getElementById("highlight-input");
let restSheetInput = document.getElementById("rest-input");

var buttonEnabledClass = "btn btn-light btn-sm";
var buttonDisabledClass = "btn btn-danger btn-sm";
let output1 = document.getElementById("v1");
let output2 = document.getElementById("v2");

restSheetInput.value = defaultRestSheet;
output1.innerHTML = highlightSheetInput.value;
output2.innerHTML = restSheetInput.value + "%";
import { notation, defaultHighlightSheet, defaultRestSheet } from "../utils.js";

const applyButton = document.getElementById("applyButton");
const autoButton = document.getElementById("autoButton");
const restoreButton = document.getElementById("restore-button");
const highlightSheetInput = document.getElementById("highlight-input");
const restSheetInput = document.getElementById("rest-input");

const buttonEnabledClass = "btn btn-light btn-sm";
const buttonDisabledClass = "btn btn-danger btn-sm";
const output1 = document.getElementById("v1");
const output2 = document.getElementById("v2");

// Initialize input values and output displays
function initValues() {
highlightSheetInput.value = defaultHighlightSheet;
restSheetInput.value = defaultRestSheet;
output1.innerHTML = defaultHighlightSheet;
output2.innerHTML = `${defaultRestSheet}%`;
}

initValues();

// Update class helper function
function setClass(element, cls) {
element.className = cls;
}

// Update the text and class of the auto apply button
function updateAutoApplyText(isAuto) {
if (isAuto) {
autoButton.innerHTML = "Disable Auto Apply";
setClass(autoButton, buttonDisabledClass);
} else {
autoButton.innerHTML = "Enable Auto Apply";
setClass(autoButton, buttonEnabledClass);
}
autoButton.innerHTML = isAuto ? "Disable Auto Apply" : "Enable Auto Apply";
setClass(autoButton, isAuto ? buttonDisabledClass : buttonEnabledClass);
}

chrome.storage.sync.get(
["highlightSheet", "restSheet", "autoApply", "isOn", "algorithm"],
(data) => {
highlightSheetInput.value = data.highlightSheet.split(":")[1].trim();
restSheetInput.value = data.restSheet.split(":")[1].trim();
output1.innerHTML = fontWeightValue;
output2.innerHTML = opacityValue + "%";
updateAutoApplyText(data.autoApply);
}
);

chrome.storage.sync.get("highlightSheet", function(data) {
let fontWeightValue = parseInt(data.highlightSheet.match(/\d+/)[0]);
document.getElementById("highlight-input").value = fontWeightValue;
output1.innerHTML = fontWeightValue;
});
// Helper to extract style values from a stored string
function extractStyleValue(styleString, regex) {
const match = styleString.match(regex);
return match ? parseInt(match[1], 10) : null;
}

chrome.storage.sync.get("restSheet", function(data) {
let opacityValue = parseInt(data.restSheet.match(/opacity:\s*(\d+(?:\.\d+)?)/i)[1] * 100);
document.getElementById("rest-input").value = opacityValue;
output2.innerHTML = opacityValue + "%";
});
// Load the stored values and update the display
async function loadStoredValues() {
const { highlightSheet, restSheet, autoApply } = await chrome.storage.sync.get(["highlightSheet", "restSheet", "autoApply"]);
const fontWeightValue = extractStyleValue(highlightSheet, /font-weight:\s*(\d+);/);
const opacityValue = extractStyleValue(restSheet, /opacity:\s*(\d+(?:\.\d+)?);/) * 100;

highlightSheetInput.value = fontWeightValue || defaultHighlightSheet;
restSheetInput.value = opacityValue || defaultRestSheet;
output1.innerHTML = fontWeightValue || defaultHighlightSheet;
output2.innerHTML = `${opacityValue || defaultRestSheet}%`;
updateAutoApplyText(autoApply);
}

highlightSheetInput.addEventListener("input", async (text) => {
onHighlightInputChange();
});
loadStoredValues();

restSheetInput.addEventListener("input", async (text) => {
onRestInputChange();
// Update storage with new style value
async function setStyleValue(styleKey, inputElement, outputElement, transformFunction = value => value) {
const value = inputElement.value;
const styleValue = transformFunction(value);
await chrome.storage.sync.set({ [styleKey]: styleValue });
outputElement.textContent = value;
}

// Event listeners for input changes
highlightSheetInput.addEventListener("input", () => {
setStyleValue('highlightSheet', highlightSheetInput, output1, fontWeightValue => `font-weight: ${fontWeightValue};`);
});

restSheetInput.addEventListener("input", () => {
setStyleValue('restSheet', restSheetInput, output2, opacityValue => `opacity: ${opacityValue / 100};`);
});

// Restore button functionality
restoreButton.addEventListener("click", async () => {
chrome.storage.sync.set({
await chrome.storage.sync.set({
highlightSheet: `font-weight: ${defaultHighlightSheet};`,
restSheet: `opacity: ${defaultRestSheet / 100};`,
});
highlightSheetInput.value = defaultHighlightSheet;
restSheetInput.value = defaultRestSheet;
output1.innerHTML = highlightSheetInput.value;
output2.innerHTML = restSheetInput.value + "%";
initValues();
});

// Apply the custom styles by invoking the notation function
applyButton.addEventListener("click", async () => {
let [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });

chrome.scripting.executeScript({
await chrome.scripting.executeScript({
target: { tabId: tab.id },
function: notation,
});
chrome.storage.sync.get(["isOn"], (data) => {
chrome.storage.sync.set({ isOn: !data.isOn });
func: notation,
});

const { isOn } = await chrome.storage.sync.get("isOn");
await chrome.storage.sync.set({ isOn: !isOn });
});

// Toggle the auto-apply functionality and update button text and class
autoButton.addEventListener("click", async () => {
chrome.storage.sync.get(["autoApply"], (data) => {
updateAutoApplyText(!data.autoApply);
chrome.storage.sync.set({ autoApply: !data.autoApply });
});
const { autoApply } = await chrome.storage.sync.get("autoApply");
const newAutoApplyValue = !autoApply;
await chrome.storage.sync.set({ autoApply: newAutoApplyValue });
updateAutoApplyText(newAutoApplyValue);
});

chrome.storage.sync.get("autoApply", (data) => {
updateAutoApplyText(data.autoApply);
});
// Initialize the auto-apply button text and class on load
async function initAutoApply() {
const { autoApply } = await chrome.storage.sync.get("autoApply");
updateAutoApplyText(autoApply);
}

initAutoApply();

// Update the highlight style value when the input changes
function onHighlightInputChange() {
let fontWeightValue = highlightSheetInput.value;
let highlightSheetValue = `font-weight: ${fontWeightValue};`;
chrome.storage.sync.set({ highlightSheet: highlightSheetValue });
output1.innerHTML = fontWeightValue;
setStyleValue(
'highlightSheet',
highlightSheetInput,
output1,
fontWeightValue => `font-weight: ${fontWeightValue};`
);
}

// Update the rest style value when the input changes
function onRestInputChange() {
let opacityValue = restSheetInput.value;
let restSheetValue = `opacity: ${opacityValue / 100};`;
chrome.storage.sync.set({ restSheet: restSheetValue});
output2.innerHTML = opacityValue + "%";
setStyleValue(
'restSheet',
restSheetInput,
output2,
opacityValue => `opacity: ${opacityValue / 100};`,
value => `${value}%`
);
}
1 change: 1 addition & 0 deletions src/popup/styles.css
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* Set the margin and font-family for the body element */
body {
margin: 0px;
font-family: Helvetica, Inter, -apple-system, BlinkMacSystemFont, "Roboto",
Expand Down
Loading

0 comments on commit 03020a0

Please sign in to comment.