Why am I not able to view event listeners added by a Chrome extension in DevTools' Event Listeners tab? - google-chrome-extension

I am using Chrome 101 in Windows 10 and have a Chrome extension installed which does an addEventListener("mouseover"..) on every element on the page. I am unable to view these events in the Event Listeners tab in DevTools. This seems to be a bug according to this question. The page shows a workaround. However, the workaround doesn't show the event listener for me.
I switched the console to the extension's context. For several selected different elements and doing a getEventListeners($0), I always get an empty object as shown. I am not sure if this option got broken or what.
Are there any other ways to display these types of event listeners? The mouseover does work on the page and if I put a breakpoint on this event, the debugger stops in the extension's code (along with code from the page).

Related

How to inject content_script only if the devtools panel is open?

I am trying to create a Chrome extension that adds a panel to the DevTools console.
The extension will have permission to interact with all urls, so to respect my users, I want to ensure that nothing is running when the devtools panel is closed.
The only thing the DevTool panel needs from the attached page (and all of its iframes) is to listen to a custom event dispatched on the respective window (could also be a postMessage)
window.addEventListener('my-custom-event', e => console.log(e.detail))
I understand that you cannot attach listeners directly from the DevTools panel onto the page/frames and you need a content_script to shuttle the messages forward (as described here).
The issue is, the content_script is always appended to the page regardless of whether the panel is open or not. It sets a listener and basically doesn't run unless the listener transfers a port to communicate on.
I would feel irresponsible knowing that my content script runs on every page, potentially impacting the performance of that page so I'd like them to only be attached to the page when the devtools panel is open and the page reloaded.
Ideally I would like to implement a "recording" button which, only when enabled, the content_script is added to the page. This is to avoid noise when doing performance profiling.
So my question is:
Can you inject content_script only when the DevTools panel is open and
Are you able to programmatically toggle content_script injection?

Is it ok to repeatedly add event listener in the event pages for chrome extension?

I am learning the Event Pages for Chrome extension and according to the documentation, the scripts will only be loaded when needed. Then I find that the Google Mail Checker's event page script will add the event listener:
// Some declarations
chrome.browserAction.onClicked.addListener(goToInbox);
// ...
And I write an event script:
chrome.tabs.create({url: 'https://www.google.com'});
function onClickListener() {
chrome.tabs.create({url: 'https://www.bing.com'});
}
chrome.browserAction.onClicked.addListener(onClickListener);
After I reloaded my extension, a new tab of google.com is opened as expected. Seconds later the process of my extension is gone in Chrome's task manager, and I clicked the extension icon. Then, both google.com and bing.com are opened! So I learn that this entire script will be loaded again.
Now look back to the script of Google Mail Checker. The listener will be added repeatedly once the script is loaded, so my question is: is it ok to add listener repeatedly? If the listener's behavior will change from A to B when the script is loaded, which one will be fired on the second load, A or B?
In fact your question is logically impossible, since the documentation has stated that the event listener will only exist in the context of the event page, that means it will be automatically removed once the event page is unloaded. So to some degree, it's ok to add listener repeatedly, although in fact you are adding it only once.
Because the listeners themselves only exist in the context of the event page, you must use addListener each time the event page loads
And Chrome also states that in the first line of best practices when using event pages
Register to receive any events your extension is interested in each time the event page is loaded. The event page will be loaded once for each new version of your extension. After that it will only be loaded to deliver events you have registered for. This generally means that your event listeners should be added at the top level scope of the event page, otherwise they may not be available when the event page reloads.

Howto send a message from content-script to inactive popup, chrome extension

I want to log text on any webpage (using content-script to handle selection) into a database that is popup's resource in order to collect text in one place.
what i am trying
I create a database in popup page and try to manage it from content-script though popup is not active (not opened) by using chrome messaging but cannot make the popup receives any message from content-script.
I'm not sure about using messaging to solve this problem.
Is there any better solution?
A content script cannot send a message to an invisible popup, because the popup's context is inactive (closed) when it's hidden.
There are several solutions to your problem.
Option 1: No message passing, use storage events
If your "database" is in fact a simple key-value store, switch to the chrome.storage API. This API is available to the Content script and the popup, and comes with an event to notify you of value changes.
Example:
// Get notified of changes (in the popup?)
chrome.storage.onChanged.addListener(function(changes, areaName) {
// Do whatever you want with the changes.
});
// Initialization of the popup (print initial information?)
chrome.storage.local.get({keyName: 'defaultValue'}, function(items) {
// Do something with items.keyName
});
// Content script, storage (remember document title?)
chrome.storage.local.set({keyName: document.title});
Option 2: Pass messages to the background/event page
The popup and the background / event page share the same process. Any database tied to the popup is also available to the background page, and vice versa. A high-level overview of this method:
Content script sends a message to the background page.
The background page stores the value in the database
If the popup is open, update the popup's view.
If the popup is opened (so it was closed before), it should read the database (either directly, or by reading data from the background page using chrome.runtime.getBackgroundPage) and handle the results.
I've provided the code corresponding to this flow in this answer.

Focus is not going inside EventHandler

I had created a simple web part with one label, and one button. I wnat that when i click the button the label content will change.
But when i debugg i found that the focus is not going inside the Button Click Event function.
When i click the button the debugger will again start with page load.
Is there any Autofocus for Button.
Please help me to resolve my problem.
Thanks :)
I think that you're confused about what happens on the client (within the browser) and what happens on the server (in your ASP.NET code).
Most events on server-side controls, like the ASP.NET button, are handled server-side, through a mechanism called postback. The generated code on the browser causes a form submit to the Web server, where the page and its controls are re-created. The form data is parsed and the button control generates (in your case) a Click event for your server-side code. This code regenerates a new HTML page which will replace the old one.
Fortunately for you in this case, the ASP.NET button has a property "OnClientClick" where you can insert JavaScript code to do whatever you want (like change the label text) directly in the browser.
See MSDN for the docs on the property and an example of using both Click and OnClientClick.
Note that this is not related to SharePoint, this is a pure ASP.NET issue.

Accessing browser events in chrome extension - DOM events in chrome extension

I am writing a chrome plugin in which I would like to receive events such as "load", "unload" of window and page. However, I'm not getting any concrete clue to start with.
Can anyone tell me how to capture DOM event in plugin?
Does Chrome support this feature?
Thank you.
You would need Content Scripts and you just add normal browser events to the JavaScript you inject as you stated in your question. Every page will have that content script and it will listen on those events.

Resources