Javascript 我的userscript只在刷新页面或在iFrame上工作,而不是在主页上工作?
这个简单的代码在Facebook页面上不起作用。此代码仅在我刷新时发出警报。如果我从,用我的鼠标进入那一页;脚本停止工作 没有警报或错误:(Javascript 我的userscript只在刷新页面或在iFrame上工作,而不是在主页上工作?,javascript,ajax,facebook,userscripts,Javascript,Ajax,Facebook,Userscripts,这个简单的代码在Facebook页面上不起作用。此代码仅在我刷新时发出警报。如果我从,用我的鼠标进入那一页;脚本停止工作 没有警报或错误:( 看起来整个脚本在我刷新之前都不起作用 // ==UserScript== // @name Purge My Facebook // @namespace http://www.arda.com // @description test // @include https://*.facebook.com* // @include http://*.fac
看起来整个脚本在我刷新之前都不起作用
// ==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的内容
您还必须@包括所有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.');
}
然而,由于页面被严重扭曲,您会发现当页面第一次加载时触发几乎总是太早
明智的做法是在您真正关心的页面的特定部分使用waitForKeyElements
。(问题中未指出)
请注意,要在Chrome中使用@require
,您必须使用Tampermonkey(您无论如何都应该这样做).这会在主窗口上引发警报,没问题。它不会在iFrame上触发,因为它们不在@include
指令的范围内。链接setTimeout
只会导致浏览器在iFrame上冻结/崩溃。不要这样做!这个脚本在“主窗口”中究竟是如何工作的不,它在主窗口不起作用。这是我的测试脚本。我在userscripts.org上阅读了你的所有答案和论坛。我现在正在整理我的头发。我测试了它;它在主窗口起作用。你叫主窗口什么?给出:(1)准确的测试URL,(2)预期行为,(3)实际行为。当代码失败时,我才开始编写代码。我今天删除了所有代码并开始编写新代码。我将在“活动日志”部分运行此脚本。但是,如果我用鼠标单击该部分,它将不起作用。如果我在“地址栏”中键入地址或刷新该页面,它将起作用。我只是尝试从主窗口获取元素,这都是不起作用的,您的两个脚本都在主框架上100%工作。您描述的是AJAX问题,而不是iframe问题,waitForKeyElements
绝对可以解决这一问题。请提供我之前请求的3个项目!它们也是从一开始就应该存在的问题。