MSN.com news - redirect to original site

Automatically redirect MSN news pages to the original source site.

  1. // ==UserScript==
  2. // @name MSN.com news - redirect to original site
  3. // @namespace http://tampermonkey.net/
  4. // @version 2025-03-17
  5. // @description Automatically redirect MSN news pages to the original source site.
  6. // @author Jamie Landeg-Jones
  7. // @match https://www.msn.com/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=msn.com
  9. // @grant none
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. // Props to the hints from this post: joeytwiddle@github - https://github.com/Tampermonkey/tampermonkey/issues/1279#issuecomment-875386821
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. function redirect_to_article()
  19. {
  20. let numAttempts = 0;
  21. let doneit = 0;
  22.  
  23. let tryNow = function()
  24. {
  25. let link_tags = document.getElementsByTagName ('link');
  26.  
  27. for (let loop=0; loop < link_tags.length; loop++)
  28. {
  29. if (link_tags[loop].rel && link_tags[loop].rel == 'canonical')
  30. {
  31. const new_url = link_tags[loop].href;
  32.  
  33. doneit = 1;
  34.  
  35. if (new_url.match ("^https://(www\.)?msn.com/"))
  36. console.info ('MSN Redirect: Ignoring redirect (same site): ' + new_url);
  37. else
  38. {
  39. console.info ('MSN Redirect: Redirecting to ' + new_url);
  40. window.location = new_url
  41. }
  42. }
  43. }
  44.  
  45. if (!doneit)
  46. {
  47. if (numAttempts++ >= 20)
  48. console.warn ('Giving up after 20 attempts. Could not find canonical link');
  49. else
  50. {
  51. console.info ('MSN Redirect: Retrying, attempt ' + numAttempts.toString() + ' of 20.');
  52. setTimeout (tryNow, 250 * Math.pow (1.1, numAttempts));
  53. }
  54. }
  55. }
  56. tryNow();
  57. }
  58.  
  59. // Run the filter when the page loads
  60. window.addEventListener ('load', redirect_to_article);
  61. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址