Ultimate Popup Blocker

Configurable popup blocker that blocks all popup windows by default.

目前为 2023-10-29 提交的版本。查看 最新版本

// ==UserScript==
// @name         Ultimate Popup Blocker
// @namespace    https://gf.qytechs.cn/en/users/670188-hacker09?sort=daily_installs
// @version      1
// @description  Configurable popup blocker that blocks all popup windows by default.
// @author       hacker09
// @include      *
// @icon         https://i.imgur.com/6SGMd42.png
// @run-at       document-end
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_openInTab
// @grant        GM_listValues
// @grant        GM_deleteValue
// @grant        GM_registerMenuCommand
// ==/UserScript==

(function() {
  'use strict';
  var timeleft = 15; //Create a variable to hold the time left
  unsafeWindow.upb_counter = 0; //Count amount of blocked popups
  const domain = location.host; //Save the domain url
  const logDiv = document.createElement('div'); //Create the script menu

  logDiv.style.cssText = 'position: fixed; bottom: 0px; left: 0px; z-index: 99999; width: unset; padding: 5px; font: inherit; background-color: black; color: white; cursor: help; display: block;'; //Add the script menu style
  document.body.appendChild(logDiv); //Add the script menu on the page

  GM_registerMenuCommand('Configure popup permissions', () => { GM_openInTab('https://f.org/ultimate-popup-blocker/settings.html', false); }); //Add a settings page on the Tampermonkey menu

  if (!GM_getValue(`trusted_${domain}`)) { //Override the browser default "window.open" implementation with the script fakeWindowOpen method
    unsafeWindow.open = (function(Url) { //Run the script fake Window Open function when the browser tries to open a new window
      unsafeWindow.upb_counter += 1; //Count the amount of blocked popups

      logDiv.innerHTML = `Allow <b><u>${domain}</u></b> to open a popup? <b>(${unsafeWindow.upb_counter})</b>`; //Shows the script menu
      logDiv.title = Url; //Shows the blocked url
      logDiv.style.display = 'block'; //Make the menu visible

      function createButton(text, id, clickCallback, inlineStyle) { //Create the script menu buttons
        const button = document.createElement('button'); //Create the button element
        button.innerHTML = text; //Add a text to the button element
        button.id = id; //Add an id to the button element (just because of the Close Button)
        button.style.cssText =`text-decoration: none; color: black; cursor: pointer; margin: 0 5px; padding: 1px 3px; background-color: rgb(255, 255, 255); border-width: 0px; border-radius: 5px; color: black; ${inlineStyle}`; //Add a style to each button (Use inlineStyle just because of the Close/createButton Buttons)
        logDiv.appendChild(button); //Add the button on the menu
        button.addEventListener('click', clickCallback); //Add a button on click action
      }

      createButton('Allow ↗️', 'upb_open', () => { open(Url, '_blank'); logDiv.style.display = 'none'; }, '',); //Create the Open Popup Button
      createButton('Always Allow ✅', 'upb_trust', () => { GM_setValue(`trusted_${domain}`, true); open(Url, '_blank'); logDiv.style.display = 'none'; }, '', ); //Create the Trust Button
      createButton(`Deny (${timeleft}) ❌`, 'upb_close', () => { logDiv.style.display = 'none'; }, ' background-color: #a00; color: white;', ); //Create the Close Button
      createButton('Config ⚙️', 'upb_config', () => { GM_openInTab('https://f.org/ultimate-popup-blocker/settings.html', false); }, ' float: right; margin: 0 10px 0 0;', ); //Create the Config Button
      if (timeleft === 15) { //If the time left is exactly 15 secs
        const Timer = setInterval(() => { //Start a interval function checker
          document.getElementById('upb_close').innerHTML = `Deny (${timeleft}) ❌`; //Update the timer
          timeleft -= 1; //Decrease the time left
          if (timeleft < 0) { //If the time left is less than 0
            clearInterval(Timer); //Stop the timer from running
            logDiv.style.display = 'none'; //Hide the script menu
            timeleft = 15; //Reset the time left
          } //Finishes the if condition
        }, 1000); //Update the displayed timer each second
      } //Finishes the if condition
      return { blur() { return false; }, focus() { return false; }, }; }); //Return the fake window function to not encounter JS runtime error when the popup originator page wants to call focus() or blur()
  } //Finishes the if condition

  if (location.href === 'https://f.org/ultimate-popup-blocker/settings.html') { //If the user is on the page settings website
    document.head.remove(); //Remove current page head
    document.body.remove(); //Remove current page body
    document.querySelector("html").innerHTML = `<title>[UPB] Permission manager</title> <style> body { margin: 0; min-width: 250px; } /* Include the padding and border in an element's total width and height */ * { box-sizing: border-box; } /* Remove margins and padding from the list */ ul { margin: 0; padding: 0; } /* Style the list items */ ul li { position: relative; padding: 12px 8px 12px 40px; list-style-type: none; background: #eee; font-size: 18px; transition: 0.2s; /* make the list items unselectable */ -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } /* Set all odd list items to a different color (zebra-stripes) */ ul li:nth-child(odd) { background: #f9f9f9; } /* Darker background-color on hover */ ul li:hover { background: #ddd; } /* Style the close button */ .close { cursor: pointer; position: absolute; right: 0; top: 0; padding: 12px 16px 12px 16px; } .close:hover { background-color: #f44336; color: white; } /* Style the header */ .header { background-color: #000000; padding: 30px 40px; color: white; text-align: center; } .title { margin: 5px; } .subtitle { text-align: left; } /* Clear floats after the header */ .header:after { content: ""; display: table; clear: both; } /* Style the footer */ .footer { background-color: #000000; padding: 5px 40px; color: white; } /* Style the input */ input { margin: 0; border: none; border-radius: 0; width: 75%; height: 45px; padding: 10px; float: left; font-size: 16px; } /* Style the "Add" button */ .addBtn { padding: 10px; width: 25%; height: 45px; background: #d9d9d9; color: #555; float: left; text-align: center; font-size: 16px; cursor: pointer; transition: 0.3s; border-radius: 0; } .addBtn:hover { background-color: #bbb; } </style> </head> <body> <div class="header"> <h2 class="title">Ultimate Popup Blocker</h2> <h3 class="subtitle">Trusted websites:</h3> <input type="text" id="Input" placeholder="e.g. reddit.com"> <span class="addBtn">Add</span> </div> <ul id="List"> </ul> <div class="footer"> <h4 class="subtitle">All data is stored in the UserScript's local storage.</h4> </div> </body> `; //Add the script settings page on the website

    function addDomainToPermissionList(domain) { //For each domain on the Tampermonkey storage
      const li = document.createElement('li'); //Create a new li element
      li.appendChild(document.createTextNode(domain.replace('trusted_', ''))); //Add the domain on the li element list
      document.getElementById('List').appendChild(li); //Add the list on the page
      const span = document.createElement('SPAN'); //Create a new span X element
      const txt = document.createTextNode('✘'); //Add a remove X button to li
      span.className = 'close'; //Add a class name to the span element (so that the page's internal CSS knows where to place the X button)
      span.appendChild(txt); //Add the remove X button on the page
      span.onclick = (function() { //When the X span element is clicked
        GM_deleteValue(`trusted_${this.parentElement.innerText.replace('\n✘', '')}`); //Remove the domain from the Tampermonkey storage
        this.parentElement.style.display = 'none'; //Hide the removed domain from the page list
      }); //Finishes the onclick function
      li.appendChild(span); //Add each span X element on the list
    } //Finishes the addDomainToPermissionList function

    document.getElementsByClassName('addBtn')[0].addEventListener('click', () => { //If the Add Button is clicked on the page
      if (document.getElementById('Input').value !== '') { //If the user is not trying to add a blank domain
        GM_setValue(`trusted_${document.getElementById('Input').value}`, true); //Add the domain on the Tampermonkey storage
      } //Finishes the if condition
      document.getElementById('Input').value = ''; //Reset the domain the user wrote
    }); //Finishes the onclick function
    //Show already stored elements in the list
    GM_listValues().forEach(addDomainToPermissionList); //Run the addDomainToPermissionList ForEach domain on the Tampermonkey Storage
  } //Finishes the if condition
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址