我正在编写一个 Greasemonkey 脚本来更改 google.com 的顶级域。
完整代码如下。在开发工具中测试我的代码,它确实显示了选择框和按钮。但是,当我单击该按钮时,页面只是重新加载,它没有切换语言站点。
// ==UserScript==
// @name GoogleFieldExpress
// @namespace http://example.gg/
// @version 1.0
// @description to change field from database
// @match http://www.google.*/
// @match https:/www.google.*/
// @copyright 2013,Matthew
// ==/UserScript==
//database domains and varibles
var domains=new Array;
domains=["gg","jp"];
var current="";
if (typeof this.href === "undefined") {
current = document.location.toString().toLowerCase();
}
else {
current = this.href.toString().toLowerCase();
}
var inner='\
<center>\
<strong>\
Choose field below: <br />\
</strong>\
<form id="choose" >\
<select id="select_item">'
for (var i = 0; i < domains.length; i++) {
inner+='<option value="'+domains[i]+'"\\>'+domains[i]+'</option>';
};
inner+='</select>\
<button id="goto2">go!</button>\
</form>\
</center>\
'
//functions
function getLanguageElement(){
var lang=document.getElementById("als");
return lang;
}
function appendHTML(element,html){
element.innerHTML+=html;
}
//main process
(function(){
appendHTML(getLanguageElement(),inner);
document.getElementById("goto2").addEventListener("click",function(){windows.location=new String('www.google.')+(document.getElementById("select_item").value)},false);
})();
我是个新手,对这些真的很生气。
如何重定向到正确的域以及我的代码有什么问题?
我想从 www.google.com 跳转至www.google.gg或www.google.jp等等。
附注“als”元素是“Google.gg提供的语言:语言”这句话的容器
最佳答案
几个问题:
http://www.google.*/
和https:/www.google.*/
是invalid values for@match
。使用:// @include http://www.google.*/ // @include https://www.google.*/
jp
似乎是一个无效的域。 Google 似乎使用co.jp
.windows
不是有效的对象或变量。通过使用
<form>
和一个<button>
使用默认类型,单击“go!”实际上提交了表格!这会覆盖尝试设置位置 URL。设置
location.
时需要协议(protocol)(http:等)请勿使用
this
在用户脚本的全局范围内。this
将根据以下因素显着变化:浏览器、沙箱状态、可能的 iframe 状态等。使用this
仅在函数内部并且仅当您确定它将引用什么时。该脚本需要进行错误检查,因为大多数 Google 页面上都不会出现所查找的节点。
还有几个小问题 - 我将跳过这些问题,因为 Stack Overflow 并不是真正的代码审查网站。只需查看下面的工作脚本,并注意它与问题脚本的不同之处。
// ==UserScript==
// @name GoogleFieldExpress
// @namespace http://example.gg/
// @version 1.0
// @description to change field from database
// @include http://www.google.*/
// @include https://www.google.*/
// ==/UserScript==
//database domains and variables
var domains = ["gg", "co.jp"];
var current = location.host; // or location.href. Case-sensitive for a reason
var inner = '\
<center>\
<strong>\
Choose field below: <br />\
</strong>\
<form id="gmChoose">\
<select id="gmSelect_item">'
;
domains.forEach ( function (item) {
inner += '<option value="' + item + '"\\>' + item + '</option>';
} );
inner += '</select>\
<button id="gmGoto2" type="button">go!</button>\
</form>\
</center>\
';
//main code
var lang = document.getElementById ("als");
if (lang) {
lang.innerHTML += inner;
document.getElementById ("gmGoto2").addEventListener ("click", function () {
location.assign (
location.protocol + '\/\/www.google.'
+ document.getElementById ("gmSelect_item").value
+ '\/'
);
}, false);
}
关于javascript - 如何向国际 Google 网站添加域切换选择器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16339279/