Redirect to Opener

Allow you to redirect customizable urls to the iOS Opener app.

  1. // ==UserScript==
  2. // @name Redirect to Opener
  3. // @version 1.1
  4. // @description Allow you to redirect customizable urls to the iOS Opener app.
  5. // @author yodaluca23
  6. // @license GNU GPLv3
  7. // @match *://*/*
  8. // @grant GM_setValue
  9. // @grant GM_getValue
  10. // @grant GM_registerMenuCommand
  11. // @namespace https://greasyfork.org/users/1315976
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16.  
  17. // Keys for stored lists
  18. const websiteListKey = "websiteList";
  19. const exclusionListKey = "exclusionList";
  20.  
  21. // Retrieve or initialize the lists
  22. let websiteList = GM_getValue(websiteListKey, []);
  23. let exclusionList = GM_getValue(exclusionListKey, []);
  24.  
  25. // Function to add a website to the redirect list
  26. function addWebsite() {
  27. const website = prompt("Enter a website or part of a URL to redirect (e.g., youtube.com):");
  28. if (!website) return;
  29.  
  30. websiteList.push(website);
  31. GM_setValue(websiteListKey, websiteList);
  32. alert(`Website "${website}" added to the redirect list.`);
  33. }
  34.  
  35. // Function to manage the redirect list
  36. function manageWebsiteList() {
  37. if (websiteList.length === 0) {
  38. alert("No websites have been added yet.");
  39. return;
  40. }
  41.  
  42. let listDisplay = "Current websites in the redirect list:\n\n";
  43. websiteList.forEach((item, index) => {
  44. listDisplay += `${index + 1}. ${item}\n`;
  45. });
  46. listDisplay += "\nEnter the number of the website to delete, or press Cancel to exit.";
  47.  
  48. const choice = prompt(listDisplay);
  49. const index = parseInt(choice, 10) - 1;
  50.  
  51. if (!isNaN(index) && index >= 0 && index < websiteList.length) {
  52. websiteList.splice(index, 1);
  53. GM_setValue(websiteListKey, websiteList);
  54. alert("Website removed successfully!");
  55. }
  56. }
  57.  
  58. // Function to add a website to the exclusion list
  59. function addExclusion() {
  60. const exclusion = prompt("Enter a URL part to exclude from redirection (e.g., login.google.com):");
  61. if (!exclusion) return;
  62.  
  63. exclusionList.push(exclusion);
  64. GM_setValue(exclusionListKey, exclusionList);
  65. alert(`Exclusion "${exclusion}" added to the list.`);
  66. }
  67.  
  68. // Function to manage the exclusion list
  69. function manageExclusionList() {
  70. if (exclusionList.length === 0) {
  71. alert("No exclusions have been added yet.");
  72. return;
  73. }
  74.  
  75. let listDisplay = "Current exclusions:\n\n";
  76. exclusionList.forEach((item, index) => {
  77. listDisplay += `${index + 1}. ${item}\n`;
  78. });
  79. listDisplay += "\nEnter the number of the exclusion to delete, or press Cancel to exit.";
  80.  
  81. const choice = prompt(listDisplay);
  82. const index = parseInt(choice, 10) - 1;
  83.  
  84. if (!isNaN(index) && index >= 0 && index < exclusionList.length) {
  85. exclusionList.splice(index, 1);
  86. GM_setValue(exclusionListKey, exclusionList);
  87. alert("Exclusion removed successfully!");
  88. }
  89. }
  90.  
  91. // Register menu commands
  92. GM_registerMenuCommand("Add Website to Redirect List", addWebsite);
  93. GM_registerMenuCommand("Manage Redirect List", manageWebsiteList);
  94. GM_registerMenuCommand("Add Exclusion", addExclusion);
  95. GM_registerMenuCommand("Manage Exclusion List", manageExclusionList);
  96.  
  97. // Redirect logic
  98. const currentURL = window.location.href;
  99.  
  100. // Check if the URL matches any exclusion before redirecting
  101. for (const exclusion of exclusionList) {
  102. if (currentURL.includes(exclusion)) {
  103. console.log(`Redirection prevented: URL contains excluded term "${exclusion}".`);
  104. return; // Stop execution if an exclusion is found
  105. }
  106. }
  107.  
  108. // Check if the URL matches any redirect condition
  109. for (const website of websiteList) {
  110. if (currentURL.includes(website)) {
  111. const encodedURL = encodeURIComponent(currentURL);
  112. const openerURL = `opener://x-callback-url/show-options?url=${encodedURL}`;
  113.  
  114. // Redirect to the constructed URL
  115. window.location.href = openerURL;
  116. break;
  117. }
  118. }
  119. })();