Add language filter In Google search for each language defined in google search settings

It takes the languages you have configured in your google search settings and adds them as a separated filter for each instead of adding just one filter for all.

目前為 2020-03-25 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name     Add language filter In Google search for each language defined in google search settings
// @description:en It takes the languages you have configured in your google search settings and adds them as a separated filter for each instead of adding just one filter for all.
// @version  2
// @grant    none
// @include     *.google.*
// @namespace https://greasyfork.org/users/320969
// @description It takes the languages you have configured in your google search settings and adds them as a separated filter for each instead of adding just one filter for all.
// ==/UserScript==
(function() {
  function addNewLangFilters() {
    let langItems = document.querySelectorAll('ul.hdtbU')[0].querySelectorAll('.hdtbItm')

    const primaryLang = langItems[1].querySelector('a') ? langItems[1] : langItems[0]

    let langList = Cookie.remember('google_search_langs', () => {

      const langKeys = primaryLang.id
        .replace(/lr_/g, '')
        .replace(/1/g, '')
        .split('|')

      const langTitles = primaryLang.textContent
        .replace('Search', '')
        .replace('pages', '')
        .split('and')
        .map(x => `Search ${x.trim()} pages`)

      if (langKeys.length !== langTitles.length) {
        return null
      }
      return langKeys
        .map((x, i) => [x, langTitles[i]])
        .reverse()
    })

    if (!langList) {
      return
    }

    // filter already selected language
    const lr = new URLSearchParams(document.location.href).get('lr')
    langList = langList.filter(ln => ln[0] !== lr)

    for (const [code, name] of langList) {
      const newLang = primaryLang.cloneNode(true)
      const newLink = newLang.querySelector('a')
      newLink.innerHTML = name
      const url = new URL(newLink.href)
      const qs = new URLSearchParams(url.search)
      qs.set('lr', code)
      url.search = qs.toString()
      newLink.href = url.href
      langItems[1].after(newLang)
    }
  }

  setTimeout(addNewLangFilters, 1000)
})()

class Cookie {
  static get(name) {
    const value = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)')
    return value ? JSON.parse(value[2]) : null
  }

  static set(name, value, days = 30) {
    const d = new Date
    d.setTime(d.getTime() + 24 * 60 * 60 * 1000 * days)
    document.cookie = name + '=' + JSON.stringify(value) + ';path=/;expires=' + d.toUTCString()
    return value
  }

  static delete(name) {
    Cookie.set(name, '', -1)
  }

  static remember(name, fn, days = 30) {
    return Cookie.get(name) || Cookie.set(name, fn(), days)
  }
}