MSN.com news - redirect to original site

Automatically redirect MSN news pages to the original source site.

// ==UserScript==
// @name         MSN.com news - redirect to original site
// @namespace    http://tampermonkey.net/
// @version      2025-03-17
// @description  Automatically redirect MSN news pages to the original source site.
// @author       Jamie Landeg-Jones
// @match        https://www.msn.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=msn.com
// @grant        none
// @license      MIT
// ==/UserScript==

// Props to the hints from this post: joeytwiddle@github - https://github.com/Tampermonkey/tampermonkey/issues/1279#issuecomment-875386821

(function() {
    'use strict';

    function redirect_to_article()
      {
        let numAttempts = 0;
        let doneit = 0;

        let tryNow = function()
          {
            let link_tags = document.getElementsByTagName ('link');

            for (let loop=0; loop < link_tags.length; loop++)
              {
                if (link_tags[loop].rel && link_tags[loop].rel == 'canonical')
                  {
                    const new_url = link_tags[loop].href;

                    doneit = 1;

                    if (new_url.match ("^https://(www\.)?msn.com/"))
                      console.info ('MSN Redirect: Ignoring redirect (same site): ' + new_url);
                     else
                      {
                        console.info ('MSN Redirect: Redirecting to ' + new_url);
                        window.location = new_url
                      }
                  }
              }

            if (!doneit)
              {
                if (numAttempts++ >= 20)
                  console.warn ('Giving up after 20 attempts. Could not find canonical link');
                 else
                  {
                    console.info ('MSN Redirect: Retrying, attempt ' + numAttempts.toString() + ' of 20.');
                    setTimeout (tryNow, 250 * Math.pow (1.1, numAttempts));
                  }
              }
          }
        tryNow();
      }

     // Run the filter when the page loads
    window.addEventListener ('load', redirect_to_article);
})();