Highlight current DOM element in Chrome Extension using DebuggerApi
I'm currently building an extension for chrome (I'm a beginner) and looking for some help to some one issue. The flow of the extension is the following:
- User activate the extension
- User click an icon in the extension panel to start the capture
- When the mouse cursor is over a DOM element it highlight it
- When the user click it gets the "selector" (unique identifier/path to the element)
After step 2 I attach a new Debugger instance to the tab. it seems like you can do this action either in background.js or content-script.js. Both work so my question is which one makes more sense. I'd say content-script because it doesn't interact directly with the browser but only with my extension. Am I right?
Second question is when using the DebuggerAPI I need to send command using the DevTools Protocol Viewer. I guess the command I must send to interact with my DOM element sit under this category (https://chromedevtools.github.io/devtools-protocol/tot/DOM). Most of the command requires a NodeId parameter. My question is how would I get this NodeId when the mouse cursor is over it. I have the following event in my content-script
chrome.runtime.onMessage.addListener(function(msg, sender){
if(msg == "togglePanel"){
togglePanel();
} else if (msg == "startCaptureElement") {
console.log("- Content-Script.js: Add Mouse Listener");
document.addEventListener('mouseover', captureEvent);
} else if (msg == "stopCaptureElement") {
console.log("- Content-Script.js: Remove Mouse Listener");
document.removeEventListener('mouseover', captureEvent);
}
});
function captureEvent(el) {
//console.log("- Content-Script.js: It's moving");
console.log(el);
chrome.runtime.sendMessage("highlightElement");
}
In my background.js script
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender);
if (request == "startCaptureElement") {
console.log("- Background.js: Attach the debugger");
chrome.debugger.attach({tabId: sender.tab.id}, "1.0");
chrome.tabs.sendMessage(sender.tab.id, "startCaptureElement");
} else if (request == "stopCaptureElement") {
console.log("- Background.js: Detach the debugger");
chrome.debugger.detach({tabId: sender.tab.id});
chrome.tabs.sendMessage(sender.tab.id, "stopCaptureElement");
} else if (request == "highlightElement") {
console.log("- Background.js: Highlight Element");
chrome.debugger.sendCommand({tabId: sender.tab.id}, "DOM.enable", {});
chrome.debugger.sendCommand({tabId: sender.tab.id}, "Overlay.inspectNodeRequested", {}, function(result) {
console.log(result);
});
}
}
);
I found the similar question here How to highlight elements in a Chrome Extension similar to how DevTools does it? but the code provided confused me a little bit.
Thanks for your help
javascript google-chrome-extension
|
show 2 more comments
I'm currently building an extension for chrome (I'm a beginner) and looking for some help to some one issue. The flow of the extension is the following:
- User activate the extension
- User click an icon in the extension panel to start the capture
- When the mouse cursor is over a DOM element it highlight it
- When the user click it gets the "selector" (unique identifier/path to the element)
After step 2 I attach a new Debugger instance to the tab. it seems like you can do this action either in background.js or content-script.js. Both work so my question is which one makes more sense. I'd say content-script because it doesn't interact directly with the browser but only with my extension. Am I right?
Second question is when using the DebuggerAPI I need to send command using the DevTools Protocol Viewer. I guess the command I must send to interact with my DOM element sit under this category (https://chromedevtools.github.io/devtools-protocol/tot/DOM). Most of the command requires a NodeId parameter. My question is how would I get this NodeId when the mouse cursor is over it. I have the following event in my content-script
chrome.runtime.onMessage.addListener(function(msg, sender){
if(msg == "togglePanel"){
togglePanel();
} else if (msg == "startCaptureElement") {
console.log("- Content-Script.js: Add Mouse Listener");
document.addEventListener('mouseover', captureEvent);
} else if (msg == "stopCaptureElement") {
console.log("- Content-Script.js: Remove Mouse Listener");
document.removeEventListener('mouseover', captureEvent);
}
});
function captureEvent(el) {
//console.log("- Content-Script.js: It's moving");
console.log(el);
chrome.runtime.sendMessage("highlightElement");
}
In my background.js script
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender);
if (request == "startCaptureElement") {
console.log("- Background.js: Attach the debugger");
chrome.debugger.attach({tabId: sender.tab.id}, "1.0");
chrome.tabs.sendMessage(sender.tab.id, "startCaptureElement");
} else if (request == "stopCaptureElement") {
console.log("- Background.js: Detach the debugger");
chrome.debugger.detach({tabId: sender.tab.id});
chrome.tabs.sendMessage(sender.tab.id, "stopCaptureElement");
} else if (request == "highlightElement") {
console.log("- Background.js: Highlight Element");
chrome.debugger.sendCommand({tabId: sender.tab.id}, "DOM.enable", {});
chrome.debugger.sendCommand({tabId: sender.tab.id}, "Overlay.inspectNodeRequested", {}, function(result) {
console.log(result);
});
}
}
);
I found the similar question here How to highlight elements in a Chrome Extension similar to how DevTools does it? but the code provided confused me a little bit.
Thanks for your help
javascript google-chrome-extension
What you are trying to do is far from trivial. Will need a pretty good understanding of the DOM to do it.
– charlietfl
Nov 18 at 23:34
Hi @charlietfl . Thanks for your input. When you say "Will need a pretty good understanding of the DOM to do it." do you mean the DOM of the page where the user starts the extension? It could be on any website...
– CyrillouPanam
Nov 18 at 23:55
DOM = Document Object Model. With regard to "highlighting elements"
– charlietfl
Nov 18 at 23:56
2
1) No, content scripts can't use chrome.debugger so that wasn't a content script if you were able to do it; 2) The command is likelyDOM.highlightNode
- to see the exact syntax try "Listening to the protocol" as described in the documentation.
– wOxxOm
Nov 19 at 4:24
Thanks a lot @wOxxOm it's extremely useful and I'm making some progress in understanding how it works. The method I need is "Overlay.inspectNodeRequested" that would return a nodeID similar to what happen when you right click and select "Inspect" in chrome. However I get this error in return: Unchecked runtime.lastError while running debugger.sendCommand: {"code":-32601,"message":"'Overlay.inspectNodeRequested' wasn't found"}
– CyrillouPanam
Nov 20 at 0:28
|
show 2 more comments
I'm currently building an extension for chrome (I'm a beginner) and looking for some help to some one issue. The flow of the extension is the following:
- User activate the extension
- User click an icon in the extension panel to start the capture
- When the mouse cursor is over a DOM element it highlight it
- When the user click it gets the "selector" (unique identifier/path to the element)
After step 2 I attach a new Debugger instance to the tab. it seems like you can do this action either in background.js or content-script.js. Both work so my question is which one makes more sense. I'd say content-script because it doesn't interact directly with the browser but only with my extension. Am I right?
Second question is when using the DebuggerAPI I need to send command using the DevTools Protocol Viewer. I guess the command I must send to interact with my DOM element sit under this category (https://chromedevtools.github.io/devtools-protocol/tot/DOM). Most of the command requires a NodeId parameter. My question is how would I get this NodeId when the mouse cursor is over it. I have the following event in my content-script
chrome.runtime.onMessage.addListener(function(msg, sender){
if(msg == "togglePanel"){
togglePanel();
} else if (msg == "startCaptureElement") {
console.log("- Content-Script.js: Add Mouse Listener");
document.addEventListener('mouseover', captureEvent);
} else if (msg == "stopCaptureElement") {
console.log("- Content-Script.js: Remove Mouse Listener");
document.removeEventListener('mouseover', captureEvent);
}
});
function captureEvent(el) {
//console.log("- Content-Script.js: It's moving");
console.log(el);
chrome.runtime.sendMessage("highlightElement");
}
In my background.js script
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender);
if (request == "startCaptureElement") {
console.log("- Background.js: Attach the debugger");
chrome.debugger.attach({tabId: sender.tab.id}, "1.0");
chrome.tabs.sendMessage(sender.tab.id, "startCaptureElement");
} else if (request == "stopCaptureElement") {
console.log("- Background.js: Detach the debugger");
chrome.debugger.detach({tabId: sender.tab.id});
chrome.tabs.sendMessage(sender.tab.id, "stopCaptureElement");
} else if (request == "highlightElement") {
console.log("- Background.js: Highlight Element");
chrome.debugger.sendCommand({tabId: sender.tab.id}, "DOM.enable", {});
chrome.debugger.sendCommand({tabId: sender.tab.id}, "Overlay.inspectNodeRequested", {}, function(result) {
console.log(result);
});
}
}
);
I found the similar question here How to highlight elements in a Chrome Extension similar to how DevTools does it? but the code provided confused me a little bit.
Thanks for your help
javascript google-chrome-extension
I'm currently building an extension for chrome (I'm a beginner) and looking for some help to some one issue. The flow of the extension is the following:
- User activate the extension
- User click an icon in the extension panel to start the capture
- When the mouse cursor is over a DOM element it highlight it
- When the user click it gets the "selector" (unique identifier/path to the element)
After step 2 I attach a new Debugger instance to the tab. it seems like you can do this action either in background.js or content-script.js. Both work so my question is which one makes more sense. I'd say content-script because it doesn't interact directly with the browser but only with my extension. Am I right?
Second question is when using the DebuggerAPI I need to send command using the DevTools Protocol Viewer. I guess the command I must send to interact with my DOM element sit under this category (https://chromedevtools.github.io/devtools-protocol/tot/DOM). Most of the command requires a NodeId parameter. My question is how would I get this NodeId when the mouse cursor is over it. I have the following event in my content-script
chrome.runtime.onMessage.addListener(function(msg, sender){
if(msg == "togglePanel"){
togglePanel();
} else if (msg == "startCaptureElement") {
console.log("- Content-Script.js: Add Mouse Listener");
document.addEventListener('mouseover', captureEvent);
} else if (msg == "stopCaptureElement") {
console.log("- Content-Script.js: Remove Mouse Listener");
document.removeEventListener('mouseover', captureEvent);
}
});
function captureEvent(el) {
//console.log("- Content-Script.js: It's moving");
console.log(el);
chrome.runtime.sendMessage("highlightElement");
}
In my background.js script
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender);
if (request == "startCaptureElement") {
console.log("- Background.js: Attach the debugger");
chrome.debugger.attach({tabId: sender.tab.id}, "1.0");
chrome.tabs.sendMessage(sender.tab.id, "startCaptureElement");
} else if (request == "stopCaptureElement") {
console.log("- Background.js: Detach the debugger");
chrome.debugger.detach({tabId: sender.tab.id});
chrome.tabs.sendMessage(sender.tab.id, "stopCaptureElement");
} else if (request == "highlightElement") {
console.log("- Background.js: Highlight Element");
chrome.debugger.sendCommand({tabId: sender.tab.id}, "DOM.enable", {});
chrome.debugger.sendCommand({tabId: sender.tab.id}, "Overlay.inspectNodeRequested", {}, function(result) {
console.log(result);
});
}
}
);
I found the similar question here How to highlight elements in a Chrome Extension similar to how DevTools does it? but the code provided confused me a little bit.
Thanks for your help
javascript google-chrome-extension
javascript google-chrome-extension
edited Nov 20 at 0:41
asked Nov 18 at 23:25
CyrillouPanam
276
276
What you are trying to do is far from trivial. Will need a pretty good understanding of the DOM to do it.
– charlietfl
Nov 18 at 23:34
Hi @charlietfl . Thanks for your input. When you say "Will need a pretty good understanding of the DOM to do it." do you mean the DOM of the page where the user starts the extension? It could be on any website...
– CyrillouPanam
Nov 18 at 23:55
DOM = Document Object Model. With regard to "highlighting elements"
– charlietfl
Nov 18 at 23:56
2
1) No, content scripts can't use chrome.debugger so that wasn't a content script if you were able to do it; 2) The command is likelyDOM.highlightNode
- to see the exact syntax try "Listening to the protocol" as described in the documentation.
– wOxxOm
Nov 19 at 4:24
Thanks a lot @wOxxOm it's extremely useful and I'm making some progress in understanding how it works. The method I need is "Overlay.inspectNodeRequested" that would return a nodeID similar to what happen when you right click and select "Inspect" in chrome. However I get this error in return: Unchecked runtime.lastError while running debugger.sendCommand: {"code":-32601,"message":"'Overlay.inspectNodeRequested' wasn't found"}
– CyrillouPanam
Nov 20 at 0:28
|
show 2 more comments
What you are trying to do is far from trivial. Will need a pretty good understanding of the DOM to do it.
– charlietfl
Nov 18 at 23:34
Hi @charlietfl . Thanks for your input. When you say "Will need a pretty good understanding of the DOM to do it." do you mean the DOM of the page where the user starts the extension? It could be on any website...
– CyrillouPanam
Nov 18 at 23:55
DOM = Document Object Model. With regard to "highlighting elements"
– charlietfl
Nov 18 at 23:56
2
1) No, content scripts can't use chrome.debugger so that wasn't a content script if you were able to do it; 2) The command is likelyDOM.highlightNode
- to see the exact syntax try "Listening to the protocol" as described in the documentation.
– wOxxOm
Nov 19 at 4:24
Thanks a lot @wOxxOm it's extremely useful and I'm making some progress in understanding how it works. The method I need is "Overlay.inspectNodeRequested" that would return a nodeID similar to what happen when you right click and select "Inspect" in chrome. However I get this error in return: Unchecked runtime.lastError while running debugger.sendCommand: {"code":-32601,"message":"'Overlay.inspectNodeRequested' wasn't found"}
– CyrillouPanam
Nov 20 at 0:28
What you are trying to do is far from trivial. Will need a pretty good understanding of the DOM to do it.
– charlietfl
Nov 18 at 23:34
What you are trying to do is far from trivial. Will need a pretty good understanding of the DOM to do it.
– charlietfl
Nov 18 at 23:34
Hi @charlietfl . Thanks for your input. When you say "Will need a pretty good understanding of the DOM to do it." do you mean the DOM of the page where the user starts the extension? It could be on any website...
– CyrillouPanam
Nov 18 at 23:55
Hi @charlietfl . Thanks for your input. When you say "Will need a pretty good understanding of the DOM to do it." do you mean the DOM of the page where the user starts the extension? It could be on any website...
– CyrillouPanam
Nov 18 at 23:55
DOM = Document Object Model. With regard to "highlighting elements"
– charlietfl
Nov 18 at 23:56
DOM = Document Object Model. With regard to "highlighting elements"
– charlietfl
Nov 18 at 23:56
2
2
1) No, content scripts can't use chrome.debugger so that wasn't a content script if you were able to do it; 2) The command is likely
DOM.highlightNode
- to see the exact syntax try "Listening to the protocol" as described in the documentation.– wOxxOm
Nov 19 at 4:24
1) No, content scripts can't use chrome.debugger so that wasn't a content script if you were able to do it; 2) The command is likely
DOM.highlightNode
- to see the exact syntax try "Listening to the protocol" as described in the documentation.– wOxxOm
Nov 19 at 4:24
Thanks a lot @wOxxOm it's extremely useful and I'm making some progress in understanding how it works. The method I need is "Overlay.inspectNodeRequested" that would return a nodeID similar to what happen when you right click and select "Inspect" in chrome. However I get this error in return: Unchecked runtime.lastError while running debugger.sendCommand: {"code":-32601,"message":"'Overlay.inspectNodeRequested' wasn't found"}
– CyrillouPanam
Nov 20 at 0:28
Thanks a lot @wOxxOm it's extremely useful and I'm making some progress in understanding how it works. The method I need is "Overlay.inspectNodeRequested" that would return a nodeID similar to what happen when you right click and select "Inspect" in chrome. However I get this error in return: Unchecked runtime.lastError while running debugger.sendCommand: {"code":-32601,"message":"'Overlay.inspectNodeRequested' wasn't found"}
– CyrillouPanam
Nov 20 at 0:28
|
show 2 more comments
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53366462%2fhighlight-current-dom-element-in-chrome-extension-using-debuggerapi%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53366462%2fhighlight-current-dom-element-in-chrome-extension-using-debuggerapi%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What you are trying to do is far from trivial. Will need a pretty good understanding of the DOM to do it.
– charlietfl
Nov 18 at 23:34
Hi @charlietfl . Thanks for your input. When you say "Will need a pretty good understanding of the DOM to do it." do you mean the DOM of the page where the user starts the extension? It could be on any website...
– CyrillouPanam
Nov 18 at 23:55
DOM = Document Object Model. With regard to "highlighting elements"
– charlietfl
Nov 18 at 23:56
2
1) No, content scripts can't use chrome.debugger so that wasn't a content script if you were able to do it; 2) The command is likely
DOM.highlightNode
- to see the exact syntax try "Listening to the protocol" as described in the documentation.– wOxxOm
Nov 19 at 4:24
Thanks a lot @wOxxOm it's extremely useful and I'm making some progress in understanding how it works. The method I need is "Overlay.inspectNodeRequested" that would return a nodeID similar to what happen when you right click and select "Inspect" in chrome. However I get this error in return: Unchecked runtime.lastError while running debugger.sendCommand: {"code":-32601,"message":"'Overlay.inspectNodeRequested' wasn't found"}
– CyrillouPanam
Nov 20 at 0:28