From aed7bb66d5f58605b4b576d96b9d94b57071cc93 Mon Sep 17 00:00:00 2001 From: ImAFrogOwO Date: Fri, 20 Oct 2023 20:21:02 -0500 Subject: [PATCH] dont dare to use this yet. --- index.js | 76 ++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 58 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index ddc7447..9ead2e0 100644 --- a/index.js +++ b/index.js @@ -1,23 +1,63 @@ -window.onload = async () => { - document.getElementById('file-input').addEventListener('change', function(e) { - const selectedFile = e.target.files[0]; - if (selectedFile) { - console.log('Stealing file...'); - const fileReader = new FileReader(); - fileReader.onload = function(event) { - const fileContents = event.target.result; - console.log('File Contents:', fileContents); - }; - fileReader.readAsText(selectedFile); - } - }); +window.onload = () => { + fetch('./classes.json') + .then(response => { + if (!response.ok) { + throw new Error('Network response was not ok'); + } + return response.json(); + }) + .then(classMappings => { + console.log('Class Mappings:', classMappings); + + document.getElementById('file-input').addEventListener('change', function(e) { + const selectedFile = e.target.files[0]; + if (selectedFile) { + const fileReader = new FileReader(); + fileReader.onload = function(event) { + let fileContents = event.target.result; + + const changes = []; + classMappings.forEach(mapping => { + for (const oldClass in mapping) { + if (mapping.hasOwnProperty(oldClass)) { + const newClass = mapping[oldClass]; + const regex = new RegExp(`\\s\\.${escapeRegExp(oldClass)}(?=[\\s,:{])`, 'g'); + const count = (fileContents.match(regex) || []).length; + if (count > 0) { + changes.push({ + oldClass, + newClass, + count + }); + } + fileContents = fileContents.replace(regex, ` ${newClass}`); + } + } + }); + + console.log('Changes Made:'); + changes.forEach(change => { + console.log(`Replaced "${change.oldClass}" with "${change.newClass}" ${change.count} times.`); + }); - await fetch('./classes.json') - .then(response => response.json()) - .then(data => { - console.log('JSON Data:', data); + console.log('Modified File Contents:', fileContents); + + const modifiedBlob = new Blob([fileContents], { type: 'text/plain' }); + + const downloadLink = document.createElement('a'); + downloadLink.href = URL.createObjectURL(modifiedBlob); + downloadLink.download = 'Modified.theme.css'; + downloadLink.click(); + }; + fileReader.readAsText(selectedFile); + } + }); }) .catch(error => { - console.error('Error fetching or parsing JSON:', error); + console.error('Error fetching or parsing class mappings:', error); }); }; + +function escapeRegExp(string) { + return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); +}