Refactor duplicated code into shared utilities module #908
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The codebase contained significant code duplication across multiple JavaScript files (
script.js,vibe/script.js, andembed-script.js), with several functions implemented independently multiple times:This duplication made maintenance harder, increased the risk of inconsistencies, and added unnecessary code bloat.
Solution
Created a new
shared-utils.jsmodule containing commonly-used utilities and refactored the existing scripts to use these shared functions:Extracted Functions
toggleDarkMode()- Unified dark mode toggle with defensive null checks for missing DOM elementsparseCSV(csv)- Consolidated CSV parser that handles both "act" and "app" headers, with consistent boolean conversion for thefor_devsfieldnormalizeText(text)- Reusable text normalization utility that removes extra whitespace and newlines, with null/undefined safetyscrollToPromptCard(targetCard, isMobile, headerHeight)- Extracted scroll and highlight animation logic with:.main-contentcontainer doesn't existfindPromptCardByTitle(title)- Fuzzy title matching utility that usesnormalizeText()for consistent card lookupsImplementation Details
Updated
_layouts/default.htmlto loadshared-utils.jsbefore the main scripts, ensuring utilities are available when needed. Added conditional loading forvibe/script.jsonly whenbody_class == 'vibe'.Results
Before/After Comparison
script.jsvibe/script.jsshared-utils.jsTesting
Notes
embed-script.jswas intentionally kept separate as it uses different localStorage keys (darkModevsdark-mode)updatePromptCount()was not extracted as the implementations differ significantly between pagesOriginal prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.