Dav*_*vid 2 javascript google-chrome google-chrome-extension
从Chrome 53开始,不受信任的事件不再调用默认操作。https://developer.mozilla.org/zh-CN/docs/Web/API/Event/isTrusted
在Chrome 53之前,此JavaScript会添加一个interrobang?。
var e = document.createEvent('TextEvent');
e.initTextEvent('textInput',
true,
true,
null,
String.fromCharCode( 8253 ));
document.activeElement.dispatchEvent(e);
Run Code Online (Sandbox Code Playgroud)
在Chrome 53中,看看会发生什么:https://jsfiddle.net/dblume/2nfhrj1j/10/
由于使用createEvent()进行的事件是不受信任的事件,因此不会像在Chrome 52及之前的版本中那样由activeElement处理其数据。
从Chrome 53开始,我的Chrome扩展程序停止运行,因为它试图调度此类textInput事件。现在应该怎么办?
切换到document.execCommand该选项可在任何文本元素以及带有contenteditable="true"和生成信任"input"事件的任何元素中使用。文本插入在插入符号位置(如果有的话,则替换选择),就像由用户键入的一样。与TextEvent事件相比,唯一的缺点是"input"事件不包含插入的文本。
document.execCommand('insertText', false, String.fromCharCode(8253));
document.execCommand('insertHTML', false, '‽'); // the same
Run Code Online (Sandbox Code Playgroud)
https://jsfiddle.net/2nfhrj1j/22/