1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550
| // ==UserScript== // @name GoogleSearch-Language_Region-Switching // @name:zh-CN Google搜索语言与地区切换 // @name:zh-TW Google搜索語言與地區切換 // @name:ja Google検索言語と地域切り替え // @description:zh-CN Google搜索结果语言和地区过滤 // @description:zh-TW Google搜索結果語言和地區過濾 // @description:ja Google検索結果の言語と地域フィルター // @description Google search result filter by language and region // @namespace GoogleSearch-Language_Region-Switching // @supportURL https://github.com/zhuzemin // @include https://www.google.*
vertical-align: middle; }
.lang-homepage-btn { height: 100%; margin-left: 8px; display: flex; align-items: center; justify-content: center; }
.lang-homepage-btn .lang-dropdown-container { height: 24px; margin: 0; }
.lang-selector-btn { background: none; border: none; cursor: pointer; display: flex; align-items: center; justify-content: center; padding: 0; width: 24px; height: 24px; outline: none; color: #5f6368; position: relative; }
.lang-selector-btn:hover { color: #1a73e8; }
.lang-selector-btn svg { width: 24px; height: 24px; display: block; }
.main-dropdown, .lang-dropdown, .region-dropdown { display: none; position: absolute; top: 100%; left: 50%; transform: translateX(-50%); z-index: 999; background: white; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.2); padding: 8px 0; min-width: 180px; margin-top: 6px; max-height: 400px; overflow-y: auto; }
.main-dropdown::before, .lang-dropdown::before, .region-dropdown::before { content: ''; position: absolute; top: -6px; left: 50%; transform: translateX(-50%); border-left: 6px solid transparent; border-right: 6px solid transparent; border-bottom: 6px solid white; }
.main-dropdown.show, .lang-dropdown.show, .region-dropdown.show { display: block; }
.menu-option, .lang-option, .region-option { display: flex !important; align-items: center !important; padding: 10px 16px !important; text-decoration: none !important; color: #202124 !important; font-size: 14px !important; text-align: left !important; transition: background-color 0.2s !important; font-family: 'Google Sans', Arial, sans-serif !important; cursor: pointer !important; }
.menu-option:hover, .lang-option:hover, .region-option:hover { background-color: #f1f3f4 !important; text-decoration: none !important; }
.menu-option.active, .lang-option.active, .region-option.active { color: #1a73e8 !important; font-weight: 500 !important; background-color: #f1f8ff !important; }
.back-option { display: flex !important; align-items: center !important; padding: 10px 16px !important; color: #5f6368 !important; font-size: 14px !important; border-bottom: 1px solid #e8eaed !important; margin-bottom: 6px !important; cursor: pointer !important; }
.back-option:hover { background-color: #f1f3f4 !important; }
.back-option .back-icon { margin-right: 8px !important; }
.menu-icon { display: inline-flex !important; align-items: center !important; justify-content: center !important; width: 20px !important; height: 20px !important; margin-right: 12px !important; color: #5f6368 !important; }
.region-icon { display: inline-block !important; width: 20px !important; margin-right: 12px !important; text-align: center !important; font-family: 'Google Sans', 'Roboto', sans-serif !important; }
.g-tooltip { position: absolute; background: #2d2d2d; color: white; padding: 6px 10px; border-radius: 4px; font-size: 14px; font-weight: 500; font-family: 'Google Sans', 'Roboto', 'Arial', sans-serif; white-space: nowrap; z-index: 1000; opacity: 0; visibility: hidden; transition: opacity 0.2s, visibility 0.2s; bottom: -38px; left: 50%; transform: translateX(-50%); pointer-events: none; letter-spacing: 0.25px; box-shadow: 0 1px 3px rgba(0,0,0,0.2); text-rendering: optimizeLegibility; -webkit-font-smoothing: antialiased; }
.g-tooltip::after { content: ''; position: absolute; top: -4px; left: 50%; transform: translateX(-50%) rotate(45deg); width: 8px; height: 8px; background: #2d2d2d; }
.lang-selector-btn:hover .g-tooltip { opacity: 1; visibility: visible; } `; document.head.appendChild(style);
// 创建按钮和下拉菜单元素 function createLanguageRegionSelectorElements(currentLang, currentRegion) { // 创建语言切换按钮容器 const container = document.createElement("div"); container.className = "lang-dropdown-container";
// 创建按钮和SVG图标 const button = document.createElement("button"); button.className = "lang-selector-btn"; button.innerHTML = ` <svg viewBox="0 0 512 512" xmlns="http://www.w3.org/2000/svg"> <style> .primary-blue { fill: #4285F4; } .secondary-blue { fill: #8AB4F8; } .accent-red { fill: #EA4335; } .light-blue { fill: #BBDEFB; } </style> <! <path class="primary-blue" d="M454.111,193.473c-3.617,3.21-7.4,6.252-11.391,9.095c-9.236,6.546-14.26,10.143-17.404,12.866 c9.216,20.58,14.875,43.094,15.996,66.857h-66.418h-17.61h-92.449V170.384h-17.671h-70.307c2.769-6.606,5.732-12.886,8.915-18.731 c9.522-17.564,20.753-31.484,32.724-40.786c0.901-0.7,1.802-1.321,2.703-1.975c5.479-1.028,11.064-1.749,16.696-2.296 c-0.327-3.737-0.554-7.487-0.554-11.257c0-8.348,0.814-16.503,2.322-24.417c-54.439,3.977-103.533,27.693-139.848,64.022 c-39.939,39.919-64.702,95.252-64.689,156.184c-0.014,60.932,24.75,116.265,64.689,156.184 c39.912,39.939,95.245,64.703,156.184,64.689c60.939,0.014,116.266-24.75,156.184-64.689 c39.938-39.918,64.695-95.251,64.689-156.184C476.879,256.1,468.658,222.935,454.111,193.473z"/> <! <path class="secondary-blue" d="M124.813,159.941c17.27-17.264,37.903-31.09,60.812-40.492c-10.918,14.133-20.213,31.404-27.76,50.936h-42.661 C118.287,166.794,121.463,163.284,124.813,159.941z"/> <path class="secondary-blue" d="M101.731,188.054h49.955c-8.708,28.134-13.86,60.165-14.634,94.238H70.688 C72.322,247.478,83.52,215.248,101.731,188.054z"/> <path class="secondary-blue" d="M94.504,382.422c-13.92-24.55-22.408-52.538-23.816-82.46h66.424 c0.674,29.435,4.624,57.275,11.317,82.46H94.504z"/> <path class="secondary-blue" d="M124.813,422.314c-6.886-6.887-13.2-14.334-18.938-22.221h47.833 c4.691,14.053,10.17,27.193,16.529,38.931c4.711,8.675,9.876,16.616,15.422,23.803 C162.737,453.424,142.097,439.584,124.813,422.314z"/> <path class="light-blue" d="M247.164,476.433c-8.835-0.414-17.496-1.475-25.958-3.07 c-0.908-0.654-1.808-1.274-2.71-1.968c-17.971-13.948-34.206-38.357-45.664-69.701c-0.186-0.52-0.353-1.081-0.547-1.602h74.879 V476.433z"/> <path class="light-blue" d="M247.164,382.422H166.68c-7-24.758-11.251-52.725-11.965-82.46h92.449V382.422z"/> <path class="light-blue" d="M247.164,282.292h-92.509c0.814-34.54,6.453-66.744,15.568-94.238h76.94V282.292z"/> <path class="light-blue" d="M264.835,299.963h92.509c-0.701,29.729-4.985,57.702-11.985,82.46h-80.524V299.963z"/> <path class="light-blue" d="M293.503,471.395c-0.894,0.694-1.802,1.314-2.71,1.968c-8.462,1.595-17.123,2.656-25.958,3.07v-76.34h74.898 c-3.964,11.044-8.488,21.28-13.506,30.509C316.705,448.165,305.474,462.079,293.503,471.395z"/> <path class="secondary-blue" d="M387.193,422.314c-17.283,17.264-37.91,31.097-60.825,40.506c11.691-15.135,21.574-33.792,29.388-55.04 c0.921-2.496,1.749-5.112,2.603-7.688h47.766C400.385,407.98,394.073,415.427,387.193,422.314z"/> <path class="secondary-blue" d="M417.495,382.422H363.69c6.673-25.165,10.584-53.051,11.257-82.46h66.365 C439.904,329.885,431.409,357.872,417.495,382.422z"/> <! <path class="accent-red" d="M313.762,173.741c22.468,15.949,27.434,19.746,35.341,34.833c6.253,11.945,16.39,32.885,16.39,32.885 c0.48,0.954,1.448,1.562,2.522,1.562c1.061,0,2.049-0.608,2.522-1.562c0,0,10.143-20.94,16.396-32.885 c7.894-15.088,12.866-18.884,35.341-34.833c24.63-17.47,41.086-45.911,41.086-78.402C463.359,42.688,420.665,0,368.014,0 c-52.658,0-95.332,42.688-95.332,95.339C272.682,127.83,289.125,156.27,313.762,173.741z"/> <! <path class="light-blue" d="M368.014,55.013c22.275,0,40.332,18.058,40.332,40.326c0,22.274-18.057,40.332-40.332,40.332 c-22.275,0-40.326-18.058-40.326-40.332C327.689,73.071,345.739,55.013,368.014,55.013z"/> </svg> <span class="g-tooltip">切换地区和语言</span> `;
// 创建主菜单下拉框 const mainDropdown = document.createElement("div"); mainDropdown.className = "main-dropdown";
// 添加两个主菜单项 const regionMenuOption = document.createElement("div"); regionMenuOption.className = "menu-option"; regionMenuOption.innerHTML = ` <span class="menu-icon">🌎</span> 选择地区 `;
const langMenuOption = document.createElement("div"); langMenuOption.className = "menu-option"; langMenuOption.innerHTML = ` <span class="menu-icon">🔤</span> 选择语言 `;
mainDropdown.appendChild(regionMenuOption); mainDropdown.appendChild(langMenuOption);
// 创建地区选择下拉菜单 const regionDropdown = document.createElement("div"); regionDropdown.className = "region-dropdown";
// 添加返回主菜单选项 const regionBackOption = document.createElement("div"); regionBackOption.className = "back-option"; regionBackOption.innerHTML = ` <span class="back-icon">←</span> 返回 `; regionDropdown.appendChild(regionBackOption);
// 添加地区选项 const regions = [ { code: "HK", label: "香港", flag: "🇭🇰" }, { code: "TW", label: "台湾", flag: "🇨🇳" }, { code: "JP", label: "日本", flag: "🇯🇵" }, { code: "SG", label: "新加坡", flag: "🇸🇬" }, { code: "US", label: "美国", flag: "🇺🇸" }, { code: "", label: "当前地区", flag: "🌐" }, ];
regions.forEach((region) => { const option = document.createElement("a"); option.className = "region-option";
// 添加当前选中地区的高亮 if ((region.code === "" && !currentRegion) || (region.code !== "" && currentRegion === region.code)) { option.classList.add("active"); }
option.innerHTML = `<span class="region-icon">${region.flag}</span>${region.label}`;
// 生成新URL const url = new URL(window.location.href); if (region.code === "") { url.searchParams.delete("gl"); } else { url.searchParams.set("gl", region.code); } option.href = url.toString();
regionDropdown.appendChild(option); });
// 创建语言选择下拉菜单 const langDropdown = document.createElement("div"); langDropdown.className = "lang-dropdown";
// 添加返回主菜单选项 const langBackOption = document.createElement("div"); langBackOption.className = "back-option"; langBackOption.innerHTML = ` <span class="back-icon">←</span> 返回 `; langDropdown.appendChild(langBackOption);
// 添加语言选项 const languages = [ { code: "en", label: "English" }, { code: "ja", label: "Japanese (日本語)" }, { code: "zh-TW", label: "Traditional Chinese (繁體中文)" }, { code: "zh-CN", label: "Simplified Chinese (简体中文)" }, { code: "zh-CN|lang_zh-TW", label: "All Chinese (中文)" }, { code: "", label: "All Languages" }, ];
languages.forEach((lang) => { const option = document.createElement("a"); option.className = "lang-option";
// 添加当前选中语言的高亮 if ((lang.code === "" && !currentLang) || (lang.code !== "" && currentLang === lang.code)) { option.classList.add("active"); }
option.textContent = lang.label;
// 处理URL,保留地区参数 const url = new URL(window.location.href); if (lang.code === "") { url.searchParams.delete("lr"); } else { url.searchParams.set("lr", "lang_" + lang.code); } option.href = url.toString();
langDropdown.appendChild(option); });
// 组装元素 container.appendChild(button); container.appendChild(mainDropdown); container.appendChild(regionDropdown); container.appendChild(langDropdown);
// 添加按钮点击事件,显示主菜单 button.addEventListener("click", function (e) { e.preventDefault(); e.stopPropagation(); mainDropdown.classList.toggle("show"); regionDropdown.classList.remove("show"); langDropdown.classList.remove("show"); });
// 添加地区菜单选项点击事件 regionMenuOption.addEventListener("click", function (e) { e.preventDefault(); e.stopPropagation(); mainDropdown.classList.remove("show"); regionDropdown.classList.add("show"); });
// 添加语言菜单选项点击事件 langMenuOption.addEventListener("click", function (e) { e.preventDefault(); e.stopPropagation(); mainDropdown.classList.remove("show"); langDropdown.classList.add("show"); });
// 添加返回按钮点击事件 regionBackOption.addEventListener("click", function (e) { e.preventDefault(); e.stopPropagation(); regionDropdown.classList.remove("show"); mainDropdown.classList.add("show"); });
langBackOption.addEventListener("click", function (e) { e.preventDefault(); e.stopPropagation(); langDropdown.classList.remove("show"); mainDropdown.classList.add("show"); });
// 点击下拉菜单区域内不会关闭菜单 mainDropdown.addEventListener("click", function (e) { e.stopPropagation(); });
regionDropdown.addEventListener("click", function (e) { // 允许链接点击通过,但阻止冒泡 if (e.target.tagName !== "A") { e.stopPropagation(); } });
langDropdown.addEventListener("click", function (e) { // 允许链接点击通过,但阻止冒泡 if (e.target.tagName !== "A") { e.stopPropagation(); } });
return container; }
// 初始化语言和地区选择器 function initLanguageRegionSelector() { // 获取当前URL中的语言和地区设置 const currentUrl = window.location.href; const url = new URL(currentUrl);
// 获取语言参数 let currentLang = ""; const langParam = url.searchParams.get("lr"); if (langParam) { const langMatch = langParam.match(/lang_([^&]+)/); if (langMatch) { currentLang = langMatch[1]; } }
// 获取地区参数 const currentRegion = url.searchParams.get("gl") || "";
// 创建选择器元素 const container = createLanguageRegionSelectorElements(currentLang, currentRegion);
// 判断是搜索结果页还是首页 if (window.location.href.includes("/search")) { // 搜索结果页 const searchBtn = document.querySelector("button.Tg7LZd"); if (!searchBtn) { setTimeout(initLanguageRegionSelector, 100); return; }
// 插入到搜索按钮前面 searchBtn.parentNode.insertBefore(container, searchBtn);
// 处理图片搜索和视频搜索链接(保持原始脚本的功能) let allLinks = document.querySelectorAll("a.hide-focus-ring"); for (let link of allLinks) { if (/(isch)|(vid)/.test(link.href)) { searchBtn.parentNode.insertBefore(link, searchBtn); } } } else { // 首页 - 使用提供的精确选择器 // 麦克风和相机所在的父容器 const rightIconsContainer = document.querySelector(".fM33ce.dRYYxd");
if (!rightIconsContainer) { setTimeout(initLanguageRegionSelector, 100); return; }
// 检查是否已经存在按钮,避免重复添加 if (rightIconsContainer.querySelector(".lang-homepage-btn")) { return; }
// 创建一个首页专用容器 const homepageBtn = document.createElement("div"); homepageBtn.className = "lang-homepage-btn"; homepageBtn.appendChild(container);
// 将按钮添加到右侧图标组的末尾 rightIconsContainer.appendChild(homepageBtn); }
// 点击页面其他区域关闭所有菜单 document.addEventListener("click", function () { document.querySelector(".main-dropdown")?.classList.remove("show"); document.querySelector(".region-dropdown")?.classList.remove("show"); document.querySelector(".lang-dropdown")?.classList.remove("show"); }); }
// 启动初始化并在DOM变化时重新检查 initLanguageRegionSelector();
// 添加MutationObserver以处理动态加载的元素 const observer = new MutationObserver(function (mutations) { // 检查是否存在语言切换按钮 const isHomePage = !window.location.href.includes("/search"); const selector = isHomePage ? ".lang-homepage-btn" : ".lang-dropdown-container";
if (!document.querySelector(selector)) { initLanguageRegionSelector(); } });
observer.observe(document.body, { childList: true, subtree: true });
|