用于重定向页面的 Violentmonkey 脚本

问题描述 投票:0回答:0

我正在尝试编写一个脚本来保存一个 url 和该 url 的自定义消息,当我访问该 url 时,它会将我重定向到一个带有自定义消息的新页面和一个按钮以继续该页面,如果我想。到目前为止,这就是我所拥有的-

// Open the IndexedDB database
var request = window.indexedDB.open("myDatabase", 1);
var db;
request.onerror = function(event) {
  console.log("Error opening database.");
};
request.onsuccess = function(event) {
  db = event.target.result;

  // Retrieve the saved URLs and messages from the database
  var transaction = db.transaction(["customMessages"], "readonly");
  var objectStore = transaction.objectStore("customMessages");
  var savedData = {};
  objectStore.openCursor().onsuccess = function(event) {
    var cursor = event.target.result;
    if (cursor) {
      savedData[cursor.key] = cursor.value;
      cursor.continue();
    }
  };

  // Function to add a new custom message and URL to the database
  function addCustomMessage() {
    var url = prompt("Enter the URL to customize:");
    var message = prompt("Enter the custom message for this URL:");
    var transaction = db.transaction(["customMessages"], "readwrite");
    var objectStore = transaction.objectStore("customMessages");
    objectStore.put(message, url);
    savedData[url] = message;
  };

  // Listen for changes to the database and update saved data
  var observer = new MutationObserver(function(mutationsList, observer) {
    for (var mutation of mutationsList) {
      if (mutation.type === 'attributes' && mutation.attributeName === 'data-custom-messages') {
        // Retrieve the updated saved data from the database
        var transaction = db.transaction(["customMessages"], "readonly");
        var objectStore = transaction.objectStore("customMessages");
        savedData = {};
        objectStore.openCursor().onsuccess = function(event) {
          var cursor = event.target.result;
          if (cursor) {
            savedData[cursor.key] = cursor.value;
            cursor.continue();
          }
        };
      }
    }
  });
  observer.observe(document.body, { attributes: true, attributeFilter: ['data-custom-messages'] });

  // Loop through the saved URLs and redirect if there is a match
  function checkSavedData() {
    for (var url in savedData) {
      if (window.location.href.indexOf(url) !== -1) {
        var message = savedData[url];
        // Display the custom message with a link to the original URL
        var confirmation = confirm(message + "\n\nDo you want to proceed to the original URL?");
        if (!confirmation) {
          window.location.href = "about:blank";
        }
      }
    }
  }
  checkSavedData();

  // Add a button to the custom message page to load the original URL
  var customMessagePage = document.createElement("div");
  customMessagePage.innerHTML = "<h1>Custom Message</h1><p>This is a custom message for this URL.</p><button id=\"load-original-url\">Load original URL</button>";
  customMessagePage.querySelector("#load-original-url").addEventListener("click", function() {
    window.location.href = url;
  });
  document.body.appendChild(customMessagePage);

  // Add a button to the page to add a new custom message
  var addCustomMessageButton = document.createElement("button");
  addCustomMessageButton.innerHTML = "Add Custom Message";
  addCustomMessageButton.addEventListener("click", addCustomMessage);
  document.body.appendChild(addCustomMessageButton);

  // Listen for clicks on links and check saved data before redirecting
  document.addEventListener("click", function(event) {
    if (event.target.tagName.toLowerCase() === "a") {
      var url = event.target.href;
      if (savedData[url]) {
        event.preventDefault();
        window.history.pushState({}, "", "custom-message.html");
        document.body.setAttribute("data-custom-messages", JSON.stringify(savedData));
      }
      checkSavedData();
    }
  });
};

// Create the object store in the database
request.onupgradeneeded = function(event) {
  db = event.target.result;
  var objectStore = db.createObjectStore("customMessages", { keyPath: "url" });
};

当我加载任何页面时,它似乎没有显示“添加自定义消息”按钮,当我加载一个单独的脚本来执行此操作时,它从不重定向我。我应该如何修复这个脚本?

javascript tampermonkey userscripts greasemonkey-4
© www.soinside.com 2019 - 2024. All rights reserved.