# Where's the call stack when debugging a content script?

**URL:** <https://discourse.mozilla.org/t/wheres-the-call-stack-when-debugging-a-content-script/66333>\
**Category:** Development\
**Created:** [August 25, 2020, 6:39am UTC](https://discourse.mozilla.org/t/wheres-the-call-stack-when-debugging-a-content-script/66333 "2020-08-25T06:39:53Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![nagle](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/nagle/32/77669_2.png) [@nagle](https://discourse.mozilla.org/u/nagle)\
**Post date:** [August 25, 2020, 6:39am UTC](https://discourse.mozilla.org/t/wheres-the-call-stack-when-debugging-a-content-script/66333/1 "2020-08-25T06:39:53Z")

</div>

![whereisstack](https://us1.discourse-cdn.com/flex001/uploads/mozilla/original/3X/d/7/d7d6b8e97f13708ffb2368f1845f258321326aeb.png)

Here I’m trying to debug an add-on content script which is doing something wrong on a specific web page. I can get the debugger going (which requires turning on badly documented settings). I can see the files in the debugger. I can set breakpoints. Execution stops at the breakpoints. The proper line in the code is boxed if I scroll to that line.

But there’s no call stack shown, ever. See picture. I can stop and start execution, and step forward, but no call stack. And, although the box showing the current line will move in the source window, if it moves out of the window the window does not scroll and there’s no indication of where execution is gone.

Yes, I have “remote debugging” turned on. FF 80.0-b8, dev version.

---

<div class="post-metadata">

**Author:** ![juraj.masiar](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/juraj.masiar/32/30587_2.png) [@juraj.masiar](https://discourse.mozilla.org/u/juraj.masiar)\
**Post date:** [August 25, 2020, 8:29am UTC](https://discourse.mozilla.org/t/wheres-the-call-stack-when-debugging-a-content-script/66333/2 "2020-08-25T08:29:46Z")

</div>

Something is wrong 🙂, it works fine in my Firefox 80.  
Try to use `web-ext`. It will run fresh Firefox with empty default profile. See if it will work there:

```auto
npm i -g web-ext
cd /my_extension_dir
web-ext run --firefox=firefoxdeveloperedition --pref=browser.ctrlTab.recentlyUsedOrder=false --pref=devtools.webconsole.timestampMessages=true --pref=signon.rememberSignons=true --start-url about:debugging#/runtime/this-firefox

```

---

<div class="post-metadata">

**Author:** ![nagle](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/nagle/32/77669_2.png) [@nagle](https://discourse.mozilla.org/u/nagle)\
**Post date:** [August 25, 2020, 4:58pm UTC](https://discourse.mozilla.org/t/wheres-the-call-stack-when-debugging-a-content-script/66333/3 "2020-08-25T16:58:57Z")

</div>

web-ext won’t build on Ubuntu 18.04 LTS. Node.js version problems.  
Besides, that’s just complicating the problem.

I’m already using a downloaded version of the developer Firefox with a nearly blank profile.

---

<div class="post-metadata">

**Author:** ![juraj.masiar](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/juraj.masiar/32/30587_2.png) [@juraj.masiar](https://discourse.mozilla.org/u/juraj.masiar)\
**Post date:** [August 25, 2020, 6:09pm UTC](https://discourse.mozilla.org/t/wheres-the-call-stack-when-debugging-a-content-script/66333/4 "2020-08-25T18:09:09Z")

</div>

So actually later today I’ve been debugging a background script and suddenly there was no Call Stack 😃. It works most of the times, but it’s far from perfect I guess…

But from my own experience I can suggest you to:

- open Browser Console (Ctrl + Shift + J) if you are looking for errors in your content script ([there is bug](https://bugzilla.mozilla.org/show_bug.cgi?id=1410932))
- when debugging gets broken or makes no sense, restart browser 😃
- if debugging is complex and keeps breaking, better to open Chrome (it’s a last resort but it’s so much more reliable)

---

<div class="post-metadata">

**Author:** ![nagle](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/nagle/32/77669_2.png) [@nagle](https://discourse.mozilla.org/u/nagle)\
**Post date:** [August 25, 2020, 6:29pm UTC](https://discourse.mozilla.org/t/wheres-the-call-stack-when-debugging-a-content-script/66333/5 "2020-08-25T18:29:32Z")

</div>

OK, you see the call stack disappearing, too. So there’s a real bug.

(I know how to find ordinary errors. I’m looking for a more subtle bug. This is an old, working add-on which needs some work to deal with a change in a page it’s working on, and for the first time I really need to step through with the debugger and see why the content script and the page content don’t get along.)

---

<div class="post-metadata">

**Author:** ![juraj.masiar](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/juraj.masiar/32/30587_2.png) [@juraj.masiar](https://discourse.mozilla.org/u/juraj.masiar)\
**Post date:** [August 25, 2020, 6:37pm UTC](https://discourse.mozilla.org/t/wheres-the-call-stack-when-debugging-a-content-script/66333/6 "2020-08-25T18:37:46Z")

</div>

Maybe few more tips:

- there is a [debugger](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/debugger) statement
- you can also print stack with `Error().stack`
- you can print “clickable” stack, just call (don’t await) this async function:

```auto
async function printStack() {
  throw 'print stack'; // throwing in async fn will not break caller YET it allows stack examination in Dev console with clickable files!
}

```

But yeah, I can feel you pain 🙂. When there are some crazy bugs it’s always a huge help to have a strong debugging tool.

---

<div class="post-metadata">

**Author:** ![nagle](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/nagle/32/77669_2.png) [@nagle](https://discourse.mozilla.org/u/nagle)\
**Post date:** [August 26, 2020, 12:02am UTC](https://discourse.mozilla.org/t/wheres-the-call-stack-when-debugging-a-content-script/66333/7 "2020-08-26T00:02:02Z")

</div>

![debuggercrashed](https://us1.discourse-cdn.com/flex001/uploads/mozilla/original/3X/b/d/bdb154b148ad21764bf846592cddb0717ac39ebe.png)

**Debugger crashed on content script.**

 ![callstackappears](https://us1.discourse-cdn.com/flex001/uploads/mozilla/original/3X/2/a/2aee8be8f5316ae7b49e542c7f033e526298e6b2.png)

Sometimes I can see the call stack, as above. Sometimes it doesn’t appear. Sometimes it goes away while stepping. I can set breakpoints and get stops, and can step from there, but no call stack.

Is this debugger just broken, or what?

---

<div class="post-metadata">

**Author:** ![nagle](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/nagle/32/77669_2.png) [@nagle](https://discourse.mozilla.org/u/nagle)\
**Post date:** [August 26, 2020, 1:44am UTC](https://discourse.mozilla.org/t/wheres-the-call-stack-when-debugging-a-content-script/66333/8 "2020-08-26T01:44:44Z")

</div>

> **[1661178 - Debugger will not show call stack when debugging content scripts](https://bugzilla.mozilla.org/show_bug.cgi?id=1661178)**
>
> UNCONFIRMED (nobody) in DevTools - Debugger. Last updated 2020-08-25.

---

<div class="post-metadata">

**Author:** ![juraj.masiar](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/juraj.masiar/32/30587_2.png) [@juraj.masiar](https://discourse.mozilla.org/u/juraj.masiar)\
**Post date:** [August 26, 2020, 6:33am UTC](https://discourse.mozilla.org/t/wheres-the-call-stack-when-debugging-a-content-script/66333/9 "2020-08-26T06:33:07Z")

</div>

I’ve seen the crash many times. Actually I’ve just managed to reproduce it 😃 just now. It’s when you pause on some line (in content script) and then your extension gets reloaded.

---

<div class="post-metadata">

**Author:** ![nagle](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/nagle/32/77669_2.png) [@nagle](https://discourse.mozilla.org/u/nagle)\
**Post date:** [August 30, 2020, 1:46am UTC](https://discourse.mozilla.org/t/wheres-the-call-stack-when-debugging-a-content-script/66333/10 "2020-08-30T01:46:15Z")

</div>

> [@juraj.masiar](#):
>
> if debugging is complex and keeps breaking, better to open Chrome (it’s a last resort but it’s so much more reliable)

I tried the WebExtension in Chromium. Works perfectly over there.

What I’m actually doing is working on my Ad Limiter, which you can get from the Mozillla add-on store. It gets ratings for sites (I have a server for that) and displays them on search results. My usual test is to search for “Hacker news”, a non ad-heavy topic. On Bing search result pages, the first search result often disappears. That’s not supposed to happen.

In the debugger, I see the page being updated properly, and a green checkmark appears next to the search result. That’s done internally in the DOM by wrapping the search result in a DIV with the checkmark icon and some other stuff, followed by the search result content. I see those DOM changes going in fine. The page displays accordingly. Then, after the add-on code has finished, something deletes the first search result, everything inside the LI. The page updates and that content disappears. It’s gone from the DOM.

So I’m trying to find what’s doing that. Something in Bing’s code? Something in my code? If I could step through in the debugger, this would be easy to find. I have console.log calls all through my code, and with them enabled, there’s nothing which seems likely to be the cause of the problem. There’s a thing for deleting some ads, but I have that turned off, logging for it turned on, and a debugger breakpoint to catch control going through there. Not happening.

This is starting to look like some obscure Firefox bug, especially since it doesn’t appear under Chrome. Any ideas?

Latest comment on the debugger bug is to try some new debugger in Nightly Firefox that might work.

---

<div class="post-metadata">

**Author:** ![jscher2000](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/jscher2000/32/28202_2.png) [@jscher2000](https://discourse.mozilla.org/u/jscher2000)\
**Post date:** [August 30, 2020, 6:55am UTC](https://discourse.mozilla.org/t/wheres-the-call-stack-when-debugging-a-content-script/66333/11 "2020-08-30T06:55:43Z")

</div>

As a side note, I’ve noticed problems manipulating the DOM of Bing results in my userscript. Stuff sometimes vanishes for no apparent reason. My guess is that Bing has some code that detects and removes unrecognized/alien tags.

[https://www.jeffersonscher.com/gm/google-hit-hider/](https://www.jeffersonscher.com/gm/google-hit-hider/)

---

<div class="post-metadata">

**Author:** ![juraj.masiar](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/juraj.masiar/32/30587_2.png) [@juraj.masiar](https://discourse.mozilla.org/u/juraj.masiar)\
**Post date:** [August 30, 2020, 6:56am UTC](https://discourse.mozilla.org/t/wheres-the-call-stack-when-debugging-a-content-script/66333/12 "2020-08-30T06:56:41Z")

</div>

You can actually debug DOM changes directly! Just right click on some element and from context menu click “Break on” / … . I think it works in both Firefox and Chrome. I’ve used it in Firefox some time ago when I needed to see what script was modifying some element.

---

<div class="post-metadata">

**Author:** ![nagle](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/nagle/32/77669_2.png) [@nagle](https://discourse.mozilla.org/u/nagle)\
**Post date:** [August 30, 2020, 10:09pm UTC](https://discourse.mozilla.org/t/wheres-the-call-stack-when-debugging-a-content-script/66333/13 "2020-08-30T22:09:33Z")

</div>

> [@juraj.masiar](#):
>
> You can actually debug DOM changes directly! Just right click on some element and from context menu click “Break on” / … .

That helped. I found the code in the Bing search result page that does a removeChild and is messing up my add-on’s page changes. The page sets a mutation observer to check if something else modifies the page. If there’s already a mutation observer (which there is, because the add-on set one), it logs a message of “CI.AdPrevention”, which presumably is sent to Bing HQ somewhere. I also found the removeChild that’s messing things up. Now I have to figure out what Bing is really trying to do.

But that’s it. It’s apparently something Bing put in to prevent people from messing with their search results.

Here’s part of Bing’s code:

```
function r(t) {
		var i = window.location.pathname,
			r;
		try {
			window.MutationObserver != undefined && (typeof MutationObserver != "function" || MutationObserver.toString().indexOf("[native code]") < 0) ? sj_log("CI.AdPrevention", "MutationObserver overrided", "true") : (window.MutationObserver || window.WebKitMutationObserver) && typeof MutationObserver == "function" ? i && (i.toUpperCase() === "/SEARCH" || i.toUpperCase() === "/" || i.toUpperCase() === "/MAPS" || i.toUpperCase() === "/MAPSPREVIEW") && (r = new MutationObserver(function (t) {
				var f, r, e, i;
				if (t)
					for (f = 0; f < t.length; f++)
						if (r = t[f].addedNodes, r && r.length)
							for (e = 0; e < r.length; e++) {
								i = r[e];
								switch (i.tagName) {
									case "IFRAME":
									case "IMG":
									case "SCRIPT":
									case "LINK":
										h(i);
										break;
									case "DIV":
									case "LI":
										c(i);
										break;
									case "OBJECT":
										i.type && i.type.indexOf("flash") >= 0 && n(i, "RN", i.outerHTML.substr(0, u));
										break;
									case "CENTER":
										n(i, "RN", i.outerHTML.substr(0, u));
										break;
									default:
										return
								}
							}
			}), r.observe(t, {
				childList: !0,
				subtree: !0
			})) : sj_log("CI.AdPrevention", "MutationObserver not available", "true")
		} catch (f) {
			sj_log("CI.AdPrevention", "error_creating_dom_observer", f.name)
		}
	}

```

---

<div class="post-metadata">

**Author:** ![juraj.masiar](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/juraj.masiar/32/30587_2.png) [@juraj.masiar](https://discourse.mozilla.org/u/juraj.masiar)\
**Post date:** [August 31, 2020, 7:20am UTC](https://discourse.mozilla.org/t/wheres-the-call-stack-when-debugging-a-content-script/66333/14 "2020-08-31T07:20:12Z")

</div>

> [@nagle](#):
>
> If there’s already a mutation observer (which there is, because the add-on set one), it logs a message of “CI.AdPrevention”,

Actually it’s just a feature detection - it’s checking if MutationObserver API is available in your browser.

Your content script lives in it’s own “bubble” and web-page can’t see it’s variables.  
Read this part for more info:

> **[Content scripts - Mozilla | MDN](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts#Content_script_environment)**
>
> A content script is a part of your extension that runs in the context of a web page. It can read and modify page content using the standard Web APIs. Content script behavior is similar to scripts that are part of a website, such as those loaded using...

From the code that you posted is seems that the observer reacts only to these elements: `"IFRAME", "IMG", "SCRIPT", "LINK", "DIV", "LI", "OBJECT", "CENTER"`.  
So all you have to do is avoid these and you should be fine 🙂.

And if you don’t feel like doing compromises, you should be fine if you use [Shadow DOM API](https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_shadow_DOM).

---

<div class="post-metadata">

**Author:** ![nagle](https://sea1.discourse-cdn.com/flex001/user_avatar/discourse.mozilla.org/nagle/32/77669_2.png) [@nagle](https://discourse.mozilla.org/u/nagle)\
**Post date:** [September 1, 2020, 1:56am UTC](https://discourse.mozilla.org/t/wheres-the-call-stack-when-debugging-a-content-script/66333/15 "2020-09-01T01:56:25Z")

</div>

Yes. I now have my add-on fixed. Instead of inserting \<DIV\> tags, I have it insert \<SPAN\> tags with “display: block;”. That does the same thing as a DIV. The anti ad blocker code in Bing search result pages does not try to modify the SPAN tags, and so it all works. Technically, this is not correct, because I now have a SPAN enclosing a block element, but at the DOM level, it seems that DIV and SPAN only differ in their “display” attribute.

If the anti ad blocker code gets more aggressive I may have to go to a shadow DOM, but it’s not necessary yet.

---

<div class="post-metadata">

**Author:** ![jamescobban](https://avatars.discourse-cdn.com/v4/letter/j/ac8455/32.png) [@jamescobban](https://discourse.mozilla.org/u/jamescobban)\
**Post date:** [October 20, 2023, 6:46am UTC](https://discourse.mozilla.org/t/wheres-the-call-stack-when-debugging-a-content-script/66333/16 "2023-10-20T06:46:50Z")

</div>

I am running Firefox 118.0.2 on Ubuntu 22.04 LTS. A function is being called with an invalid parameter value. But because THERE IS NO CALL STACK displayed by the debugger how the hell am I supposed to figure out where my function is being called from?

 ![image](https://us1.discourse-cdn.com/flex001/uploads/mozilla/original/3X/d/e/de3c61b33aebe83c406051396f2393d9adb2aa14.png)
