A quick function to replace the selected text (in Firefox). Accepts:
替换所选文本的快速功能(在Firefox中)。 接受:
- string to replace the selection, or 字符串来替换选择,或者
- callback function that takes the selected text and returns a string to replace the selection带有所选文本并返回字符串以替换所选内容的回调函数
function replaceSelection(t) {
if (typeof t === 'function') {
t = t(window.getSelection().toString());
}
var range = window.getSelection().getRangeAt(0);
range.deleteContents();
range.insertNode(document.createTextNode(t));
}
Usage:
用法:
1. Replace selected text with "dude" replaceSelection('dude');
2. Uppercase the selected text replaceSelection(function(s){return s.toUpperCase()});
3. Lowercase the selected text replaceSelection(function(s){return s.toLowerCase()});
1.将所选文本替换为“ dude” replaceSelection('dude');
2.大写所选文本replaceSelection(function(s){return s.toUpperCase()});
3.小写所选文本replaceSelection(function(s){return s.toLowerCase()});
Luckily I'm so close (and yet so far) to finishing this book, otherwise I won't be able to help myself but create a whole toolbar of bookmarklets for "power" Google doc users...
幸运的是,我已经很接近(到目前为止)完成本书,否则我将无法为自己服务,而是为“强大”的Google文档用户创建了一个完整的书签小工具栏...
Tell your friends about this post on Facebook and Twitter
在Facebook和Twitter上告诉您的朋友有关此帖子的信息