Last active
July 31, 2018 04:31
-
-
Save tkrkt/4fa57eedf9922c0719d26d67db08b8c2 to your computer and use it in GitHub Desktop.
[userscript] [Google] Do not sort tabs Google
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 [Google Search] Do not sort tabs Google | |
// @namespace https://gist.github.com/tkrkt | |
// @description Fix order of tabs on google search | |
// @version 2 | |
// @include https://www.google.co.jp/search* | |
// @grant none | |
// ==/UserScript== | |
const order = [ | |
'search', | |
'map', | |
'image', | |
'video', | |
'news', | |
'more', | |
'shopping', | |
'books', | |
'flight', | |
'finance' | |
]; | |
const style = document.createElement('style'); | |
style.textContent = ` | |
.hover-gray:hover {background: rgba(0,0,0,0.1)}; | |
`; | |
document.head.appendChild(style); | |
const links = {}; | |
const tbmMap = { | |
www: 'search', | |
maps: 'map', | |
isch: 'image', | |
vid: 'video', | |
nws: 'news', | |
shop: 'shopping', | |
bks: 'books', | |
flm: 'flight', | |
fin: 'finance' | |
}; | |
const main = (tab, more) => { | |
Array.from(tab.querySelectorAll('.hdtb-mitem')).forEach(element => { | |
const a = element.firstChild; | |
const href = a.href || location.href; | |
const tbm = new URL(href).searchParams.get('tbm') || new URL(href).host.split('.')[0]; | |
links[tbmMap[tbm]] = { | |
name: element.textContent.trim(), | |
href, | |
tbm, | |
isActive: element.classList.contains('hdtb-msel') | |
}; | |
}); | |
let moreClassName; | |
Array.from(more.querySelectorAll('a[role="menuitem"]')).forEach(a => { | |
moreClassName = a.className; | |
const tbm = new URL(a.href).searchParams.get('tbm') || new URL(a.href).host.split('.')[0]; | |
links[tbmMap[tbm]] = { | |
name: a.textContent.trim(), | |
href: a.href, | |
tbm | |
}; | |
}); | |
while(tab.firstChild && tab.removeChild(tab.firstChild)); | |
while(more.firstChild && more.removeChild(more.firstChild)); | |
let isTab = true; | |
order.forEach(type => { | |
if (type === 'more') { | |
isTab = false; | |
} else { | |
const item = links[type]; | |
if (isTab) { | |
const div = document.createElement('div'); | |
if (item.isActive) { | |
div.className = 'hdtb-mitem hdtb-msel hdtb-imb'; | |
div.textContent = item.name; | |
} else { | |
div.className = 'hdtb-mitem hdtb-imb'; | |
const a = document.createElement('a'); | |
a.textContent = item.name; | |
a.className = 'q qs'; | |
a.href = item.href; | |
div.appendChild(a); | |
} | |
tab.appendChild(div, tab.lastChild); | |
} else { | |
const a = document.createElement('a'); | |
a.textContent = item.name; | |
a.className = moreClassName + ' hover-gray'; | |
a.href = item.href; | |
a.setAttribute('role', 'menuitem'); | |
more.appendChild(a); | |
} | |
} | |
}); | |
}; | |
let timer; | |
const watch = () => { | |
clearInterval(timer); | |
timer = setInterval(() => { | |
const tab = document.getElementById('hdtb-msb-vis'); | |
const more = document.querySelector('#ow3 > div') || document.querySelector('#lb > div[role="menu"]'); | |
if (tab && more) { | |
main(tab, more); | |
clearInterval(timer); | |
} | |
}, 100); | |
}; | |
watch(); | |
window.addEventListener('hashchange', watch, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment