Skip to content

Instantly share code, notes, and snippets.

@cb109
Last active March 24, 2026 08:34
Show Gist options
  • Select an option

  • Save cb109/3535b87eb616493ae3c8925fd80fc92c to your computer and use it in GitHub Desktop.

Select an option

Save cb109/3535b87eb616493ae3c8925fd80fc92c to your computer and use it in GitHub Desktop.
chrome extension to auto-increment cache busting query parameter in url
/**
* Installation: chrome://extensions -> Load unpacked -> Select folder
*
* Bind e.g. Ctrl+Shift+R as a shortcut for "cachebust-refresh" in
* chrome://extensions/shortcuts (e.g. Ctrl+Shift+R)
*/
async function bust(tab) {
if (!tab.url) return;
const MODE_EPOCH = "epoch";
const MODE_INCREMENT = "increment";
const { mode } = await chrome.storage.sync.get({ mode: MODE_EPOCH });
const url = new URL(tab.url);
const param = "_cacheBust";
if (mode === MODE_EPOCH) {
url.searchParams.set(param, String(Date.now()));
}
else if (mode === MODE_INCREMENT) {
const current = url.searchParams.get(param);
if (current === null) {
url.searchParams.set(param, "1");
} else {
const next = parseInt(current, 10) + 1;
url.searchParams.set(param, String(next));
}
}
chrome.tabs.update(tab.id, { url: url.toString() });
}
chrome.commands.onCommand.addListener(async (command) => {
if (command === "cachebust-refresh") {
const [tab] = await chrome.tabs.query({
active: true,
currentWindow: true,
});
if (tab) bust(tab);
}
});
{
"manifest_version": 3,
"name": "CacheBust Incrementer",
"version": "1.1",
"description": "Increment ?_cacheBust on the current URL.",
"permissions": ["tabs", "storage"],
"action": {
"default_title": "Increment CacheBust",
"default_popup": "popup.html"
},
"background": {
"service_worker": "background.js"
},
"commands": {
"cachebust-refresh": {
"suggested_key": {
"default": "Ctrl+Shift+R"
},
"description": "Increment _cacheBust and reload"
}
}
}
<!DOCTYPE html>
<html>
<head>
<style>
body {
width: 220px;
padding: 12px;
font-family: system-ui, sans-serif;
font-size: 14px;
}
h3 { margin: 0 0 8px; }
label {
display: block;
margin: 4px 0;
cursor: pointer;
}
</style>
</head>
<body>
<h3>CacheBust Mode</h3>
<label><input type="radio" name="mode" value="epoch"> Current epoch time</label>
<label><input type="radio" name="mode" value="increment"> Incrementing number</label>
<script src="popup.js"></script>
</body>
</html>
const radios = document.querySelectorAll('input[name="mode"]');
chrome.storage.sync.get({ mode: "epoch" }, (data) => {
document.querySelector(`input[value="${data.mode}"]`).checked = true;
});
radios.forEach((r) =>
r.addEventListener("change", () => {
chrome.storage.sync.set({ mode: r.value });
})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment