I'm trying to write a script that saves a url and a custom message for said url, and when I visit the url, it will redirect me to a new page with the custom message and a button to proceed to the page if I want. So far this is what I have-
// ==UserScript==
// @name Custom Messages
// @namespace http://localhost
// @version 1
// @description Custom messages for specific URLs
// @match http://*/*
// @match https://*/*
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==
// Retrieve the saved custom messages from the storage
var savedData = GM_getValue("customMessages", {});
// Function to add a new custom message and URL to the storage
function addCustomMessage() {
var url = prompt("Enter the URL to customize:");
var message = prompt("Enter the custom message for this URL:");
savedData[url] = message;
GM_setValue("customMessages", savedData);
};
// 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();
// Display the custom message with a link to the original URL
var message = savedData[url];
var confirmation = confirm(message + "\n\nDo you want to proceed to the original URL?");
if (confirmation) {
window.location.href = url;
}
}
}
});
// Add a button to the page to add a custom message
var button = document.createElement("button");
button.textContent = "Add Custom Message";
button.addEventListener("click", addCustomMessage);
document.body.appendChild(button);
What it does now is add a button to every page which I can use to add custom messages for a specific page. However, when I load the page, it doesn't redirect me to a page with the custom message and a button to proceed, it just loads the page as is. How should I go about rewriting this script so it does what I intend it to do?