让 jQuery 和 GM_addStyle 在基于工作 Greasemonkey 脚本的 Chrome 用户脚本中工作
我编写了一个简单的 Greasemonkey 脚本,用于放大天桥弹出窗口中的缩略图。它使用了大量的 jQuery。它在 Firefox 上运行得很好。但在 Chrome 上不行,因为它不支持 @require。
我遇到了这个解决方案 对于此事。但即使我将其与绕过代码集成后,该脚本也无法在 Chrome 上运行。我只是将所有脚本代码放入解决方案代码的主函数中。
是不是错了?如果有人能指出问题出在哪里,以及我能做些什么来解决问题,我将不胜感激。
function addJQuery(callback) {
var script = document.createElement("script");
script.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");
script.addEventListener('load', function() {
var script = document.createElement("script");
script.textContent = "(" + callback.toString() + ")();";
document.body.appendChild(script);
}, false);
document.body.appendChild(script);
}
function main()
{
$("body").append ('<div id="idLargePicturePopupWindow"><img></div>');
$('#idLargePicturePopupWindow').bind
(
"mouseenter mouseleave",
{bInPopup: true},
myImageHover
);
$('#profPhotos .profPhotoLink > img').bind
(
"mouseenter mouseleave",
{bInPopup: false},
myImageHover
);
function myImageHover (zEvent)
{
if (zEvent.type == 'mouseenter')
{
if ( ! zEvent.data.bInPopup)
{
var imgurl = this.src.toString();
var bigimg = imgurl.replace(/\/thumbs\/[0-9x]+\//i, "/photos/");
$("#idLargePicturePopupWindow img").attr ('src', bigimg);
}
$("#idLargePicturePopupWindow").show();
}
else
{
$("#idLargePicturePopupWindow").hide();
}
}
GM_addStyle ( (<><![CDATA[
#idLargePicturePopupWindow
{
position: absolute;
background: white;
border: none;
margin: 1ex;
opacity: 1.0;
z-index: 1222;
min-height: 100px;
min-width: 200px;
padding: 0;
display: none;
top: 2em;
left: 50em;
}
#idLargePicturePopupWindow img
{
margin: 0;
margin-bottom: -4px;
padding: 0;
}
]]></>).toString () );
}
addJQuery(main);
I wrote a simple Greasemonkey script that enlarges thumbnail pictures in a flyover popup. It uses a lot of jQuery in it. It works just fine on Firefox. But not on Chrome since it doesn't support @require.
I came across this solution for this matter. But the script didn't work on Chrome even after I integrated it with the get-around code. I just put all my script code inside the solution code's main function.
Is it wrong? If anyone can point out where is the problem, and what I can do to get it right, it'll be very much appreciated.
function addJQuery(callback) {
var script = document.createElement("script");
script.setAttribute("src", "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");
script.addEventListener('load', function() {
var script = document.createElement("script");
script.textContent = "(" + callback.toString() + ")();";
document.body.appendChild(script);
}, false);
document.body.appendChild(script);
}
function main()
{
$("body").append ('<div id="idLargePicturePopupWindow"><img></div>');
$('#idLargePicturePopupWindow').bind
(
"mouseenter mouseleave",
{bInPopup: true},
myImageHover
);
$('#profPhotos .profPhotoLink > img').bind
(
"mouseenter mouseleave",
{bInPopup: false},
myImageHover
);
function myImageHover (zEvent)
{
if (zEvent.type == 'mouseenter')
{
if ( ! zEvent.data.bInPopup)
{
var imgurl = this.src.toString();
var bigimg = imgurl.replace(/\/thumbs\/[0-9x]+\//i, "/photos/");
$("#idLargePicturePopupWindow img").attr ('src', bigimg);
}
$("#idLargePicturePopupWindow").show();
}
else
{
$("#idLargePicturePopupWindow").hide();
}
}
GM_addStyle ( (<><![CDATA[
#idLargePicturePopupWindow
{
position: absolute;
background: white;
border: none;
margin: 1ex;
opacity: 1.0;
z-index: 1222;
min-height: 100px;
min-width: 200px;
padding: 0;
display: none;
top: 2em;
left: 50em;
}
#idLargePicturePopupWindow img
{
margin: 0;
margin-bottom: -4px;
padding: 0;
}
]]></>).toString () );
}
addJQuery(main);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
两个主要问题和 1 个可能 问题:
1) 不要将
GM_addStyle()
包装在main()
函数内。GM_addStyle()
仅适用于脚本范围,它无法注入到目标页面(这就是main()
和addJQuery() 业务确实如此)。
2) 当前代码使用 E4X 制作多行字符串发送到
GM_addStyle( )
,但 Chrome 不支持 E4X。唉,Chrome 支持的多行字符串 hack(目前) 在 Firefox 中不起作用。
这意味着如果您希望支持两种浏览器,则使用
GM_addStyle()
编写逼真的样式会稍微困难一些。使用多行转义字符 (\
),如下所示:¿3?) 特定版本的
addJQuery()
可能并不总是有效(竞争条件)。如果没有,请告诉我。Two main problems and 1 possible problem:
1) Do not wrap
GM_addStyle()
inside themain()
function.GM_addStyle()
only works in script scope, it will not work injected to the target page (which is what thatmain()
andaddJQuery()
business does).2) The current code uses E4X to make a multiline string to send to
GM_addStyle()
, but Chrome doesn't support E4X.Alas, the multiline string hack that Chrome does support (for now) does not work in Firefox.
That means it's slightly harder to code realistic styles with
GM_addStyle()
if you wish to support both browsers. Use the multiline escape character (\
) like so:¿3?) That particular version of
addJQuery()
may not always work (race condition). Let me know if it doesn't.