admin管理员组文章数量:1289556
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
// @description test
// @include https://*.facebook*
// @include http://*.facebook*
// @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
// @description test
// @include /*/allactivity*
// @include /*/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);
}
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.
// @description test
// @include https://*.facebook.*
// @include http://*.facebook.*
// @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.
// @description test
// @include http://www.facebook./*/allactivity*
// @include https://www.facebook./*/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);
}
Share
Improve this question
edited Jul 26, 2013 at 13:57
Arda
asked Jul 25, 2013 at 15:30
ArdaArda
4705 silver badges10 bronze badges
11
-
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 thesetTimeout
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 Commented Jul 25, 2013 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.. i am pulling my hair right now. – Arda Commented Jul 25, 2013 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 Commented Jul 25, 2013 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 Commented Jul 25, 2013 at 21:19
-
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 Commented Jul 25, 2013 at 22:43
2 Answers
Reset to default 9The 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./*/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.
// @description test
// @include http://www.facebook./*
// @include https://www.facebook./*
// @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).
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./*/projects"],
runAtStart: true,
waitForElement: ".btn-danger", // optional
};
run(handler, config);
本文标签:
版权声明:本文标题:javascript - My userscript only works when the page is refreshed or on iframes, not the main page? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741404476a2376860.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论