0

I am working on a project which produces a Chrome extension. In my extension, I am using webRequest API to see inside a page websites. Then, I collect them and adding an array. I realized that, this array becomes empty after a time. In addition, it sometimes found 1 website sometimes 20 even 0. I mean it is not stable. What is the problem? How can I fix that? Here is my code:

function webRequestsChecking() {
  let includedUrls = []
  return new Promise((resolve, reject) => {
    try {
      chrome.webRequest.onBeforeRequest.addListener(
        (details) => {
          if (details !== undefined && details.parentFrameId !== -1) {
            if (!includedUrls.includes(details.url.split('/')[2]))
              includedUrls.push(details.url.split('/')[2])
          }
          console.log(includedUrls, 'setlemeden önce bg')
          setIncludedUrls(includedUrls)
        },
        { urls: ['<all_urls>'] },
        []
      )
    } catch (e) {
      reject(e)
    }
  })
}

Also, I am adding a picture for more understandable:

image

Ceren Keklik
  • 291
  • 2
  • 11
  • 1
    The background script unloads after 30 seconds so rework your code to use chrome.storage.local or [force the service worker to persist](https://stackoverflow.com/a/66618269). – wOxxOm Nov 24 '21 at 16:42
  • @wOxxOm However, I run the script when tab changed or tab updated. This problem occurs in 5 sec. Is it possible because of serviceworker? – Ceren Keklik Nov 25 '21 at 11:04
  • My comment already explains the problem and describes how to solve it. – wOxxOm Nov 25 '21 at 11:07

0 Answers0