如何修改 Safari 上下文菜单扩展中的文本
我对 javascript 和编写扩展是全新的,所以我需要你的帮助。
我正在尝试创建一个 Safari 上下文菜单项,该菜单项将允许用户在选择输入窗口中的部分或全部文本(就像我在其中询问此问题的文本)时修改他输入的文本。
我的扩展程序显示在上下文菜单中,并且单击它会调用我的 global.html
文件,该文件又会调用我的结束脚本 inject.js< /code>,文件如下所示:
function handleMessage(event) {
var range = window.getSelection();//.getRangeAt(0);
console.log("range:"+range);
var selectionParentText = range.startContainer.textContent;
range.startContainer.textContent = selectionParentText.substring(0,range.startOffset) + "[b]" + range.toString() + "[/b]" + selectionParentText.substring(range.endOffset,selectionParentText.length);
}
safari.self.addEventListener("message", handleMessage, false);
日志显示我选择的文本的值,但第 4 行生成 TypeError: Result of expression 'range.startContainer' [undefined] is not an object.
错误。修改所选文本的正确方法是什么?
I'm brand new to javascript and writing extensions so I need your help.
I'm trying to create a Safari context menu item which will allow the user to modify text that he has entered when he selects some or all of the text in the input window (like the one I'm asking this question in).
I have it partially working in that my extension shows up in the context menu, and clicking it calls my global.html
file which in turn calls my End Script, inject.js
, file which looks like this:
function handleMessage(event) {
var range = window.getSelection();//.getRangeAt(0);
console.log("range:"+range);
var selectionParentText = range.startContainer.textContent;
range.startContainer.textContent = selectionParentText.substring(0,range.startOffset) + "[b]" + range.toString() + "[/b]" + selectionParentText.substring(range.endOffset,selectionParentText.length);
}
safari.self.addEventListener("message", handleMessage, false);
The log shows the value of the text I select but the 4th line generates a TypeError: Result of expression 'range.startContainer' [undefined] is not an object.
error. What is the proper way to modify the selected text?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)