UTM param stripper

Drop the UTM params from a URL when the page loads.

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey, Greasemonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

คุณจะต้องติดตั้งส่วนขยาย เช่น Tampermonkey หรือ Violentmonkey เพื่อติดตั้งสคริปต์นี้

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name          UTM param stripper
// @author        Paul Irish
// @namespace     http://github.com/paulirish
// @version       1.1.2
// @description   Drop the UTM params from a URL when the page loads.
// @extra         Cuz you know they're all ugly n shit.
// @include       /^https?:\/\/.*[\?#&]utm_.*/
// @grant         none
// ==/UserScript==


// Install instruction!!!!
// Click `raw` in the top right on this gist!

if (/utm_/.test(location.search) && window.history.replaceState){  
  
  // thx @cowboy for the revised hash param magic.
  var oldUrl = location.href;
  var newUrl = oldUrl.replace(/\?([^#]*)/, function(_, search) {
    search = search.split('&').map(function(v) {
      return !/^utm_/.test(v) && v;
    }).filter(Boolean).join('&'); // omg filter(Boolean) so dope.
    return search ? '?' + search : '';
  });

  if ( newUrl != oldUrl ) {
    window.history.replaceState({},'', newUrl); 
  }
  
}

// also..
// drop this into your own site, in case people link to you with 
// email newsletters or your rss feed's feedburner tracking or whatever.