Nefarious URL Redirect Blocker

Detects and stops URL redirections, loads the original URL, and logs the actions

当前为 2024-04-09 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Nefarious URL Redirect Blocker
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.8
  5. // @description Detects and stops URL redirections, loads the original URL, and logs the actions
  6. // @match http://*/*
  7. // @match https://*/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. // Store the original URL
  16. const originalUrl = window.location.href;
  17.  
  18. // Flag to track if the script has been activated
  19. let scriptActivated = false;
  20.  
  21. // Function to log actions
  22. function logAction(message) {
  23. console.log(message);
  24. }
  25.  
  26. // Function to handle redirection
  27. function handleRedirect(event) {
  28. // Check if the URL has changed
  29. if (window.location.href !== originalUrl && !scriptActivated) {
  30. // Set the script activation flag
  31. scriptActivated = true;
  32.  
  33. // Stop the redirection
  34. event.preventDefault();
  35. event.stopPropagation();
  36.  
  37. // Log the action
  38. logAction('Redirection stopped.');
  39. }
  40. }
  41.  
  42. // Function to continuously check for URL changes
  43. function checkUrlChange() {
  44. if (window.location.href !== originalUrl && !scriptActivated) {
  45. // Set the script activation flag
  46. scriptActivated = true;
  47.  
  48. // Push the original URL into the browser history
  49. window.history.pushState(null, null, originalUrl);
  50.  
  51. // Replace the current URL with the original URL
  52. window.history.replaceState(null, null, originalUrl);
  53.  
  54. // Log the action
  55. logAction('Redirection stopped. Original URL loaded.');
  56. }
  57.  
  58. // Reset the script activation flag
  59. scriptActivated = false;
  60.  
  61. // Schedule the next check
  62. setTimeout(checkUrlChange, 100);
  63. }
  64.  
  65. // Listen for the beforeunload event (forward direction)
  66. window.addEventListener('beforeunload', handleRedirect);
  67.  
  68. // Listen for the popstate event (backward direction)
  69. window.addEventListener('popstate', handleRedirect);
  70.  
  71. // Start checking for URL changes
  72. checkUrlChange();
  73. })();