injecting jQuery, Colorbox, & custom code into a page via a bookmarklet
I've been inspired by the Instapaper bookmarklets that allow a page to be added to Instapaper without a separate pop-up window and without requiring a full page reload. I'd like to combine that idea with a Delicious bookmarklet for bookmarking pages.
The jQuery Colorbox plugin supports loading iframe content into colobox pop-ups, so it seemed like a good place to start. I pieced together several bookmarklets to get the code below, which seemed off to a good start, until I added the "$.fn.colorbox..." line. I can't seem to get a Colorbox to open. I even tried simplifying it to just open the Google hopepage, but no luck there either:
javascript:
function iprl5(){
var d=document,z=d.createElement('scr'+'ipt'),y=d.createElement('scr'+'ipt'),x=d.createElement('scr'+'ipt'),b=d.开发者_开发百科body,l=d.location,t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');
try{
if(!b)
throw(0);
d.title='(Saving...)%20'+d.title;
x.setAttribute('src','http://cachedcommons.org/cache/jquery/1.4.2/javascripts/jquery.js');
y.setAttribute('src','http://cachedcommons.org/cache/jquery-colorbox/1.3.9/javascripts/jquery-colorbox.js');
b.appendChild(x);
b.appendChild(y);
$.fn.colorbox({href:'http://www.delicious.com/save?url='+encodeURIComponent(window.location.href)+'&title='+encodeURIComponent(document.title)+'&v=5&',open:true,iframe:true});
}
catch(e){
alert('Please%20wait%20until%20the%20page%20has%20loaded.');
}
}
iprl5();
void(0)
Scripts are loaded in a separate thread. You need to wait until the script is loaded before you use it. Usually something like this:
var checkIfLoaded = function(){
if($.fn.colorbox){
// continue your processing
}else{
window.setTimeout(200, checkIfLoaded);
}
}
checkIfLoaded();
精彩评论