我的用户名仅在页面刷新或iframe而不是主页时有效? [英] My userscript only works when the page is refreshed or on iframes, not the main page?

查看:118
本文介绍了我的用户名仅在页面刷新或iframe而不是主页时有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个简单的代码在Facebook页面上不起作用。此代码仅在我刷新页面时提醒。如果我使用鼠标从我的Facebook个人资料访问该页面;脚本停止工作。



没有警报或错误。 (

看起来整个脚本在刷新之前不起作用。

  // = = UserScript == 
// @name清除我的Facebook
// @namespace http://www.arda.com
// @description test
// @include https ://*.facebook.com*
// @include http://*.facebook.com*
// @version 1
// == / UserScript ==
window.setTimeout(isParent,500);

函数isParent(){

if(window.self == window.top){
alert 主窗口);
} else
window.setTimeout(isParent,500);
}

我还试过这个,其中包括:

  // == UserScript == 
// @name清除我的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 +\\\
main window);
else
alert(document.domain +\\\
if a iframe);
}
catch(e){
alert(document.domain +\\\
Had a problem:\\\
+ e);
}


解决方案

问题是一个AJAX 。当您键入URL或刷新页面时,脚本将起作用。当您点击活动日志按钮时,脚本不会。 iframe不是您报告的症状的一个因素。



这是因为点击该链接,从不加载新页面;它会触发替换部分内容的AJAX调用,使其像新页面一样,但不是。



所以,如果您希望您的脚本在新主页上启动,您必须使用此答案中的技术。



在Facebook的情况下,唯一通常会更改的是报告的URL(但不触发hashchange!),而# mainContainer div。



您还必须 @include 所有FB页面, code> https://www.facebook.com/*/allactivity* 通常只能通过AJAX访问,所以您的脚本需要在以前的



此脚本将解决您的问题提出的问题:

  // == UserScript == 
// @name清除我的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('Newmain(top)page loaded。
else
alert('newiframed page loaded。);
}



但是,由于该页面非常AJAX,您会发现页面首次加载时的触发几乎总是太早了。



聪明的事情是使用<$ c#c> waitForKeyElements 在您真正关心的页面的特定部分。 (问题中没有指出) / p>

以下是使用 waitForKeyElements 请注意,要在Chrome中使用 @require ,您必须使用Tampermonkey(您应该正在做)。


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);
}

解决方案

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).

这篇关于我的用户名仅在页面刷新或iframe而不是主页时有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆