javascript - How to add a domain-switching selector to International Google sites? -
i writing greasemonkey script change top-level domains of google.com.
the complete code below. testing code in dev tool, did display choosebox , button. but, when clicked button, page reloaded, did not switch language sites.
// ==userscript== // @name googlefieldexpress // @namespace http://example.gg/ // @version 1.0 // @description change field database // @match http://www.google.*/ // @match https:/www.google.*/ // @copyright 2013,matthew // ==/userscript== //database domains , 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">' (var = 0; < 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); })();
greenhand , mad @ these. how can redirect right domain , what's wrong code?
i want jump www.google.com www.google.gg or www.google.jp , on.
p.s. "als" element container of sentence "google.gg offered in: languages"
several issues:
http://www.google.*/
,https:/www.google.*/
invalid values@match
. use:// @include http://www.google.*/ // @include https://www.google.*/
jp
appears invalid domain. google appears useco.jp
.windows
not valid object or variable.by using
<form>
,<button>
default type, clicking "go!" submits form! overrides trying set location url.you need protocol (http:, etc.) when setting
location.
do not use
this
in global scope of userscript.this
vary markedly depending on: browser, sandbox state, possible iframe state, etc. usethis
inside functions , when sure refer to.the script needs error checking since sought nodes not present on google pages.
there several more minor issues -- i'll skip, since stack overflow not code-review site. see working script, below, , note different question's script.
// ==userscript== // @name googlefieldexpress // @namespace http://example.gg/ // @version 1.0 // @description change field database // @include http://www.google.*/ // @include https://www.google.*/ // ==/userscript== //database domains , variables var domains = ["gg", "co.jp"]; var current = location.host; // or location.href. case-sensitive 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); }
Comments
Post a Comment