Rad*_*art 9 javascript jquery opera
我想知道是否可以在Opera用户JavaScript中加载jQuery ,这样我就可以在其中使用jQuery功能,甚至在当前没有使用jQuery的页面上也是如此.
我刚刚编写了一个Opera userJS,我从谷歌Chrome扩展程序转换而来,可以很容易地加载jQuery,但是在Opera中我必须使用另一个解决方案,我想在我的代码中进行最小的更改.
我只是在文档的标题中添加一个新的脚本标记,其中包含一个有效的jQuery源代码.它开始在DOM准备好之前下载.
这是我的工作解决方案,希望有所帮助!
// ==UserScript==
// @name This is the name of my userJS
// @description That's the description of my userJS
// @include http://blahblah.hu/*
// @include http://*.blahblah.hu/*
// @match http://blahblah.hu/*
// @match http://*.blahblah.hu/*
// @version 1.0
// ==/UserScript==
(function( ) {
// include jQuery
var headID = document.getElementsByTagName("head")[0];
var newScript = document.createElement('script');
newScript.type = 'text/javascript';
newScript.id = 'myjQuery';
newScript.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js';
headID.appendChild(newScript);
window.addEventListener('load', function (e) {
// you can write your jQuery code now!
$('#yourElementId').css('background-color', 'yellow');
// further jQuery codes
// ...
}, false);
})( );
Run Code Online (Sandbox Code Playgroud)
如果您将全部 jQuery 粘贴到 UserJS 文件中,或者使用 jQuery 库为所有页面创建一个单独的 UserJS 文件,那么这当然是可能的。但是,它有可能与任何使用 jQuery 的页面发生冲突。
根据您的要求,也许您可以推出自己的迷你框架?例如一些通过标签/类/id 抓取元素的简单函数:
function $id(id) {
return document.getElementById(id);
}
Run Code Online (Sandbox Code Playgroud)
注意:您可能会在Opera 论坛上得到更好的回应。