有些注定了被遗忘的话,也就有的注定了属于沉静的自私.

扩展:Gadget-HotCat/中文输入法支持

来自森亮号航海见识 {SLboat Insight;}
跳转至: 导航搜索

想法

  • 支持中文输入法而不是第一个字符的时候就被踢出来

看起来需要

  • 处理第一个字符,第一次的时候

todo

  • 捕获更多可能的英文输入
  • 如果丢失焦点呢?

探索

  • hook大到这里的位置
ClipCapIt-130915-203630.PNG
  • 关联代码
                    if (!noSuggestions) {
                        text.onkeyup = function(evt) {
                            evt = evt || 
                            window.event || window.Event;
                            var key = evt.keyCode || 0;
                            if (key === 38 || key === 40 || key === 33 || key === 34) {
                                if (self.keyCount === 0)
                                    return self.processKey(evt);
                            } else {
                                if (key == 27) {
                                    if (!self.resetKeySelection()) {
                                        self.cancel();
                                        return;
                                    }
                                }
                                var dont_autocomplete = (key == 8 || key == 46 || key == 27);
                                if (self.engine && suggestionConfigs[self.engine] && suggestionConfigs[self.engine].temp && !dont_autocomplete) {
                                    self.engine = HotCat.suggestions;
                                }
                                self.state = CategoryEditor.CHANGE_PENDING;
                                window.setTimeout(function() {
                                    self.textchange(dont_autocomplete);
                                }, HotCat.suggest_delay);
                            }
                            return true;
                        };
                        text.onkeydown = function(evt) {
                            evt = evt || window.event || window.Event;
                            self.lastKey = evt.keyCode || 0;
                            self.keyCount = 0;
                            if (self.lastKey == 13)
                                return self.accept(evt);
                            return (self.lastKey == 27) ? evtKill(evt) : true;
                        };
                        text.onkeypress = function(evt) {
                            self.keyCount++;
                            return self.processKey(evt);
                        };
                    }
  • 锁定目标,看起来只要处理是否为229,然后关闭自动完成就可以了,可以通过lastkey来提取按下的key
                        text.onkeyup = function(evt) {
                            evt = evt || 
                            window.event || window.Event;
                            var key = evt.keyCode || 0;
                            if (key === 38 || key === 40 || key === 33 || key === 34) {
                                if (self.keyCount === 0)
                                    return self.processKey(evt);
                            } else {
                                if (key == 27) {
                                    if (!self.resetKeySelection()) {
                                        self.cancel();
                                        return;
                                    }
                                }
                                var dont_autocomplete = (key == 8 || key == 46 || key == 27);
                                if (self.engine && suggestionConfigs[self.engine] && suggestionConfigs[self.engine].temp && !dont_autocomplete) {
                                    self.engine = HotCat.suggestions;
                                }
                                self.state = CategoryEditor.CHANGE_PENDING;
                                window.setTimeout(function() {
                                    self.textchange(dont_autocomplete);
                                }, HotCat.suggest_delay);
                            }
                            return true;
                        };
  • 首个补丁,没有工作
              // https://bugzilla.mozilla.org/show_bug.cgi?id=524360
              var dont_autocomplete = (key == 8 || key == 46 || key == 27 || self.lastKey == 229); // BS, DEL, ESC, 以及输入法之中
              if (self.engine && suggestionConfigs[self.engine] && suggestionConfigs[self.engine].temp && !dont_autocomplete) {
                self.engine = HotCat.suggestions; // Reset to a search upon input
              }
  • 放到这里中文输入的东西全部不见了
        text.onkeydown =
          function (evt) {
            evt = evt || window.event || window.Event; // W3C, IE, Netscape
            self.lastKey = evt.keyCode || 0;
            self.keyCount = 0;
            // Handle return explicitly, to override the default form submission to be able to check for ctrl
            if (self.lastKey == 13) return self.accept (evt);
            // Inhibit default behavior of ESC (revert to last real input in FF: we do that ourselves)
            return (self.lastKey == 27 || self.lastKey == 229) ? evtKill(evt) : true; //放入这里处理的时候一切都会消失
          };
  • 另外一个截断,看起来也屏蔽了,但是有点彻底
        text.onkeyup =
          function (evt) {
            evt = evt || window.event || window.Event; // W3C, IE, Netscape
            var key = evt.keyCode || 0;
            if (key === 38 || key === 40 || key === 33 || key === 34) { // Up and down arrows, page up/down
              // In case a browser doesn't generate keypress events for arrow keys...
              if (self.keyCount === 0) return self.processKey (evt);
            } else {
              if (key == 27) { // ESC
                if (!self.resetKeySelection ()) {
                  // No undo of key selection: treat ESC as "cancel".
                  self.cancel ();
                  return;
                }
              }
			  return; //全部送回来了呢,这样可以截断全部的自动填写-以至于完全失去了这样的功能
              // Also do this for ESC as a workaround for Firefox bug 524360
              // https://bugzilla.mozilla.org/show_bug.cgi?id=524360
              var dont_autocomplete = (key == 8 || key == 46 || key == 27); // BS, DEL, ESC
              if (self.engine && suggestionConfigs[self.engine] && suggestionConfigs[self.engine].temp && !dont_autocomplete) {
                self.engine = HotCat.suggestions; // Reset to a search upon input
              }
  • 自动建议的产生看起来在这里
      if (noSuggestions) {
        // No Ajax: just make sure the list is hidden
        if (this.list) this.list.style.display = 'none';
        if (this.engineSelector) this.engineSelector.style.display = 'none';
        if (this.icon) this.icon.style.display = 'none';
        return;
      }		
      //if (v.length === 0) { this.showSuggestions([]); return; }  //这里产生了自动建议-如果默认那样没有被注释的话
      if (this.callbackObj) this.callbackObj.cancelled = true;
      var engineName  = suggestionConfigs[this.engine] ? this.engine : 'combined';
  • 当前在这里的想法
              if (key == 27) { // ESC
                if (!self.resetKeySelection ()) {
                  // No undo of key selection: treat ESC as "cancel".
                  self.cancel ();
                  return;
                }
              }
			  // 想法:在这里增加监测上次的按下是不是229,以及长度是不是0,是的话那就取消下面的工作
			  // 更优良的做法:监测是否输入完成了,对比内容?
              // Also do this for ESC as a workaround for Firefox bug 524360
              // https://bugzilla.mozilla.org/show_bug.cgi?id=524360
              var dont_autocomplete = (key == 8 || key == 46 || key == 27); // BS, DEL, ESC
              if (self.engine && suggestionConfigs[self.engine] && suggestionConfigs[self.engine].temp && !dont_autocomplete) {
                self.engine = HotCat.suggestions; // Reset to a search upon input
              }
              self.state = CategoryEditor.CHANGE_PENDING;
              window.setTimeout (function () {self.textchange (dont_autocomplete);}, HotCat.suggest_delay);
            }
            return true;
  • 新的调试方式,通过gadget里关闭hotcat,然后用源码输入开发者工具的方式来注入完成,这样变动得以刷新一次后即完成调试
  • 首次中文输入有效,只是还不能输出事件
              if (key == 27) { // ESC
                if (!self.resetKeySelection ()) {
                  // No undo of key selection: treat ESC as "cancel".
                  self.cancel ();
                  return;
                }
              }
			  if (self.lastKey==229) //如果是中文的?
			  {
				console.log("上次是中文的输入按下事件");
				  return;  //抛弃一切?
			  }
			  // 想法:在这里增加监测上次的按下是不是229,以及长度是不是0,是的话那就取消下面的工作
			  // 更优良的做法:监测是否输入完成了,对比内容?
              // Also do this for ESC as a workaround for Firefox bug 524360
              // https://bugzilla.mozilla.org/show_bug.cgi?id=524360
              var dont_autocomplete = (key == 8 || key == 46 || key == 27); // BS, DEL, ESC
              if (self.engine && suggestionConfigs[self.engine] && suggestionConfigs[self.engine].temp && !dont_autocomplete) {
                self.engine = HotCat.suggestions; // Reset to a search upon input
              }
              self.state = CategoryEditor.CHANGE_PENDING;
              window.setTimeout (function () {self.textchange (dont_autocomplete);}, HotCat.suggest_delay);
            }
  • 哇喔,更加优良的注入方式
ClipCapIt-130927-131139.PNG
扩展:Gadgets/调试js文件
  • 捕获上次输入内容,但看起来很没用
        text.onkeydown =
          function (evt) {
            evt = evt || window.event || window.Event; // W3C, IE, Netscape
            self.lastKey = evt.keyCode || 0;
            self.keyCount = 0;
            // Handle return explicitly, to override the default form submission to be able to check for ctrl
            if (self.lastKey == 13) return self.accept (evt);
			self.new_ime_input= false;//非全新输入
			//这里的this.value可以获得输入框的内容哦
			if (self.lastKey == 229) //如果是中文的输入呢
			{
				console.log("中文输入按下去事件");
				if (this.value.length==0) //还没有开始输入
				{
					self.new_ime_input=true; //全新的输入
				}
			}
			//结束-返回
  • 初版本最终补丁-补丁ver.a

补丁

ver.a

  • diff在这里
https://github.com/SLboat/Gadget-HotCat-Mediawiki/commit/1a143ad4c806a9991c89ddbe51e3a61f806c6b41
      if (!noSuggestions) {
        text.onkeyup =
          function (evt) {
            evt = evt || window.event || window.Event; // W3C, IE, Netscape
            var key = evt.keyCode || 0;
            if (key === 38 || key === 40 || key === 33 || key === 34) { // Up and down arrows, page up/down
              // In case a browser doesn't generate keypress events for arrow keys...
              if (self.keyCount === 0) return self.processKey (evt);
            } else {
              if (key == 27) { // ESC
                if (!self.resetKeySelection ()) {
                  // No undo of key selection: treat ESC as "cancel".
                  self.cancel ();
                  return;
                }
              }
              //SLboat-ime-fix-patch -> 森亮号的中文输入补丁
              /*  想法:在这里增加监测上次的按下是不是229,以及长度是不是0,是的话那就取消下面的工作
               * 更优良的做法:监测是否输入完成了,对比内容? 
               */
              /* TODO:@done捕获enter键(winodws里没有事件),@done捕获1-9数字键(选字?),捕获shift候选键(windows)
               * 记忆最后一次完成输入的词,这样就不再处理输入法了
              */
              var ime_char_input = (key == 32 || key == 13 || (key > 48 && key < 57)) ; //32-space键,13-Enter键,最通常的输出
              if (self.lastKey==229 && !ime_char_input ) //如果上次是中文,并且这次有激键数据
			  {
				  //console.log("抛弃含糊的中文事件");
				  return;  //抛弃一切?
			  }
              // Also do this for ESC as a workaround for Firefox bug 524360
              // https://bugzilla.mozilla.org/show_bug.cgi?id=524360
              var dont_autocomplete = (key == 8 || key == 46 || key == 27); // BS, DEL, ESC
              if (self.engine && suggestionConfigs[self.engine] && suggestionConfigs[self.engine].temp && !dont_autocomplete) {
                self.engine = HotCat.suggestions; // Reset to a search upon input
              }
              self.state = CategoryEditor.CHANGE_PENDING;
              window.setTimeout (function () {self.textchange (dont_autocomplete);}, HotCat.suggest_delay);
            }
            return true;
          };

相关见识

Html 输入法事件