Site Filter (Protocol-Independent)

Manage allowed sites dynamically and reference this in other scripts.

As of 2025-02-13. See the latest version.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greatest.deepsurf.us/scripts/526770/1536556/Site%20Filter%20%28Protocol-Independent%29.js

  1. // ==UserScript==
  2. // @name Site Filter (Protocol-Independent)
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Manage allowed sites dynamically and reference this in other scripts.
  6. // @author You
  7. // @match *://*/*
  8. // @grant GM_getValue
  9. // @grant GM_setValue
  10. // @grant GM_registerMenuCommand
  11. // @grant GM_download
  12. // ==/UserScript==
  13.  
  14. (function () {
  15. 'use strict';
  16.  
  17. const STORAGE_KEY = "additionalSites";
  18.  
  19. function getDefaultList() {
  20. return [
  21. "*.simplyblock.io*",
  22. "*nshipster.com*"
  23. ];
  24. }
  25.  
  26. function normalizeUrl(url) {
  27. return url.replace(/^https?:\/\//, ''); // Remove "http://" or "https://"
  28. }
  29.  
  30. let additionalSites = GM_getValue(STORAGE_KEY, []);
  31. let mergedSites = [...new Set([...getDefaultList(), ...additionalSites])].map(normalizeUrl);
  32.  
  33. GM_registerMenuCommand("➕ Add Current Site to Include List", addCurrentSiteMenu);
  34. GM_registerMenuCommand("📜 View Included Sites", viewIncludedSites);
  35. GM_registerMenuCommand("🗑️ Delete Specific Entries", deleteEntries);
  36. GM_registerMenuCommand("✏️ Edit an Entry", editEntry);
  37. GM_registerMenuCommand("🚨 Clear All Entries", clearAllEntries);
  38. GM_registerMenuCommand("📤 Export Site List as JSON", exportAdditionalSites);
  39. GM_registerMenuCommand("📥 Import Site List from JSON", importAdditionalSites);
  40.  
  41. async function shouldRunOnThisSite() {
  42. const currentFullPath = normalizeUrl(`${window.location.href}`);
  43. return mergedSites.some(pattern => wildcardToRegex(normalizeUrl(pattern)).test(currentFullPath));
  44. }
  45.  
  46. function wildcardToRegex(pattern) {
  47. return new RegExp("^" + pattern.replace(/[.+?^${}()|[\]\\]/g, '\\$&').replace(/\*/g, '.*').replace(/\?/g, '.') + "$");
  48. }
  49.  
  50. function addCurrentSiteMenu() {
  51. const currentHost = window.location.hostname;
  52. const currentPath = window.location.pathname;
  53. const domainParts = currentHost.split('.');
  54. const baseDomain = domainParts.length > 2 ? domainParts.slice(-2).join('.') : domainParts.join('.');
  55. const secondLevelDomain = domainParts.length > 2 ? domainParts.slice(-2, -1)[0] : domainParts[0];
  56.  
  57. const options = [
  58. { name: `Base Hostname (*.${baseDomain})`, pattern: `*.${baseDomain}` },
  59. { name: `Base Domain (*.${secondLevelDomain}.*)`, pattern: `*.${secondLevelDomain}.*` },
  60. { name: `Host Contains (*${secondLevelDomain}*)`, pattern: `*${secondLevelDomain}*` },
  61. { name: `Exact Path (${currentHost}${currentPath})`, pattern: normalizeUrl(`${window.location.href}`) },
  62. { name: "Custom Wildcard Pattern", pattern: normalizeUrl(`${window.location.href}`) }
  63. ];
  64.  
  65. const userChoice = prompt(
  66. "Select an option to add the site:\n" +
  67. options.map((opt, index) => `${index + 1}. ${opt.name}`).join("\n") +
  68. "\nEnter a number or cancel."
  69. );
  70.  
  71. if (!userChoice) return;
  72. const selectedIndex = parseInt(userChoice, 10) - 1;
  73. if (selectedIndex >= 0 && selectedIndex < options.length) {
  74. let pattern = normalizeUrl(options[selectedIndex].pattern);
  75. if (options[selectedIndex].name === "Custom Wildcard Pattern") {
  76. pattern = normalizeUrl(prompt("Edit custom wildcard pattern:", pattern));
  77. if (!pattern.trim()) return alert("Invalid pattern. Operation canceled.");
  78. }
  79. if (!additionalSites.includes(pattern)) {
  80. additionalSites.push(pattern);
  81. GM_setValue(STORAGE_KEY, additionalSites);
  82. mergedSites = [...new Set([...getDefaultList(), ...additionalSites])].map(normalizeUrl);
  83. alert(`✅ Added site with pattern: ${pattern}`);
  84. }
  85. }
  86. }
  87.  
  88. function viewIncludedSites() {
  89. //alert(`🔍 Included Sites:\n${mergedSites.join("\n") || "No sites added yet."}`);
  90. alert(`🔍 Included Sites:\n${additionalSites.join("\n") || "No sites added yet."}`);
  91. }
  92.  
  93. function deleteEntries() {
  94. if (additionalSites.length === 0) return alert("⚠️ No user-defined entries to delete.");
  95. const userChoice = prompt("Select entries to delete (comma-separated numbers):\n" +
  96. additionalSites.map((item, index) => `${index + 1}. ${item}`).join("\n"));
  97. if (!userChoice) return;
  98. const indicesToRemove = userChoice.split(',').map(num => parseInt(num.trim(), 10) - 1);
  99. additionalSites = additionalSites.filter((_, index) => !indicesToRemove.includes(index));
  100. GM_setValue(STORAGE_KEY, additionalSites);
  101. mergedSites = [...new Set([...getDefaultList(), ...additionalSites])].map(normalizeUrl);
  102. alert("✅ Selected entries have been deleted.");
  103. }
  104.  
  105. function editEntry() {
  106. if (additionalSites.length === 0) return alert("⚠️ No user-defined entries to edit.");
  107. const userChoice = prompt("Select an entry to edit:\n" +
  108. additionalSites.map((item, index) => `${index + 1}. ${item}`).join("\n"));
  109. if (!userChoice) return;
  110. const selectedIndex = parseInt(userChoice, 10) - 1;
  111. if (selectedIndex < 0 || selectedIndex >= additionalSites.length) return alert("❌ Invalid selection.");
  112. const newPattern = normalizeUrl(prompt("Edit the pattern:", additionalSites[selectedIndex]));
  113. if (newPattern && newPattern.trim() && newPattern !== additionalSites[selectedIndex]) {
  114. additionalSites[selectedIndex] = newPattern.trim();
  115. GM_setValue(STORAGE_KEY, additionalSites);
  116. mergedSites = [...new Set([...getDefaultList(), ...additionalSites])].map(normalizeUrl);
  117. alert("✅ Entry updated.");
  118. }
  119. }
  120.  
  121. function clearAllEntries() {
  122. if (additionalSites.length === 0) return alert("⚠️ No user-defined entries to clear.");
  123. if (confirm(`🚨 You have ${additionalSites.length} entries. Clear all?`)) {
  124. additionalSites = [];
  125. GM_setValue(STORAGE_KEY, additionalSites);
  126. mergedSites = [...getDefaultList()].map(normalizeUrl);
  127. alert("✅ All user-defined entries cleared.");
  128. }
  129. }
  130.  
  131. function exportAdditionalSites() {
  132. GM_download("data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(additionalSites, null, 2)), "additionalSites_backup.json");
  133. alert("📤 Additional sites exported as JSON.");
  134. }
  135.  
  136. function importAdditionalSites() {
  137. const input = document.createElement("input");
  138. input.type = "file";
  139. input.accept = ".json";
  140. input.onchange = event => {
  141. const reader = new FileReader();
  142. reader.onload = e => {
  143. additionalSites = JSON.parse(e.target.result);
  144. GM_setValue(STORAGE_KEY, additionalSites);
  145. mergedSites = [...new Set([...getDefaultList(), ...additionalSites])].map(normalizeUrl);
  146. alert("📥 Sites imported successfully.");
  147. };
  148. reader.readAsText(event.target.files[0]);
  149. };
  150. input.click();
  151. }
  152.  
  153. window.shouldRunOnThisSite = shouldRunOnThisSite;
  154. })();