Search code examples
javascriptgreasemonkey-4

Greasemonkey add parameter to end of URL


I am having a little bit of Greasmonkey trouble

I want to write a scrip that automatically adds a "?" to the end of every url fetched when using a forum (long story, but doing this prevents the caching issues the owners are having)

I have this, but it does what I want, however it keeps on redirecting and adding another "?" so i end up with "forum.domain.com/viewforum.php?f=4????????????" and it keeps adding another question mark without loading the forum

This is basic to me so i cannot work this out, so help would be appreciated.

// ==UserScript==
// @name       sort out caching issue
// @version    1.01
// @description  Adds parameter to sort caching issue
// @include      http://forum.domain.com/*
// @include      http://forum.domain.com/viewforum.php?f=4
// @include      http://forum.domain.com/viewforum.php?f=5
// @exclude      http://forum.domain.com/index.php
// @run-at document-start
// ==/UserScript==

window.location.replace (window.location.href + "~");

Im guessing there needs to be some sort of check to see if ts been run already, but as I am jumping on StackOverflow as a beginner, any help would be appreciated.

Thanks


Solution

  • Because you're adding a ~ to the end of the URL, it sounds like all you need to do is check to see if the final character in the current URL is a ~ or not. If not, then you can add it, and the page will refresh; else, do nothing, thus preventing the infinite refresh loop:

    const { href } = window.location;
    if (href.slice(-1) !== '~') {
      window.location.replace(href + '~');
    }