簡體   English   中英

我的用戶腳本僅在頁面刷新或在 iframe 上有效,而不是主頁?

[英]My userscript only works when the page is refreshed or on iframes, not the main page?

這個簡單的代碼在 Facebook 頁面上不起作用。 此代碼僅在我刷新頁面時發出警報。 如果我 go 從我的 Facebook 個人資料到那個頁面,用我的鼠標; 該腳本停止工作。

沒有警報或錯誤。 :(
看起來整個腳本在我刷新之前不起作用。

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

我也試過這個,除其他外:

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

問題是 AJAX 問題。 當您鍵入 URL 或刷新頁面時,您的腳本將起作用。 當您單擊“活動日志”按鈕時,腳本不會。 iframe 不是您報告的症狀的一個因素。

這是因為單擊該鏈接不會加載新頁面; 它會觸發替換部分內容的 AJAX 調用,使其看起來像一個新頁面,但事實並非如此。

因此,如果您希望腳本在“新”“主頁”上觸發,則必須使用本答案中的技術

在 Facebook 的情況下,唯一通常會更改的是報告的 URL(但不會觸發 hashchange!)和#mainContainer div 的內容。

您還必須@include所有 FB 頁面,因為像https://www.facebook.com/*/allactivity*這樣的頁面通常只能通過 AJAX 訪問,因此您的腳本需要在上一頁上運行。

此腳本將解決您的問題中提出的問題:

// ==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.');
}


但是,由於該頁面大量使用 AJAX,您會發現在頁面首次加載時觸發幾乎總是為時過早。

明智的做法是在您真正關心的頁面的特定部分使用waitForKeyElements (這在問題中沒有指出。)

這是使用waitForKeyElements的示例。 請注意,要在 Chrome 中使用@require ,您必須使用 Tampermonkey(無論如何您都應該這樣做)。

我使用了上面由 Brock Adams 提出的解決方案,並創建了一個我傾向於在我的用戶腳本中使用的小庫。 它稱為 Spa Runner,可在此處訪問。 它是這樣工作的:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM