Does my account only work when refreshing a page or in an iframe, and not on the main page?

This simple code does not work on the Facebook page. This code only warns when I refresh the page . If I go to this page from my Facebook profile with the mouse; the script stops working.

There are no warnings or errors. :(
It seems like the whole script is not working until I update.

// ==UserScript== // @name Purge My Facebook // @namespace http://www.arda.com // @description test // @include https://*.facebook.com* // @include http://*.facebook.com* // @version 1 // ==/UserScript== window.setTimeout (isParent, 500); function isParent () { if (window.self == window.top) { alert ("main window"); } else window.setTimeout (isParent, 500); } 

I also tried this, by the way:

 // ==UserScript== // @name Purge My Facebook // @namespace http://www.arda.com // @description test // @include http://www.facebook.com/*/allactivity* // @include https://www.facebook.com/*/allactivity* // @version 1 // ==/UserScript== try{ if (window.self == window.top) alert(document.domain+"\nmain window"); else alert(document.domain+"\nin a iframe"); } catch(e){ alert(document.domain+"\nHad a problem:\n"+e); } 
+5
javascript ajax facebook userscripts
source share
1 answer

The problem is AJAX. When you enter a URL or refresh the page, your script works. When you click the Activity Log button, the script is not. Iframes are not a factor for the symptoms you report.

This is because clicking this link never loads a new page; it calls AJAX calls that replace part of the content, making it look like a new page, but it is not.

So, if you want your script to catch fire on the "new" "main pages", you should use methods like this answer .

In the case of Facebook, the only thing that usually changes is the reported URL (but without running hashchange!) And the contents of the #mainContainer div.

You should also @include use all FB pages, since pages like https://www.facebook.com/*/allactivity* are often accessed only through AJAX, so your script should be run on the previous page.

This script will solve the problem posed in your question:

 // ==UserScript== // @name Purge My Facebook // @namespace http://www.ardaterekeci.com // @description test // @include http://www.facebook.com/* // @include https://www.facebook.com/* // @version 1 // ==/UserScript== var pageURLCheckTimer = setInterval ( function () { if ( this.lastPathStr !== location.pathname || this.lastQueryStr !== location.search || this.lastPathStr === null || this.lastQueryStr === null ) { this.lastPathStr = location.pathname; this.lastQueryStr = location.search; gmMain (); } } , 222 ); function gmMain () { if (window.self === window.top) alert ('"New" main (top) page loaded.'); else alert ('"New" iframed page loaded.'); } 


However, since the page is heavily AJAXed, you will find that firing when the first page loads will almost always be too early.

The smart thing is to use waitForKeyElements on the part of the page that you really need. (which is not indicated in the question.)

Here is an example of using waitForKeyElements . Note that in order to use @require in Chrome, you must use Tampermonkey (which you should do anyway).

+4
source share

All Articles