7

This simple code doesn't work on a Facebook page. This code only alerts when I refresh the page. If I go to that page from my Facebook profile, with my mouse; The script stops working.

There are no alerts or errors. :(
It looks like the whole script doesn't work until I refresh.

// ==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, among other things:

// ==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);
}
Arda
  • 470
  • 5
  • 10
  • This throws up an alert on the main window, no problem. It doesn't fire on iframes because they are not covered by the `@include` directives. Chaining the `setTimeout` will just cause the browser to freeze/crash on iframes anyway. Don't do that! How, ***exactly***, is this script not working in "Main Window"? – Brock Adams Jul 25 '13 at 21:04
  • nope.. it doesnt work on main window. this was my test script.. i read all of your answers and forums on userscripts.org.. i am pulling my hair right now. – Arda Jul 25 '13 at 21:09
  • I tested it; it does work on the main window. What are you calling the main window? Give: (1) Exact test URL, (2) Expected behavior, (3) Actual behavior. – Brock Adams Jul 25 '13 at 21:17
  • i just started to write my code when it fails i deleted all of it and started to new one today.. i will run this script on activity log section however if i go there with mouse clicks it doesnt work.. if i type address to adressbar or if i refresh that page it works. i am just trying to get elements from mainwindow thats all – Arda Jul 25 '13 at 21:19
  • That's a different problem! One that has been addressed many times. The easiest solutions involve using `waitForKeyElements`. You can see an example, at [this question](http://stackoverflow.com/q/14024120/331508). To use that kind of code on Chrome, you must run it with the Tampermonkey extension -- which is a good idea, regardless. – Brock Adams Jul 25 '13 at 22:18
  • as i said Brock i tried everything (waitForKeyElements) too.. it doesnt work beacuse my script only works on iframe.. thats my problem.. i have run my script on main page :( – Arda Jul 25 '13 at 22:24
  • 1
    No, both your scripts work 100% on the main frame. What you are describing is an AJAX problem, not an iframe problem, and `waitForKeyElements` will work for that, absolutely. **Provide those 3 items I requested before!** They're, also, what should have been in the question from the start. – Brock Adams Jul 25 '13 at 22:43
  • can u give me complete code of what you are using as script.. believe me it doesnt work on my computer right now.. thats why i am pulling my hair. – Arda Jul 25 '13 at 22:46
  • @BrockAdams can we discuss in chat so we can post solution here... – Arda Jul 25 '13 at 23:04
  • **Provide the 3 items I list [here](http://stackoverflow.com/questions/17862394/my-userscript-only-works-in-iframe-not-in-main-window?noredirect=1#comment-26090961).** I tested the code in your question, exactly, on a random facebook-user page. – Brock Adams Jul 26 '13 at 01:06
  • @BrockAdams updated u can check.. thank you very much for your help – Arda Jul 26 '13 at 01:39

2 Answers2

7

The problem is an AJAX one. When you type the URL, or refresh the page, your script works. When you click on your "Activity Log" button, the script doesn't. Iframes are not a factor for the symptoms you report.

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

So, if you want your script to fire on "new" "main pages", you must utilize techniques like those in this answer.

In the case of Facebook, the only thing that typically changes is the reported URL (but without triggering hashchange!), and the content of the #mainContainer div.

You must also @include all FB pages because pages like https://www.facebook.com/*/allactivity*are often only reached via AJAX, so your script needed to be running 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'll find that firing when the page first loads will almost always be too early.

The smart thing to do is to use waitForKeyElements on the specific part of the page that you really care about. (Which was not indicated in the question.)

Here's an example of using waitForKeyElements. Note that to use @require in Chrome, you have to be using Tampermonkey (which you should be doing anyway).

Community
  • 1
  • 1
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
0

I used the solution presented above by Brock Adams and created a small lib that I tend to use on my userscripts. It's called Spa Runner and can be accessed here. It works like this:

import { run } from "@banjoanton/spa-runner";


const handler = () => {
    console.log("hello world!");
}

const config = {
    timeBetweenUrlLookup: 250,
    urls: ["https://www.github.com/*/projects"],
    runAtStart: true,
    waitForElement: ".btn-danger", // optional
};

run(handler, config);
Anton Ödman
  • 451
  • 3
  • 7
  • 17