// background.js

// Function to block specific requests
function blockRequest(details) {
    // Check if the request URL matches the file you want to block
    if (details.url.includes("title_manager.js")) {
        console.log("Blocking request for:", details.url);
        return { cancel: true }; // Cancel the request
    }
}

// Check if the browser is Firefox or Chrome
const isFirefox = typeof browser !== "undefined";

if (isFirefox) {
    // Use the browser API for Firefox
    browser.webRequest.onBeforeRequest.addListener(
        blockRequest,
        { urls: [
            "https://oss-auth.blinklab.com/*",
            "https://oss-auth.thecheese.io/*",
            "https://oss-auth.shop.wii.com/*"
        ]}, // Adjust this to match the URLs where you want to block files
        ["allowed"] // Enable blocking
    );
} else {
    // Use the chrome API for Chrome
    chrome.webRequest.onBeforeRequest.addListener(
        blockRequest,
        { urls: [
            "https://oss-auth.blinklab.com/*", 
            "https://oss-auth.thecheese.io/*",
            "https://oss-auth.shop.wii.com/*",
            "http://wiimart:8080/oss/serv/*",
            "https://wiimart:8080/oss/serv/*"
        ]}, // Adjust this to match the URLs where you want to block files
        ["allowed"] // Enable blocking
    );
}

// background.js

// Listen for web requests
chrome.webRequest.onBeforeRequest.addListener(
    function(details) {
        // Check if the URL matches the pattern
        const urlPattern = /^miip:\/\/CID\/.*\.bmp\?width=48&height=48&bgR=\d{1,3}&bgG=\d{1,3}&bgB=\d{1,3}$/;
        if (urlPattern.test(details.url)) {
            // Redirect to the image stored in the extension
            return { redirectUrl: chrome.runtime.getURL("images/mii.bmp") };
        }
    },
    { urls: [
        "https://oss-auth.blinklab.com/*", 
        "https://oss-auth.thecheese.io/*",
        "https://oss-auth.shop.wii.com/*",
        "http://wiimart:8080/oss/serv/*",
        "https://wiimart:8080/oss/serv/*"
    ]}, // You can restrict this to specific URLs if needed
    ["blocking"]
);