【发布时间】:2011-12-24 11:21:19
【问题描述】:
我写了一个简单的Greasemonkey script,它在天桥弹出窗口中放大缩略图。它在其中使用了很多 jQuery。它在 Firefox 上运行良好。但不是在 Chrome 上,因为它不支持 @require。
我在这件事上遇到了这个solution。但是即使在我将它与绕过代码集成后,该脚本也无法在 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);
【问题讨论】:
-
<><![CDATA[ ...构造在 Chrome 中不起作用。此外,jQuery 注入也不是最好的。如果有人没有打败我,我会在几个小时内发布更详细的答案。 -
我也猜到了。谢谢你。 :)
标签: javascript google-chrome greasemonkey userscripts