How to keep the content script alive in a tab even if user navigates to another page in chrome extension?

1,046 views
Skip to first unread message

Pankaj Shinde

unread,
Oct 27, 2016, 9:50:46 AM10/27/16
to Chromium-Extensions-Announce
Problem :
Whenever I click the 'record' button, it successfully captures both events on the active tab (the tab from which I had my last focus). But, when I click on any hyperlink on the page, then the page navigates to another page on the same tab. I am then not able to detect any of the events on the new page, even though they are in the same tab.

How can I keep the myScript (contentScript) alive after user navigates to other link in same chrome tab. I am stuck at this stage really bad.

Antony Sargent

unread,
Oct 27, 2016, 11:42:19 AM10/27/16
to Pankaj Shinde, Chromium-extensions

Content scripts always run in an isolated world js context for a frame, and in general when a tab navigates normally, eg by clicking on an anchor tag, the frame gets torn down and replaced by a new one. So there is no way to keep a content script's js context alive. The solution to this is to use chrome.storage (https://developer.chrome.com/extensions/storage) or messaging back to your event page, to keep track of any state that you need to persist beyond the navigation.


--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extensions+unsub...@chromium.org.
To post to this group, send email to chromium-extensions@chromium.org.
Visit this group at https://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/6d9aea22-c242-439d-a84d-023f2e1b7c25%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/d/optout.

Reply all
Reply to author
Forward
0 new messages