admin管理员组文章数量:1399754
I try to get the DOM I select by ContextMenu
in Chrome Extension.
Code:
chrome.contextMenus.onClicked.addListener(function(info, tab){
// the info.selectionText just the text, don not contains html.
});
chrome.contextMenus.create({
title: "Demo",
contexts: ["selection"],
id: "demo"
});
but the info.selectionText don't contains the HTML DOM. Is there any way to get the selection dom in Chrome extension contextMenu?. Please suggest. thanks.
I try to get the DOM I select by ContextMenu
in Chrome Extension.
Code:
chrome.contextMenus.onClicked.addListener(function(info, tab){
// the info.selectionText just the text, don not contains html.
});
chrome.contextMenus.create({
title: "Demo",
contexts: ["selection"],
id: "demo"
});
but the info.selectionText don't contains the HTML DOM. Is there any way to get the selection dom in Chrome extension contextMenu?. Please suggest. thanks.
Share Improve this question asked Jan 29, 2015 at 9:31 kenticnykenticny 5352 gold badges9 silver badges21 bronze badges 1- Possible duplicate of Get page selection including HTML? – Zach Saucier Commented Oct 17, 2016 at 12:34
3 Answers
Reset to default 6To access the selection, you will need to inject a content script into the page.
There, you can call getSelection()
to get a Selection
object and play with ranges in it to extract the DOM you need.
// "activeTab" permission is sufficient for this:
chrome.contextMenus.onClicked.addListener(function(info, tab){
chrome.tabs.executeScript(tab.id, {file: "getDOM.js"})
});
getDOM.js:
var selection = document.getSelection();
// extract the information you need
// if needed, return it to the main script with messaging
You may want to take a look at Messaging docs.
If you want just selected text from context menu than you can do it by following code
function getClickHandler() {
return function(info, tab) {
// info.selectionText contain selected text when right clicking
console.log(info.selectionText);
};
};
/**
* Create a context menu which will only when text is selected.
*/
chrome.contextMenus.create({
"title" : "Get Text!",
"type" : "normal",
"contexts" : ["selection"],
"onclick" : getClickHandler()
});
How I solved it in v3 manifest:
chrome.contextMenus.onClicked.addListener(async (info, tab) => {
const tabId = tab?.id;
if (tabId && info.menuItemId === 'my menu item' && info.selectionText) {
chrome.scripting.executeScript(
{
func: () => {
const selection = window.getSelection();
const range = selection?.getRangeAt(0);
if (range) {
const clonedSelection = range.cloneContents();
const div = document.createElement('div');
div.appendChild(clonedSelection);
return div.innerHTML;
}
},
target: {
tabId,
},
},
(result) => {
const html = result[0].result;
// do something with `html`
},
);
}
});
This requires both scripting
and activeTab
permissions.
本文标签: javascriptGet Selection DOM in chrome extension contextmenuStack Overflow
版权声明:本文标题:javascript - Get Selection DOM in chrome extension contextmenu - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744232607a2596409.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论