Skip to content

Instantly share code, notes, and snippets.

@netmarkjp
Created February 10, 2020 00:57
Show Gist options
  • Save netmarkjp/55c7f7817469939f5d5e3f9a6373322b to your computer and use it in GitHub Desktop.
Save netmarkjp/55c7f7817469939f5d5e3f9a6373322b to your computer and use it in GitHub Desktop.
[UserScript] Swap google search language en / any
// ==UserScript==
// @name Swap google search language en / any
// @namespace https://netmark.jp/userscript/www.google.com
// @version 0.1
// @description switch search language en or all
// @author Toshiaki Baba
// @match https://www.google.com/search?*
// @grant all
// ==/UserScript==
function swapLRParam(){
let u;
u = new URL(location);
if (u.searchParams.has('lr')) {
// remove the param lr
u.search = u.search.replace(/&lr=[^&]*&/, '&');
u.search = u.search.replace(/&lr=[^&]*$/, '');
u.search = u.search.replace(/\?lr=[^&]*&/, '?');
} else {
u.search = u.search+'&lr=lang_en';
}
console.log(u.href);
// // replacing location is prohibited(behavior is overwrited by google's script
// failed: setTimeout(()=>{location.href = u.href;}, 800);
// failed: location.replace(u.href);
window.open(u.href, "_blank");
}
(function() {
'use strict';
let button;
let searchButton;
button = document.createElement("button");
button.textContent = "en/all";
// Append button next to the search button
searchButton = document.getElementsByTagName("button")[0];
button.setAttribute("class", searchButton.getAttribute("class"));
button.addEventListener("click", swapLRParam);
searchButton.parentElement.appendChild(button);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment