动态加载的 jQuery 与 GreaseMonkey 在页面上不一致(刷新似乎可以修复它)...不知道为什么
我希望在我访问的每个站点上都有一个自定义页面分析页脚...因此我使用了一种方法将 JQuery 附加到 unsafeWindow。
然后我在页面上创建一个浮动页脚。我希望能够调用菜单中的命令,进行一些处理,然后将结果放在页脚中。不幸的是,它有时有效,有时无效。
printOutput 函数中至少应发生两个警报。有时它只触发一个,然后它(崩溃?)没有错误?在其他页面上,两个警报都会触发并找到该元素,但不会添加额外的文本。 (例如 www.linode.com)
刷新页面,然后再次运行 printOutput 命令似乎总是有效。
有谁知道怎么回事吗???
用户脚本可以安装在: http://www.captionwizard.com/test/page_analysis.user.js
// ==UserScript==
// @name page_analysis
// @namespace markspace
// @description Page Analysis
// @include http://*/*
// ==/UserScript==
(function()
{
// Add jQuery
var GM_JQ = document.createElement('script');
GM_JQ.src = 'http://code.jquery.com/jquery-1.4.2.min.js';
GM_JQ.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(GM_JQ);
var jqueryActive = false;
//Check if jQuery's loaded
function GM_wait()
{
if(typeof unsafeWindow.jQuery == 'undefined')
{ window.setTimeout(GM_wait,100); }
else
{ $ = unsafeWindow.jQuery; letsJQuery(); }
}
GM_wait();
function letsJQuery()
{
jqueryActive = true;
setupOutputFooter();
}
/******************************* Analysis FOOTER Functions ******************************/
function printOutput(someText)
{
alert('printing output');
if($('div.analysis_footer').length)
{
alert('is here - appending');
$('div.analysis_footer').append('<br>' + someText);
}
else
{
alert('not here - trying again');
setupOutputFooter();
$('div.analysis_footer').append('<br>' + someText);
}
}
GM_registerMenuCommand("Test Output", testOutput, "k", "control", "k" );
function testOutput()
{
printOutput('testing this');
}
function setupOutputFooter()
{
$('<div class="analysis_footer">Page Analysis Footer:</div>').appendTo('body');
$('div.analysis_footer').css('position','fixed').css('bottom', '0px').css('background-color','#F8F8F8');
$('div.analysis_footer').css('width','100%').css('color','#3B3B3B').css('font-size', '0.8em');
$('div.analysis_footer').css('font-family', '"Myriad",Verdana,Arial,Helvetica,sans-serif').css('padding', '5px');
$('div.analysis_footer').css('border-top', '1px solid black').css('text-align', 'left');
}
}());
I want a custom page analysis footer on every site I visit... so I've used a method to attach JQuery to unsafeWindow.
I then create a floating footer on the page. I want to be able to call commands in a menu, do some processing, then put the results in the footer. Unfortunately it sometimes works, sometimes it doesn't.
At least two alerts should happen in the printOutput function. Sometimes it only fires one, then it (crashes?) without error? On other pages, both alerts fire and it finds the element, but it doesn't add the extra text. (e.g. www.linode.com)
Refreshing the page, then running the printOutput command again seems to always work.
Does anyone know what's going on???
The userscript can be installed at:
http://www.captionwizard.com/test/page_analysis.user.js
// ==UserScript==
// @name page_analysis
// @namespace markspace
// @description Page Analysis
// @include http://*/*
// ==/UserScript==
(function()
{
// Add jQuery
var GM_JQ = document.createElement('script');
GM_JQ.src = 'http://code.jquery.com/jquery-1.4.2.min.js';
GM_JQ.type = 'text/javascript';
document.getElementsByTagName('head')[0].appendChild(GM_JQ);
var jqueryActive = false;
//Check if jQuery's loaded
function GM_wait()
{
if(typeof unsafeWindow.jQuery == 'undefined')
{ window.setTimeout(GM_wait,100); }
else
{ $ = unsafeWindow.jQuery; letsJQuery(); }
}
GM_wait();
function letsJQuery()
{
jqueryActive = true;
setupOutputFooter();
}
/******************************* Analysis FOOTER Functions ******************************/
function printOutput(someText)
{
alert('printing output');
if($('div.analysis_footer').length)
{
alert('is here - appending');
$('div.analysis_footer').append('<br>' + someText);
}
else
{
alert('not here - trying again');
setupOutputFooter();
$('div.analysis_footer').append('<br>' + someText);
}
}
GM_registerMenuCommand("Test Output", testOutput, "k", "control", "k" );
function testOutput()
{
printOutput('testing this');
}
function setupOutputFooter()
{
$('<div class="analysis_footer">Page Analysis Footer:</div>').appendTo('body');
$('div.analysis_footer').css('position','fixed').css('bottom', '0px').css('background-color','#F8F8F8');
$('div.analysis_footer').css('width','100%').css('color','#3B3B3B').css('font-size', '0.8em');
$('div.analysis_footer').css('font-family', '"Myriad",Verdana,Arial,Helvetica,sans-serif').css('padding', '5px');
$('div.analysis_footer').css('border-top', '1px solid black').css('text-align', 'left');
}
}());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能只是 code.jquery.com 有时无法(或至少非常慢)加载 jQuery 脚本。你所做的看起来不错。您还可以使用 @require 将 jQuery 与用户脚本一起打包(因此不需要为每个页面加载请求 jquery):
阅读本文以获取更多信息。
It could just be that code.jquery.com sometimes fails (or at least is very slow) to load the jQuery script. What you've done looks fine. You can also package jQuery with the userscript, with @require (thus jquery doesn't need to be requested for every page load):
read this for more information.