Skip to content

Instantly share code, notes, and snippets.

@hexium310
Last active March 26, 2024 19:50
Show Gist options
  • Save hexium310/4382fac313926d4613fe9e510690baa3 to your computer and use it in GitHub Desktop.
Save hexium310/4382fac313926d4613fe9e510690baa3 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name 検索結果の言語切り替えるやつ
// @description 検索結果ページの言語を切り替えるボタンを表示します
// @namespace hexium310
// @version 1.1.0
// @author Hexin
// @match https://www.google.com/search*
// @resource MaterialIcons https://fonts.googleapis.com/icon?family=Material+Icons
// @grant GM_addStyle
// @grant GM_getResourceText
// @downloadURL https://gist.github.com/hexium310/4382fac313926d4613fe9e510690baa3/raw/change-google-search-language.user.js
// @updateURL https://gist.github.com/hexium310/4382fac313926d4613fe9e510690baa3/raw/change-google-search-language.user.js
// ==/UserScript==
(() => {
'use strict';
GM_addStyle(GM_getResourceText("MaterialIcons"));
const url = new URL(location.href);
const searchParams = url.searchParams;
if (searchParams.get('hl') === 'en') {
searchParams.set('hl', 'ja');
searchParams.set('gl', 'jp');
} else {
searchParams.set('hl', 'en');
searchParams.set('gl', 'us');
}
const element = document.querySelector('#searchform > :last-child > :last-child');
const outer = document.createElement('div');
outer.style.position = 'relative';
outer.style.alignSelf = 'center';
outer.setAttribute('aria-selected', 'false');
const translation = document.createElement('a');
translation.href= url.toString();
translation.style.paddingTop = '0';
translation.style.paddingBottom = '0';
const translationImage = document.createElement('span');
translationImage.classList.add('material-icons');
translationImage.innerText = 'translate';
translation.insertAdjacentElement('beforeend', translationImage);
outer.insertAdjacentElement('beforeend', translation);
element.insertAdjacentElement('beforebegin', outer);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment