Skip to content

Instantly share code, notes, and snippets.

@Flashwalker
Last active May 2, 2023 15:10
Show Gist options
  • Save Flashwalker/c02e28809a897c4955f34e271db0e427 to your computer and use it in GitHub Desktop.
Save Flashwalker/c02e28809a897c4955f34e271db0e427 to your computer and use it in GitHub Desktop.
Open url alert prompt userscript
// ==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