Content Script in iFrame pauses if browser is minimized

169 views
Skip to first unread message

David Jung

unread,
May 9, 2022, 7:38:34 AM5/9/22
to Chromium Extensions
Greetings together!

I inject a content script using the following lines of code in my manifest V2:

  "content_scripts": [
    {
      "matches": ["file:///C:/Users/david/demo_iframe.html"],
      "all_frames": true,
      "js": ["js/content_script.js"]
    }
  ],

This code does work if the browser window is open or in the background. But if I minimize the browser window, the content script pauses the execution. 

To give a bit more details:
In my content script I am calling an async function to write text into a textbox.

Is this a known issue? Is there any way to solve that?

Thanks in advance,
David


Cuyler Stuwe

unread,
May 9, 2022, 9:08:35 AM5/9/22
to David Jung, Chromium Extensions
Does it pause execution, or does it stop triggering the timers/events/etc. that trigger your execution?

--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/8c21e264-54af-4b4c-862a-c4da7377d093n%40chromium.org.

David Jung

unread,
May 9, 2022, 9:10:53 AM5/9/22
to Chromium Extensions, salem...@gmail.com, Chromium Extensions, David Jung
It does stop the execution.

I am using a function to write a text, example "123456789".
If I minimize the browser window in after "123", wait a minute and open it again, it continues with "456789".

Cuyler Stuwe

unread,
May 9, 2022, 9:13:17 AM5/9/22
to David Jung, Chromium Extensions
And you’re saying it happens only in content scripts in iframes? Not for top-frame content scripts?

David Jung

unread,
May 9, 2022, 9:31:41 AM5/9/22
to Chromium Extensions, salem...@gmail.com, Chromium Extensions, David Jung
I saw the issue only with iFrames. Now I made a simple script to test top-frame content-scripts and I see the same behaviour. So the content scripts pause the execution if the browser is minimized (top-frame and iFrame-scripts)

Cuyler Stuwe

unread,
May 9, 2022, 9:33:39 AM5/9/22
to David Jung, Chromium Extensions
What does your script look like?

David Jung

unread,
May 9, 2022, 9:45:28 AM5/9/22
to Chromium Extensions, salem...@gmail.com, Chromium Extensions, David Jung
That's the function in the content script:
async function checkPageStatus() {
  console.log("[ALL] Checking page status...")
  await randomTimeout(2000)
  await setText("//input[@id='1']", "Test 1234567890", true)
}

randomTimeout:
async function randomTimeout(minTimeToWait: number) {
   // some number randomizing
   return new Promise((resolve) => setTimeout(resolve, msToWait + timeout))
}


setText:
async function setText(<myParams>): Promise<HTMLInputElement>{
  // Mouse movements using Input.dispatchMouseEvent
  // iterating through every character in the string and using the following method to insert the char
   document.execCommand("insertText", false, char)
}

Cuyler Stuwe

unread,
May 9, 2022, 10:22:44 AM5/9/22
to David Jung, Chromium Extensions
The entirety of the function would've been nice, but this is at least a start.

document.execCommand is weird.

Have you tried logging to console or doing anything else instead of document.execCommand as a sanity check to test your assertion that the script itself is paused?

David Jung

unread,
May 9, 2022, 10:41:18 AM5/9/22
to Chromium Extensions, salem...@gmail.com, Chromium Extensions, David Jung
Yes, I used console.log(char) instead of document.execCommand and I face the same issue.

Here is the full code:

export async function setText(
  xpath: string,
  text: string,
  simulateTyping: boolean,
  waitTime = 200,
  typeSpeed = 0,
): Promise<HTMLInputElement> {
  const element = getInput(xpath)

  if (element) {
    await setElementText(element, text, simulateTyping, waitTime, typeSpeed)
  }

  return element
}

async function setElementText(
  element: HTMLInputElement,
  text: string,
  simulateTyping: boolean,
  waitTime = 500,
  typeSpeed: number,
): Promise<void> {
  element.focus()
  element.select()
  document.execCommand("delete", false)
  element.focus()

  if (simulateTyping) {
    for (const char of text) {
      await typingTimeout(typeSpeed)
      //document.execCommand("insertText", false, char)
      console.log(char)
    }
  } else {
    if (!document.execCommand("insertText", false, text)) {
      // Fallback for Firefox: just replace the value
      element.value = text
    }
  }

  element.click()
  await randomTimeout(waitTime)
}

export async function typingTimeout(extra = 0): Promise<number> {
  const min = Number(200) - (Math.random() * (15 - 5) + 5)
  const max = Number(200) + (Math.random() * (15 - 5) + 5)
  const timeout = Math.random() * (max - min) + min + extra
  return new Promise((resolve) => setTimeout(resolve, timeout))
}

// random timeout with minimum time to wait
export async function randomTimeout(msToWait = 0): Promise<number> {
  const min = Number(200) - (Math.random() * (15 - 5) + 5)
  const max = Number(200) + (Math.random() * (15 - 5) + 5)
  const timeout = Math.random() * (max - min) + min
  return new Promise((resolve) => setTimeout(resolve, msToWait + timeout))
}

Cuyler Stuwe

unread,
May 9, 2022, 10:47:40 AM5/9/22
to David Jung, Chromium Extensions
Have you tried pasting this code into the console of a given webpage to see whether it behaves the same way outside of the context of a content script?

David Jung

unread,
May 9, 2022, 10:59:53 AM5/9/22
to Chromium Extensions, salem...@gmail.com, Chromium Extensions, David Jung
I tried using a light-version of my code and did not had the issue.

async function write() {
    for (const char of "Test123456789") {
      await new Promise(r => setTimeout(r, 500));
      console.log(char)
    }
}

await write()

David Jung

unread,
May 9, 2022, 11:12:01 AM5/9/22
to Chromium Extensions, David Jung, salem...@gmail.com, Chromium Extensions
Tried again with my code in the extension and changed the timeout parameter to 10 (milliseconds). With this way I saw, that the functions only execute, if the browser window is not minimized. So now I would say it really stops triggering events/timers etc.

hrg...@gmail.com

unread,
May 9, 2022, 5:06:30 PM5/9/22
to Chromium Extensions, davidj...@gmail.com, salem...@gmail.com, Chromium Extensions
Does the same occur if instead of minimizing the window you activate another tab in that window?

David Jung

unread,
May 10, 2022, 7:29:53 AM5/10/22
to Chromium Extensions, hrg...@gmail.com, David Jung, salem...@gmail.com, Chromium Extensions
Yes it does.

PhistucK

unread,
May 10, 2022, 7:50:54 AM5/10/22
to David Jung, Chromium Extensions, hrg...@gmail.com, salem...@gmail.com
Timers are significantly throttled in background/minimised tabs/windows. Since your code seems to use timers, they are being throttled.
I would expect them to be throttled to once per second, though, not to be completely paused, but implementation details vary, I guess.

PhistucK


hrg...@gmail.com

unread,
May 10, 2022, 9:32:37 AM5/10/22
to Chromium Extensions, PhistucK, Chromium Extensions, hrg...@gmail.com, salem...@gmail.com, davidj...@gmail.com
According to this document, throttling should occur in a tab after 5 minutes of being hidden, not immediately.

Have you checked if the issue also occurs with normal scripts in a page (not content scripts)?
For example, if you go to google.com and execute this script in DevTools:
count=0 ; setInterval(()=> document.querySelector('[name=q]').value = ++count, 1000) ;

Does the script stop executing too?

Alternatively, try creating a local html file and put a timer routing in it.

Cuyler Stuwe

unread,
May 10, 2022, 9:35:41 AM5/10/22
to hrg...@gmail.com, Chromium Extensions, PhistucK, davidj...@gmail.com
“Which OS?” is a pretty important question here too.
I’ve seen several platform-specific bugs with regard to determining visibility/focus over the years.

Cuyler Stuwe

unread,
May 10, 2022, 9:39:56 AM5/10/22
to hrg...@gmail.com, Chromium Extensions, PhistucK, davidj...@gmail.com
@hrg — I think that’s an old and/or incomplete document. For example, a minimized window throttles its timers to once every second as soon as it’s minimized.

Cuyler Stuwe

unread,
May 10, 2022, 9:43:36 AM5/10/22
to hrg...@gmail.com, Chromium Extensions, PhistucK, davidj...@gmail.com
This is the link I’ve referred to in the past for a more comprehensive overview of throttling behavior:


It doesn’t say anything about pausing execution entirely, though.
Reply all
Reply to author
Forward
0 new messages