Last active
May 2, 2023 15:10
-
-
Save Flashwalker/c02e28809a897c4955f34e271db0e427 to your computer and use it in GitHub Desktop.
Open url alert prompt userscript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Open url prompt | |
// @version 0.0.3 | |
// @match *://*/* | |
// @author Flashwalker | |
// @description Press Ctrl+Shift+: or Ctrl+Shift+' to open location prompt | |
// @updateURL https://gist.githubusercontent.com/Flashwalker/c02e28809a897c4955f34e271db0e427/raw/open-url-prompt.user.js | |
// @downloadURL https://gist.githubusercontent.com/Flashwalker/c02e28809a897c4955f34e271db0e427/raw/open-url-prompt.user.js | |
// @homepage https://gist.github.com/Flashwalker/c02e28809a897c4955f34e271db0e427 | |
// ==/UserScript== | |
(function(window, undefined){ | |
if (window.self !== window.top){ | |
return; | |
} | |
const keySet = new Set() | |
keySet.add("Semicolon") | |
keySet.add("Quote") | |
// You can choose another key by event code: https://www.toptal.com/developers/keycode/ | |
let operatingSystem = navigator.userAgent.toLowerCase().search("mac") !== -1 ? "mac" : "pc" | |
// `CTRL + KEY` | |
function shortcutPressed(e, activationKey) { | |
// Set key event code (: or ') | |
if (operatingSystem === "mac") { | |
// CMD + KEY | |
return e.code === activationKey && e.metaKey && !e.shiftKey && !e.altKey && !e.ctrlKey; | |
} else { | |
// CTRL + KEY | |
return e.code === activationKey && e.ctrlKey && !e.shiftKey && !e.altKey && !e.metaKey; | |
} | |
} | |
// `CTRL + SHIFT + KEY` | |
function shortcutShiftPressed(e, activationKey) { | |
// Set key event code (: or ') | |
if (operatingSystem === "mac") { | |
// CMD + SHIFT + KEY | |
return e.code === activationKey && e.metaKey && e.shiftKey && !e.altKey && !e.ctrlKey; | |
} else { | |
// CTRL + SHIFT + KEY | |
return e.code === activationKey && e.ctrlKey && e.shiftKey && !e.altKey && !e.metaKey; | |
} | |
} | |
//for (const activationKey of keySet) { | |
document.addEventListener('keydown', e => { | |
for (const activationKey of keySet) { | |
if (shortcutPressed(e, activationKey)) { | |
let placeholder = "" | |
let url = prompt("Enter the URL", placeholder) | |
if(url) { | |
let proto = url.replace(/(^.+?:\/\/).+/gim, '$1') | |
url = url.replace(/^.*?:\/\/(.+)/gim, '$1') | |
if (proto === url) { | |
location.href = 'http://' + url | |
} else { | |
location.href = proto + url | |
} | |
} | |
}else if (shortcutShiftPressed(e, activationKey)){ | |
let placeholder = location.href | |
let url = prompt("Enter the URL", placeholder) | |
if(url) { | |
let proto = url.replace(/(^.+?:\/\/).+/gim, '$1') | |
url = url.replace(/^.*?:\/\/(.+)/gim, '$1') | |
if (proto === url) { | |
location.href = 'http://' + url | |
} else { | |
location.href = proto + url | |
} | |
} | |
} | |
} | |
}) | |
//} | |
})(window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment