我userscript只能在页面刷新或内部框架,而不是主页? [英] My userscript only works when the page is refreshed or on iframes, not the main page?

查看:141
本文介绍了我userscript只能在页面刷新或内部框架,而不是主页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个简单的code没有一个Facebook页面上的工作。这code,只有当我刷新的页面警报。如果我去该网页我的Facebook个人资料,用我的鼠标;该脚本将停止工作。

有没有警告或错误。 :(
它看起来像整个脚本无法正常工作,直到我刷新。

  // == UserScript ==
// @name清除我的Facebook
// @namespace http://www.arda.com
// @description测试
// @包括HTTPS://*.facebook.com*
// @包括HTTP://*.facebook.com*
// @version 1
// == / UserScript ==
window.setTimeout(isParent,500);

功能isParent(){

    如果(window.self == window.top){
        警报(主窗口);
    } 其他
        window.setTimeout(isParent,500);
}
 

我也试过这一点,除其他事项外:

  // == UserScript ==
// @name清除我的Facebook
// @namespace http://www.arda.com
// @description测试
// @include http://www.facebook.com/*/allactivity*
// @include https://www.facebook.com/*/allactivity*
// @version 1
// == / UserScript ==

尝试{
  如果(window.self == window.top)
    警报(document.domain的+\ nmain窗口);
  其他
    警报(document.domain的+\ n在一个IFRAME);
}
赶上(E){
  警报(document.domain的+\ nHad一个问题:\ N+ E);
}
 

解决方案

现在的问题是一个AJAX之一。当您键入的URL,或刷新页面,你的脚本工作。当你点击你的活动日志按钮时,脚本没有。内部框架是不是你报的症状的一个因素。

这是因为点击该链接,不要加载了新的一页;它触发了更换内容的一部分AJAX调用,使得它的的像一个新的页面,但事实并非如此。

所以,如果你希望你的脚本火的新,主要页面,你必须利用技术,如那些这个答案

在Facebook的情况下,通常改变的仅仅是报告URL(但不触发hashchange!),而 #mainContainer div的内容。

您还必须 @include 所有FB页面,因为像 https://www.facebook.com/*/allactivity* 往往只能通过AJAX达到了,所以你的脚本需要被上运行的 previous 的页面。

该脚本将提出解决你的问题中的问题:

  // == UserScript ==
// @name清除我的Facebook
// @namespace http://www.ardaterekeci.com
// @description测试
// @include http://www.facebook.com/*
// @include https://www.facebook.com/*
// @version 1
// == / UserScript ==

VAR pageURLCheckTimer =的setInterval(
    功能 () {
        如果(this.lastPathStr!== location.pathname
            || this.lastQueryStr!== location.search
            || this.lastPathStr ===空
            || this.lastQueryStr ===空
        ){
            this.lastPathStr = location.pathname;
            this.lastQueryStr = location.search;
            gmMain();
        }
    }
    ,222
);

传播gmMain(){
    如果(window.self === window.top)
        警报(新主要(顶部)页面加载。');
    其他
        警报(新iframed页面加载。');
}
 


但是,,因为页面是大量AJAXed,你会发现,当射击第一次加载页面几乎总是太早了。

聪明的做法是使用 waitForKeyElements 在你真正关心的网页的特定部分。(这是不是在这个问题说明。)

下面是使用的例子 waitForKeyElements 注意,要使用 @require 在Chrome中,你必须要使用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).

这篇关于我userscript只能在页面刷新或内部框架,而不是主页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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