// ==UserScript==
// @name Google Search English Filter
// @name:ja Google Search English Filter
// @name:zh-CN Google Search English Filter
// @description Add "Search English pages" option to the language filter on Google search Tools.
// @description:ja Google検索のツールで選べる絞り込み言語として、英語を追加します。
// @description:zh-CN 作为可以通过Google搜索工具选择的缩小语言,追加英语。
// @namespace knoa.jp
// @include https://www.google.*/search?*
// @version 1.0.2
// @grant none
// ==/UserScript==
(function(){
const SCRIPTID = 'GoogleSearchEnglishFilter';
const SCRIPTNAME = 'Google Search English Filter';
const DEBUG = false;/*
[update] 1.0.2
fixed @include.
[bug]
[todo]
[possible]
[memo]
https://www.google.com/search?q=google&client=firefox-b&sxsrf=ACYBGNTaF1aCsCLcgnQOwwDAo3nGoELowQ:1577943675296&source=lnt&tbs=lr:lang_1ja&lr=lang_ja&sa=X&ved=2ahUKEwif0PahmuTmAhWhGaYKHRCLDE0QpwV6BAgKEBk
https://www.google.com/search?q=test&hl=zh-CN&sxsrf=ACYBGNQ3oa8YIfHamy9rqBV9t5530dg6Nw:1577946432840&source=lnt&tbs=lr:lang_1zh-CN%7Clang_1zh-TW&lr=lang_zh-CN%7Clang_zh-TW&sa=X&ved=2ahUKEwiIhOrEpOTmAhVME6YKHWwjBeoQpwV6BAgLEBk
*/
if(window === top && console.time) console.time(SCRIPTID);
const RESET = 'GoogleSearchEnglishFilter_RESET';
const LANGUAGES = [
/* If you edited LANGUAGES, you should search "GoogleSearchEnglishFilter_RESET" on Google to apply your update */
/* https://www.google.com/search?q=GoogleSearchEnglishFilter_RESET */
{code: 'en', label: 'Search English pages', value: 'lang_en'},
//{code: 'ja', label: 'Search Japanese pages', value: 'lang_ja'},
//{code: 'fr', label: 'Search French pages', value: 'lang_fr'},
//{code: 'ru', label: 'Search Russian pages', value: 'lang_ru'},
//{code: 'es', label: 'Search Spanish pages', value: 'lang_es'},
//{code: 'ar', label: 'Search Arabic pages', value: 'lang_ar'},
//{code: 'zh-CN_zh-TW', label: 'Search Chinese (Simplified) and Chinese (Traditional) pages', value: 'lang_zh-CN%7Clang_zh-TW'},
//{code: 'zh-CN', label: 'Search Chinese (Simplified) pages', value: 'lang_zh-CN'},
//{code: 'zh-TW', label: 'Search Chinese (Traditional) pages', value: 'lang_zh-TW'},
];
const LANGUAGEQUERY = /(\?|&)(lr)=([^&]+)/;
const RETRY = 10;
let site = {
targets: {
languageList: () => $('#lr_', li => li.parentNode),
},
get: {
language: (li) => {
let a = li.querySelector('a[href]'), url = a ? a.href : location.href;
let match = url.match(LANGUAGEQUERY);
if(match === null) return log('LANGUAGEQUERY doesn\'t match.', url);
return {
code: match[3].replace(/lang_/g, '').replace(/%7C/g, '_'),
label: li.textContent,
value: match[3],
};
},
listItem: (languageList, language) => {
let a = languageList.querySelector('a[href]');
if(a === null) return log('a[href] doesn\'t exist.');
let url = [a.href, location.href].find(href => LANGUAGEQUERY.test(href));
if(url === undefined) return log('URL doesn\'t match.');
let li = a.parentNode.cloneNode(true), lia = li.querySelector('a[href]');
li.id = SCRIPTID + '-' + language.code;
lia.href = url.replace(LANGUAGEQUERY, `$1$2=${language.value}`);
lia.textContent = language.label;
return li;
}
},
is: {
reset: () => location.href.includes(RESET),
},
};
let html, elements = {}, timers = {}, sizes = {};
let languages = [];
let core = {
initialize: function(){
html = document.documentElement;
html.classList.add(SCRIPTID);
core.ready();
core.addStyle();
},
ready: function(){
core.getTargets(site.targets, RETRY).then(() => {
log("I'm ready.");
core.readLanguages();
core.getLanguages();
core.addLanguages();
});
},
readLanguages: function(){
if(site.is.reset()){
languages = LANGUAGES;
alert(`${SCRIPTNAME} has reset.`);
}else{
languages = Storage.read('languages') || LANGUAGES;
}
},
getLanguages: function(){
let languageList = elements.languageList;
/* add dataset for each list items */
Array.from(languageList.children).forEach((li, i) => {
if(i === 0) return;/*any language*/
let language = site.get.language(li);
li.dataset.code = language.code;
li.dataset.label = language.label;
li.dataset.value = language.value;
/* get default languages */
if(languages.find(l => l.code === language.code)) return;
languages.splice(i - 1, 0, language);/*keep the order of the languages*/
});
/* get and update localized labels */
languages.forEach(language => {
let li = Array.from(languageList.children).find(li => li.dataset.code === language.code);
if(li) language.label = li.dataset.label;
});
Storage.save('languages', languages);
},
addLanguages: function(){
let languageList = elements.languageList;
languages.forEach((language, i) => {
if(Array.from(languageList.children).some(li => li.dataset.code === language.code)) return;
let li = site.get.listItem(languageList, language);
languageList.insertBefore(li, languageList.children[i + 1]);
});
},
getTargets: function(targets, retry = 0){
const get = function(resolve, reject, retry){
for(let i = 0, keys = Object.keys(targets), key; key = keys[i]; i++){
let selected = targets[key]();
if(selected){
if(selected.length) selected.forEach((s) => s.dataset.selector = key);
else selected.dataset.selector = key;
elements[key] = selected;
}else{
if(--retry < 0) return reject(log(`Not found: ${key}, I give up.`));
log(`Not found: ${key}, retrying... (left ${retry})`);
return setTimeout(get, 1000, resolve, reject, retry);
}
}
resolve();
};
return new Promise(function(resolve, reject){
get(resolve, reject, retry);
});
},
addStyle: function(name = 'style'){
if(core.html[name] === undefined) return;
let style = createElement(core.html[name]());
document.head.appendChild(style);
if(elements[name] && elements[name].isConnected) document.head.removeChild(elements[name]);
elements[name] = style;
},
html: {
style: () => `
<style type="text/css">
#id{
color: red;
}
</style>
`,
},
};
const setTimeout = window.setTimeout, clearTimeout = window.clearTimeout, setInterval = window.setInterval, clearInterval = window.clearInterval, requestAnimationFrame = window.requestAnimationFrame;
const alert = window.alert, confirm = window.confirm, getComputedStyle = window.getComputedStyle, fetch = window.fetch;
if(!('isConnected' in Node.prototype)) Object.defineProperty(Node.prototype, 'isConnected', {get: function(){return document.contains(this)}});
class Storage{
static key(key){
return (SCRIPTID) ? (SCRIPTID + '-' + key) : key;
}
static save(key, value, expire = null){
key = Storage.key(key);
localStorage[key] = JSON.stringify({
value: value,
saved: Date.now(),
expire: expire,
});
}
static read(key){
key = Storage.key(key);
if(localStorage[key] === undefined) return undefined;
let data = JSON.parse(localStorage[key]);
if(data.value === undefined) return data;
if(data.expire === undefined) return data;
if(data.expire === null) return data.value;
if(data.expire < Date.now()) return localStorage.removeItem(key);
return data.value;
}
static delete(key){
key = Storage.key(key);
delete localStorage.removeItem(key);
}
static saved(key){
key = Storage.key(key);
if(localStorage[key] === undefined) return undefined;
let data = JSON.parse(localStorage[key]);
if(data.saved) return data.saved;
else return undefined;
}
}
const $ = function(s, f){
let target = document.querySelector(s);
if(target === null) return null;
return f ? f(target) : target;
};
const $$ = function(s){return document.querySelectorAll(s)};
const createElement = function(html = '<span></span>'){
let outer = document.createElement('div');
outer.innerHTML = html;
return outer.firstElementChild;
};
const observe = function(element, callback, options = {childList: true, attributes: false, characterData: false, subtree: false}){
let observer = new MutationObserver(callback.bind(element));
observer.observe(element, options);
return observer;
};
const log = function(){
if(!DEBUG) return;
let l = log.last = log.now || new Date(), n = log.now = new Date();
let error = new Error(), line = log.format.getLine(error), callers = log.format.getCallers(error);
//console.log(error.stack);
console.log(
(SCRIPTID || '') + ':',
/* 00:00:00.000 */ n.toLocaleTimeString() + '.' + n.getTime().toString().slice(-3),
/* +0.000s */ '+' + ((n-l)/1000).toFixed(3) + 's',
/* :00 */ ':' + line,
/* caller.caller */ (callers[2] ? callers[2] + '() => ' : '') +
/* caller */ (callers[1] || '') + '()',
...arguments
);
};
log.formats = [{
name: 'Firefox Scratchpad',
detector: /MARKER@Scratchpad/,
getLine: (e) => e.stack.split('\n')[1].match(/([0-9]+):[0-9]+$/)[1],
getCallers: (e) => e.stack.match(/^[^@]*(?=@)/gm),
}, {
name: 'Firefox Console',
detector: /MARKER@debugger/,
getLine: (e) => e.stack.split('\n')[1].match(/([0-9]+):[0-9]+$/)[1],
getCallers: (e) => e.stack.match(/^[^@]*(?=@)/gm),
}, {
name: 'Firefox Greasemonkey 3',
detector: /\/gm_scripts\//,
getLine: (e) => e.stack.split('\n')[1].match(/([0-9]+):[0-9]+$/)[1],
getCallers: (e) => e.stack.match(/^[^@]*(?=@)/gm),
}, {
name: 'Firefox Greasemonkey 4+',
detector: /MARKER@user-script:/,
getLine: (e) => e.stack.split('\n')[1].match(/([0-9]+):[0-9]+$/)[1] - 500,
getCallers: (e) => e.stack.match(/^[^@]*(?=@)/gm),
}, {
name: 'Firefox Tampermonkey',
detector: /MARKER@moz-extension:/,
getLine: (e) => e.stack.split('\n')[1].match(/([0-9]+):[0-9]+$/)[1] - 6,
getCallers: (e) => e.stack.match(/^[^@]*(?=@)/gm),
}, {
name: 'Chrome Console',
detector: /at MARKER \(<anonymous>/,
getLine: (e) => e.stack.split('\n')[2].match(/([0-9]+):[0-9]+\)?$/)[1],
getCallers: (e) => e.stack.match(/[^ ]+(?= \(<anonymous>)/gm),
}, {
name: 'Chrome Tampermonkey',
detector: /at MARKER \(chrome-extension:.*?\/userscript.html\?id=/,
getLine: (e) => e.stack.split('\n')[2].match(/([0-9]+):[0-9]+\)?$/)[1] - 6,
getCallers: (e) => e.stack.match(/[^ ]+(?= \(chrome-extension:)/gm),
}, {
name: 'Chrome Extension',
detector: /at MARKER \(chrome-extension:/,
getLine: (e) => e.stack.split('\n')[2].match(/([0-9]+):[0-9]+\)?$/)[1],
getCallers: (e) => e.stack.match(/[^ ]+(?= \(chrome-extension:)/gm),
}, {
name: 'Edge Console',
detector: /at MARKER \(eval/,
getLine: (e) => e.stack.split('\n')[2].match(/([0-9]+):[0-9]+\)$/)[1],
getCallers: (e) => e.stack.match(/[^ ]+(?= \(eval)/gm),
}, {
name: 'Edge Tampermonkey',
detector: /at MARKER \(Function/,
getLine: (e) => e.stack.split('\n')[2].match(/([0-9]+):[0-9]+\)$/)[1] - 4,
getCallers: (e) => e.stack.match(/[^ ]+(?= \(Function)/gm),
}, {
name: 'Safari',
detector: /^MARKER$/m,
getLine: (e) => 0,/*e.lineが用意されているが最終呼び出し位置のみ*/
getCallers: (e) => e.stack.split('\n'),
}, {
name: 'Default',
detector: /./,
getLine: (e) => 0,
getCallers: (e) => [],
}];
log.format = log.formats.find(function MARKER(f){
if(!f.detector.test(new Error().stack)) return false;
//console.log('////', f.name, 'wants', 0/*line*/, '\n' + new Error().stack);
return true;
});
core.initialize();
if(window === top && console.timeEnd) console.timeEnd(SCRIPTID);
})();