Created
June 24, 2019 01:48
-
-
Save xiaoxiaoflood/a6ae974c867e38f1ff9f4305ade17b87 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 enterSelects.uc.js | |
// @include main | |
// @startup UC.enterSelects.exec(win); | |
// @shutdown UC.enterSelects.destroy(); | |
// @author xiaoxiaoflood | |
// @onlyonce | |
// ==/UserScript== | |
(function () { | |
UC.enterSelects = { | |
exec: function (win) { | |
xPref.lock('browser.urlbar.autoFill', false); | |
xPref.lock('browser.urlbar.insertMethod', 0); | |
xPref.set('browser.urlbar.matchBuckets', 'general:5,suggestion:Infinity'); | |
let gURLBar = win.gURLBar; | |
let {popup} = gURLBar; | |
let document = win.document; | |
popup.shouldSelect = false; | |
popup.cache = ''; | |
popup.orig_oRA = popup.onResultsAdded; | |
popup.onResultsAdded = (function () { | |
return function () { | |
if (popup.shouldSelect = UC.enterSelects.shouldSelect(win)) | |
popup.selectedIndex = 1; | |
var result = this.orig_oRA.apply(this, arguments); | |
return result; | |
}; | |
} | |
)(); | |
gURLBar.addEventListener('keydown', this.keyD, false); | |
}, | |
shouldSelect: function (win) { | |
let gURLBar = win.gURLBar; | |
let {popup} = gURLBar; | |
if (popup.cache == gURLBar.value) | |
return popup.shouldSelect; | |
popup.cache = gURLBar.value; | |
if (popup.matchCount < 1 || popup.selectedIndex > 0) | |
return false; | |
let {value} = gURLBar; | |
if (gURLBar.selectionEnd == value.length) | |
value = value.slice(0, gURLBar.selectionStart); | |
let search = value.trim(); | |
try { | |
// Test if is URL | |
return !Services.eTLD.getBaseDomainFromHost(search); | |
} catch (ex) {} | |
// Test about:about, chrome://blabla ... | |
if (/[a-zA-Z][\w-]*:(\/\/)?[\w-]+/.test(search)) | |
return false; | |
// Check if the first word is a keyword (search or bookmark) | |
if (this.isKeyword(search.match(/[^\s]*/)[0], popup)) | |
return false; | |
return true; | |
}, | |
isKeyword: function (keyword, popup) { | |
if (Services.search.getEngineByAlias(keyword) != null) | |
return true; | |
if (popup.richlistbox.children[0].getAttribute('url').startsWith('moz-action:keyword')) | |
return true; | |
return false; | |
}, | |
keyD: function (e) { | |
let gURLBar = e.target; | |
let {popup} = gURLBar; | |
if (e.keyCode == e.DOM_VK_RETURN && popup.selectedIndex == 1) { | |
--popup.selectedIndex; | |
gURLBar.controller.handleKeyNavigation(e.DOM_VK_DOWN); | |
} else if (e.keyCode == e.DOM_VK_TAB) { | |
let url = popup.richlistbox.children[1].getAttribute('url'); | |
if (popup.selectedIndex == 1 && gURLBar.value != url && new RegExp(/^(https?:\/\/(www\.)?)?/.source + gURLBar.value.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&')).test(url) && gURLBar.value != url.match(/(\w*:\/\/)?.+\..+?\//)[0]) { | |
gURLBar.value = url.match(/(\w*:\/\/)?.+\..+?\//)[0]; | |
popup.selectedIndex = gURLBar.value == url ? 1 : -1; | |
e.preventDefault(); | |
} else if (popup.selectedIndex == -1) { | |
popup.selectedIndex = 1; | |
gURLBar.value = url; | |
e.preventDefault(); | |
} else if (popup.selectedIndex == 1 && gURLBar.value != url) { | |
gURLBar.value = url; | |
e.preventDefault(); | |
} | |
} | |
}, | |
destroy: function () { | |
xPref.unlock('browser.urlbar.autoFill'); | |
xPref.unlock('browser.urlbar.insertMethod'); | |
xPref.clear('browser.urlbar.matchBuckets'); | |
_uc.windows((doc, win) => { | |
let gURLBar = win.gURLBar; | |
let {popup} = gURLBar; | |
popup.onResultsAdded = popup.orig_oRA; | |
gURLBar.removeEventListener('keydown', this.keyD, false); | |
delete popup.orig_oRA; | |
delete popup.shouldSelect; | |
delete popup.cache; | |
}); | |
delete UC.enterSelects; | |
} | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment