Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/454.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何向国际谷歌网站添加域切换选择器?_Javascript_Internationalization_Greasemonkey_Html Select - Fatal编程技术网

Javascript 如何向国际谷歌网站添加域切换选择器?

Javascript 如何向国际谷歌网站添加域切换选择器?,javascript,internationalization,greasemonkey,html-select,Javascript,Internationalization,Greasemonkey,Html Select,我正在写一个Greasemonkey脚本来改变google.com的顶级域名 完整的代码如下。在dev工具中测试我的代码时,它确实显示了choosebox和按钮。但是,当我点击按钮时,页面只是重新加载,它没有切换语言站点 // ==UserScript== // @name GoogleFieldExpress // @namespace http://example.gg/ // @version 1.0 // @description to change field f

我正在写一个Greasemonkey脚本来改变google.com的顶级域名

完整的代码如下。在dev工具中测试我的代码时,它确实显示了choosebox和按钮。但是,当我点击按钮时,页面只是重新加载,它没有切换语言站点

// ==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);
})();
/==UserScript==
//@name GoogleFieldExpress
//@名称空间http://example.gg/
//@version 1.0
//@description更改数据库中的字段
//@匹配http://www.google.*/
//@match https:/www.google*/
//@版权所有2013,Matthew
//==/UserScript==
//数据库域和变量
var域=新数组;
域名=[“gg”,“jp”];
var current=“”;
if(typeof this.href==“未定义”){
当前=document.location.toString().toLowerCase();
}
否则{
当前=this.href.toString().toLowerCase();
}
内部变量\
\
\
选择下面的字段:
\
\ \ ' 对于(var i=0;i

我是一个新手,对这些很生气。 如何重定向到正确的域以及我的代码有什么问题

我想从跳到或等等

另外,“als”元素是“Google.gg以:语言提供”这句话的容器

  • http://www.google.*/
    https:/www.google.*/
    是。使用:

  • jp
    似乎是无效域。谷歌似乎使用了
    co.jp

  • windows
    不是有效的对象或变量

  • 通过使用默认类型的
    ,单击“go!”实际上提交了表单!这将覆盖尝试设置位置URL的操作

  • 设置
    位置时需要协议(http:,等等)。

  • 不要在用户脚本的全局范围内使用此<代码>此将因浏览器、沙盒状态、可能的iframe状态等而显著不同。仅在函数内部以及确定其引用内容时使用此

  • 脚本需要进行错误检查,因为搜索的节点不会出现在大多数Google页面上

  • 还有几个小问题——我将跳过,因为堆栈溢出不是真正的代码审查站点。请看下面的工作脚本,并注意它与问题脚本的不同之处

    // ==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);
    }
    
    /==UserScript==
    //@name GoogleFieldExpress
    //@名称空间http://example.gg/
    //@version 1.0
    //@description更改数据库中的字段
    //@包括http://www.google.*/
    //@包括https://www.google.*/
    //==/UserScript==
    //数据库域和变量
    var域=[“gg”,“co.jp”];
    var current=location.host;//或location.href。区分大小写是有原因的
    内部变量\
    \
    \
    选择下面的字段:
    \
    \ \ ' ; domains.forEach(函数(项){ 内部+=''+项目+''; } ); 内部+='\ 走\ \ \ '; //主代码 var lang=document.getElementById(“als”); 如果(朗){ lang.innerHTML+=内部; document.getElementById(“gmGoto2”).addEventListener(“单击”,函数)(){ 地点分配( location.protocol+'\/\/www.google' +document.getElementById(“gmSelect_项”).值 + '\/' ); },假); }
    几个问题:

  • http://www.google.*/
    https:/www.google.*/
    是。使用:

  • jp
    似乎是无效域。谷歌似乎使用了
    co.jp

  • windows
    不是有效的对象或变量

  • 通过使用默认类型的
    ,单击“go!”实际上提交了表单!这将覆盖尝试设置位置URL的操作

  • 设置
    位置时需要协议(http:,等等)。

  • 不要在用户脚本的全局范围内使用此<代码>此将因浏览器、沙盒状态、可能的iframe状态等而显著不同。仅在函数内部以及确定其引用内容时使用此

  • 脚本需要进行错误检查,因为搜索的节点不会出现在大多数Google页面上

  • 还有几个小问题——我将跳过,因为堆栈溢出不是真正的代码审查站点。请看下面的工作脚本,并注意它与问题脚本的不同之处

    // ==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);
    }
    
    /==UserScript==
    //@name GoogleFieldExpress
    //@名称空间http://example.gg/
    //@version 1.0
    //@description更改数据库中的字段
    //@包括http://www.google.*/
    //@包括https://www.google.*/
    //==/UserScript==
    //数据库域和变量
    var域=[“gg”,“co.jp”];
    var current=location.host;//或location.href。区分大小写是有原因的
    内部变量\
    \
    \
    选择下面的字段:
    \
    \ \ ' ; domains.forEach(函数(项){ 内部+=''+项目+''; } ); 内部+='\ 走\ \ \ '; //主代码 var lang=document.getElementById(“als”); 如果(朗){ lang.innerHTML+=内部; document.getElementById(“gmGoto2”).addEventListener(“单击”,函数)(){ 地点分配( location.protocol+'\/\/www.google'