Re: [crx] How to detect user leaving current page in the content script

1,856 views
Skip to first unread message

PhistucK

unread,
Sep 1, 2012, 3:11:07 AM9/1/12
to Jay Jay, chromium-...@chromium.org
I do not really think it is supposed to change anything, but maybe try to change the "run_at" value to "document_start".
Also, try using vanilla JavaScript -
function listen(e)
{
 alert(e.type);
}
window.addEventListener("unload", listen, false);
window.addEventListener("beforeunload", listen, false);

Do any of these suggestions work?

PhistucK



On Thu, Aug 30, 2012 at 11:10 PM, Jay Jay <jc10...@gmail.com> wrote:

Hi All

I am writing a Chrome Extension which needs to detect the user is leaving current page either by closing Chrome, closing the tab or navigating to other page.  I have tried to bind “unload” and “onbeforunload” in my content script. But it seems none of them works.  Can anyone give me a hint?

Thanks

My manifest.js is listed as following

{

  "name": "My First Extension",

  "version": "1.0",

  "manifest_version": 2,

  "description": "The first extension",

  "browser_action": {

    "default_icon": "icon.png",

    "default_popup": "popup.html"

  },

  "permissions": [

    "http://api.flickr.com/",

    "http://www.yahoo.com/"

  ],

 

  "content_scripts":[

  {

            "matches": ["http://www.yahoo.com/*"],

            "js": ["jquery-1.7.2.min.js", "myscript.js"],

            "run_at": "document_end"

  }

  ]

}

 

 

 

Myscript.js is listed as following

 

alert("hello from myscript.js");

$(window).beforeunload(function(){

            alert("In the beforeunload");

});

 

$(window).unload(function(){

            alert("In the unload");

});

--
You received this message because you are subscribed to the Google Groups "Chromium-extensions" group.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msg/chromium-extensions/-/lznqc_c5pcEJ.
To post to this group, send email to chromium-...@chromium.org.
To unsubscribe from this group, send email to chromium-extens...@chromium.org.
For more options, visit this group at http://groups.google.com/a/chromium.org/group/chromium-extensions/?hl=en.

Scott Fujan

unread,
Sep 1, 2012, 3:23:19 AM9/1/12
to PhistucK, Jay Jay, chromium-...@chromium.org
I do not believe that unload events can be stopped with anything such as alert(). beforeunload is useful only to pop up the built in "stay on page or leave page" dialog. I am unsure if you can message on unload (because it is asynchronous). When I need to achieve close detection I usually use a chrome.tabs listener or open a port connection and wait for a disconnect.
Reply all
Reply to author
Forward
0 new messages