Cache

Limpa o cache sempre que uma nova guia for aberta

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name                  Cache
// @description           Limpa o cache sempre que uma nova guia for aberta
// @namespace             CowanCACHE
// @license               GPL-3.0
// @version               2.1
// @author                Cowanbas
// @match                 *://*/*
// @run-at                document-start
// ==/UserScript==

(function () {
  'use strict';

  // Armazena a chave única para o site atual no sessionStorage
  const siteKey = `cacheCleared:${window.location.hostname}`;

  // Verifica se a limpeza já foi realizada para esta aba
  if (sessionStorage.getItem(siteKey)) {
    console.log(`Cache already cleared for ${window.location.hostname}.`);
    return; // Sai do script se a limpeza já foi feita nesta sessão
  }

  // Marca a limpeza como feita para esta sessão
  sessionStorage.setItem(siteKey, 'true');

  // Limpa Service Workers
  function clearServiceWorkers() {
    if ('serviceWorker' in navigator) {
      navigator.serviceWorker.getRegistrations().then(function (registrations) {
        registrations.forEach(function (registration) {
          registration.unregister();
          console.log('Service worker cache cleared.');
        });
      });
    }
  }

  // Adiciona parâmetros únicos para evitar uso de cache nos recursos atuais
  function reloadWithoutCache() {
    const allLinks = document.querySelectorAll('link[rel="stylesheet"], script, img');
    allLinks.forEach(element => {
      const url = new URL(element.src || element.href, window.location.origin);
      url.searchParams.set('_nocache', Date.now());
      if (element.tagName === 'LINK' || element.tagName === 'SCRIPT') {
        element.href = url.toString();
      } else if (element.tagName === 'IMG') {
        element.src = url.toString();
      }
    });
    console.log('Resources reloaded with cache-busting parameters.');

    // Recarrega a página sem usar cache
    location.reload(); // Recarrega a página normalmente
  }

  // Executa a limpeza
  console.log(`Performing cache clear for ${window.location.hostname}.`);
  clearServiceWorkers();
  reloadWithoutCache();
})();