Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rajucs/b32ae162307d147fab2bc522c51f74c0 to your computer and use it in GitHub Desktop.
Save rajucs/b32ae162307d147fab2bc522c51f74c0 to your computer and use it in GitHub Desktop.
window.onload = function () {
function getCookieValue(name) {
const cookies = document.cookie.split('; ');
for (const cookie of cookies) {
const [cookieName, cookieValue] = cookie.split('=');
if (cookieName === name) {
return cookieValue;
}
}
return null;
}
const existingCookie = getCookieValue('bbQueryPSet');
if (existingCookie) {
// The cookie already exists, so update it with new values
const urlSearchParams = new URLSearchParams(window.location.search);
const paramsObject = Object.fromEntries(urlSearchParams.entries());
const cookieValue = JSON.stringify(paramsObject);
document.cookie = `bbQueryPSet=${cookieValue}`;
} else {
// The cookie does not exist, so create it
const urlSearchParams = new URLSearchParams(window.location.search);
const paramsObject = Object.fromEntries(urlSearchParams.entries());
const cookieValue = JSON.stringify(paramsObject);
document.cookie = `bbQueryPSet=${cookieValue}`;
}
const bbQueryPSetValue = getCookieValue('bbQueryPSet');
if (bbQueryPSetValue) {
const params = JSON.parse(bbQueryPSetValue);
const urls = document.getElementsByTagName("a");
for (let i = 0; i < urls.length; i++) {
try {
const url = new URL(urls[i].href);
for (const [key, value] of Object.entries(params)) {
url.searchParams.append(key, value);
}
urls[i].href = url.toString();
} catch (error) {
console.error(`Invalid URL: ${urls[i].href}`);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment