jQuery Waypoints from Greasemonkey -
i'm attempting use jquery waypoints plugin greasemonkey script, , can't seem basic tests work. know of reason waypoints wouldn't work user script?
@require lines:
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js // @require http://cdn.jsdelivr.net/jquery.waypoints/2.0.2/waypoints.min.js
script:
$('div.container').waypoint(function(){ alert('you hit bottom'); },{ offset: 'bottom-in-view' });
interestingly, if div "container" class exists, script breaks, , code outside statement not run. if change selector find non-existent element, rest of script runs fine.
does have idea what's going on here? i'm banging head against wall. thanks!
ps. i've tried pasting waypoints plugin code directly script (instead of using cdn), , same results.
unfortunately, extension not written many other jquery extensions. uses this
, , jquery
in, let's say, "unfortunate" ways. means crashes userscript scope, if @grant none
set.
for libraries, this, options are:
find better library (recommended).
find better way code yourself, if it's not involved.
if page doesn't use jquery, or uses compatible version of jquery, can use such extensions via script injection. see below.
if page uses incompatible version of jquery, there may nothing can without breaking page. sometimes workaround possible, question.
case page uses compatible version of jquery:
you need inject waypoints , code uses waypoints. not use @require
.
so:
// ==userscript== // @name _your_script_name // @include http://your_server.com/your_path/* // @grant gm_addstyle // ==/userscript== /*- @grant directive needed work around design change introduced in gm 1.0. restores sandbox. */ function gm_main () { $('div.container').waypoint(function(){ alert('you hit bottom'); },{ offset: 'bottom-in-view' }); } addjs_node ( null, "http://cdn.jsdelivr.net/jquery.waypoints/2.0.2/waypoints.min.js", null, function () {addjs_node (null, null, gm_main); } ); //-- standard-ish utility function. function addjs_node (text, s_url, functorun, runonload) { var d = document; var scriptnode = d.createelement ('script'); if (runonload) { scriptnode.addeventlistener ("load", runonload, false); } scriptnode.type = "text/javascript"; if (text) scriptnode.textcontent = text; if (s_url) scriptnode.src = s_url; if (functorun) scriptnode.textcontent = '(' + functorun.tostring() + ')()'; var targ = d.getelementsbytagname ('head')[0] || d.body || d.documentelement; targ.appendchild (scriptnode); }
case page doesn't use jquery @ all:
you need inject jquery , chain injection of waypoints , code uses waypoints. not use @require
.
so:
// ==userscript== // @name _your_script_name // @include http://your_server.com/your_path/* // @grant gm_addstyle // ==/userscript== /*- @grant directive needed work around design change introduced in gm 1.0. restores sandbox. */ function gm_main () { $('div.container').waypoint(function(){ alert('you hit bottom'); },{ offset: 'bottom-in-view' }); } //-- add jquery. addjs_node ( null, "http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js", null, addwaypointsandfiremain ); function addwaypointsandfiremain () { addjs_node ( null, "http://cdn.jsdelivr.net/jquery.waypoints/2.0.2/waypoints.min.js", null, function () {addjs_node (null, null, gm_main); } ); } //-- standard-ish utility function. function addjs_node (text, s_url, functorun, runonload) { var d = document; var scriptnode = d.createelement ('script'); if (runonload) { scriptnode.addeventlistener ("load", runonload, false); } scriptnode.type = "text/javascript"; if (text) scriptnode.textcontent = text; if (s_url) scriptnode.src = s_url; if (functorun) scriptnode.textcontent = '(' + functorun.tostring() + ')()'; var targ = d.getelementsbytagname ('head')[0] || d.body || d.documentelement; targ.appendchild (scriptnode); }
unfortunately, when have inject script's code, becomes messier use gm_
functions. if applies you, see: "how call greasemonkey's gm_ functions code must run in target page scope?"
Comments
Post a Comment