New issue
Advanced search Search tips

Issue 600162 link

Starred by 1 user

Issue metadata

Status: Archived
Owner:
Closed: Dec 5
Cc:
Components:
EstimatedDays: ----
NextAction: ----
OS: All
Pri: 3
Type: Feature



Sign in to add a comment

DevTools: Mechanism for injecting script (or snippet) into every frame?

Project Member Reported by rbyers@chromium.org, Apr 2 2016

Issue description

I'm trying to debug an issue where I know a site is calling addEventListener with some bad argument (3rd arg is object instead of boolean).  I'm trying to get devtools to break when this happens.  I've injected the following code on page load (by breaking on the first line of script in index.html):

var aeln = EventTarget.prototype.addEventListener
EventTarget.prototype.addEventListener = function(type, handler, capture) {
  if (typeof(capture) == "object")
    debugger;
  aeln.call(this, type, handler, capture);
}

But it's not being hit.  I suspect that's because the script in question is in one of the MANY iframes on the page.  I don't see any easy way to break when script first runs in a frame, so there's no way for me to get this injected.

Maybe devtools should have a feature (console API is fine) that injects some specified script into every frame as soon as it's created?
 
I did something similar before:

var match=String.prototype.match
String.prototype.match=function(...args){
   debugger
   return match.apply(this, args)
}

It would be nice if debug(builtin) e.g. debug(EventTarget.prototype.addEventListener) could break every addEventListener() callsite.


"injects some specified script into every frame as soon as it's created"

This reminds me of Content Scripts with "run_at": "document_start".

https://developer.chrome.com/extensions/content_scripts


Owner: kozyatinskiy@chromium.org
Status: Assigned (was: Untriaged)
Aren't these the passive event listeners you are adding? :)

What you should do is:

1) open devtools against that page
2) open devtools on those devtools (undock, click Ctrl+Shift+I)
3) in the console of that second instance type:
WebInspector.targetManager.mainTarget().pageAgent().reload(true /** ignore cache */, "alert(1)")

That would be evaluating "alert(1)" in each of the frames before the page scripts are executed. That's where you place your smart capture trap.

We can add UI for that, but so far we did not get too many requests for that.
Summary: DevTools: Mechanism for injecting script (or snippet) into every frame? (was: Mechanism for injecting script into every frame?)
Alternative manual way to break on script run is: go to Sources panel, open up Event Listener Breakpoints in the sidebar and check "Script First Statement" under Script category. Should help with this particular usecase.

We had a similar request for auto-running snippets in every created execution context, but it never got enough attention.

pfeldman@ Awesome, that's worked great - thanks!  Yes this is related to passive event listeners.

dgozman@ Oh cool, I've looked for a "Break on first script" option before but never thought to look here (usually end up just trying to find the first line to stick a breakpoint on).  Thank you!  But this breaks on the first line of EVERY script, right?  That's really too often to be useful in this case (where I need to stop only one per iframe).

Comment 7 by rbyers@chromium.org, Jun 15 2017

This "WebInspector.targetManager" technique isn't working for me anymore - WebInspector is not defined when I attach DevTools to DevTools.  I spent a few minutes searching code trying to find where these APIs now live but haven't succeeded yet.

kozyatinskiy/dgozman can you give me an updated version of the work-around in #4 please?
SDK.targetManager.mainTarget().pageAgent().reload(...) is the new magic.
Owner: kozy@chromium.org
Status: Archived (was: Assigned)
Bulk-closing low priority bugs / feature requests with no action plan.

Sign in to add a comment