Remove WWW

Redirect to URL without www.

// ==UserScript== 
// @name        Remove WWW
// @namespace   i2p.schimon.no-www
// @description Redirect to URL without www.
// @author      Schimon Jehudah, Adv.
// @homepageURL https://greasyfork.org/scripts/465951-no-www
// @supportURL  https://greasyfork.org/scripts/465951-no-www/feedback
// @copyright   2023 - 2024, Schimon Jehudah (http://schimon.i2p)
// @license     MIT; https://opensource.org/licenses/MIT
// @run-at      document-start
// @include     *://www.*
// @connect     self
// @grant       GM_xmlhttpRequest
// @grant       GM.xmlHttpRequest
// @icon        data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTI4IiBoZWlnaHQ9IjEyOCIgdmlld0JveD0iMCAwIDMuODQgMy44NCIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj48dGV4dCB4bWw6c3BhY2U9InByZXNlcnZlIiBzdHlsZT0iZm9udC1zdHlsZTpub3JtYWw7Zm9udC12YXJpYW50Om5vcm1hbDtmb250LXdlaWdodDo3MDA7Zm9udC1zdHJldGNoOm5vcm1hbDtmb250LXNpemU6MTkycHg7bGluZS1oZWlnaHQ6MDtmb250LWZhbWlseTpNb25vc3BhY2U7LWlua3NjYXBlLWZvbnQtc3BlY2lmaWNhdGlvbjonTW9ub3NwYWNlIEJvbGQnO2ZvbnQtdmFyaWFudC1saWdhdHVyZXM6bm9ybWFsO2ZvbnQtdmFyaWFudC1wb3NpdGlvbjpub3JtYWw7Zm9udC12YXJpYW50LWNhcHM6bm9ybWFsO2ZvbnQtdmFyaWFudC1udW1lcmljOm5vcm1hbDtmb250LXZhcmlhbnQtYWx0ZXJuYXRlczpub3JtYWw7Zm9udC12YXJpYW50LWVhc3QtYXNpYW46bm9ybWFsO2ZvbnQtZmVhdHVyZS1zZXR0aW5nczpub3JtYWw7dGV4dC1pbmRlbnQ6MDt0ZXh0LWFsaWduOnN0YXJ0O3RleHQtZGVjb3JhdGlvbjpub25lO3RleHQtZGVjb3JhdGlvbi1saW5lOm5vbmU7dGV4dC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xkJyI+V1dXPC90c3Bhbj48L3RleHQ+PHBhdGggZD0iTTEuOTIgMGExLjkyIDEuOTIgMCAxIDAgMS45MiAxLjkyQTEuOTIgMS45MiAwIDAgMCAxLjkyIDBabTAgMy40NTZBMS41MzYgMS41MzYgMCAwIDEgLjM4NCAxLjkyIDEuNTIgMS41MiAwIDAgMSAuNzA4Ljk4bDIuMTUzIDIuMTUyYTEuNTIgMS41MiAwIDAgMS0uOTQxLjMyNFptMS4yMTItLjU5NUwuOTc5LjcwOEExLjUyIDEuNTIgMCAwIDEgMS45Mi4zODQgMS41MzYgMS41MzYgMCAwIDEgMy40NTYgMS45MmExLjUyIDEuNTIgMCAwIDEtLjMyNC45NFoiIHN0eWxlPSJmaWxsOnJlZDtmaWxsLW9wYWNpdHk6MTtzdHJva2Utd2lkdGg6LjE5MTk5NztzdHJva2UtZGFzaGFycmF5Om5vbmUiLz48L3N2Zz4=
// @version     24.04
// ==/UserScript==

// SVG icon from https://www.svgrepo.com/svg/357363/ban

// @makyen
// /greasemonkey/greasemonkey/issues/3160#issuecomment-1456758080
const gmXmlhttpRequest = typeof GM_xmlhttpRequest === 'function' ? GM_xmlhttpRequest : GM.xmlHttpRequest;

// if (!location.host.startsWith('www.')) return; // exit (else, continue)

var newURL = location.href.replace('://www.','://');

gmXmlhttpRequest({
  method: 'GET',
  url: newURL,
  onprogress: console.log('Checking for no-www...'),
  onload: function(response) {
    if (response.finalUrl == newURL && response.status == 200) {
      location.href = newURL;
      console.info(`Successfully removed www (Response status: ${response.status}).`);
    } else {
      console.log(`Please contact webmaster to remove www. https://no-www.org/ (Response status: ${response.status}).`);
    }
  },
  onerror: function(response) {
    console.log('Error requesting for no-www. Please contact webmaster to remove www.')
  }
})

/*
fetch(newURL)
  .then((response) => {
     if (request.reseponseURL == newURL) {
       window.open(newURL,'_self');
     }
  })
*/