chrome html显示口,在html弹出窗口中显示用户选定的文本,并在chrome extension

这篇博客介绍了如何创建一个Chrome扩展,该扩展在点击图标时触发background.js,打开Editor.html用于显示和编辑用户选中的错别字。content.js能够捕获用户选择的文本,并将其发送到常驻的Editor.html。当用户关闭Editor.html时,内容将保存到本地存储中。博客还讨论了如何将Editor.html和Editor.js分离以便在不同浏览器中复用。
摘要由CSDN通过智能技术生成

我有一个后台脚本,在点击chrome图标时触发:

background.js:

chrome.browserAction.onClicked.addListener(function (tab) {

console.log("chrome.browserAction.onClicked...." + tab);

var openSeleniumEditor = window.open(

chrome.extension.getURL("Editor.html"),

"Words Misspelled",

"width=600,height=400"

);

});

我的目标是收集文章中需要更正的所有字词。用户从当前页面中选择文本,内容脚本应该收集该文本并附加到编辑器中的列表中。与常规弹出窗口不同,Editor.html始终处于打开状态。当用户关闭此Editor.html时,Editor.html中的文本应保存到本地存储中,以便以后再引用。

content.js:

function getSelectedParagraphText() {

if (window.getSelection) {

selection = window.getSelection();

} else if (document.selection) {

selection = document.selection.createRange();

}

var parent = selection.anchorNode;

while (parent != null && parent.localName != "P") {

parent = parent.parentNode;

}

if (parent == null) {

return "";

} else {

return parent.innerText || parent.textContent;

}

}

Editor.html:

body { width: 300px; }

textarea { width: 250px; height: 100px;}

我想将Editor.html / Editor.js的代码保存在一个单独的文件夹中,以便我可以在每个浏览器中使用它。现在我如何调用/更新Editor.js或Editor.html以从content.js附加用户选择的文本?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值