javascript [userscript] [Google]修复Google搜索标签的顺序

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript [userscript] [Google]修复Google搜索标签的顺序相关的知识,希望对你有一定的参考价值。

// ==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);

以上是关于javascript [userscript] [Google]修复Google搜索标签的顺序的主要内容,如果未能解决你的问题,请参考以下文章

javascript [userscript] [QiitaDeck]格雷访问了链接

JavaScript 将图像嵌入为代码(对于UserScripts)

javascript [userscript] [Google]修复Google搜索标签的顺序

javascript [userscript] [tabelog]通过发送链接将当前商店添加到Google日历(需​​要修复开始/结束日期)

向 webview 添加 userScript

在 tampermonkey(userscript) 中包含所有页面