代码之家  ›  专栏  ›  技术社区  ›  Matt V.

通过bookmarklet将jquery、colorbox和自定义代码注入页面

  •  0
  • Matt V.  · 技术社区  · 14 年前

    我的灵感来自 Instapaper bookmarklets 它允许在没有单独弹出窗口和无需重新加载整个页面的情况下将页面添加到Instapaper。我想把这个想法和 Delicious bookmarklet 用于书签页面。

    这个 jQuery Colorbox plugin 支持将iframe内容加载到colobox弹出窗口中,所以它看起来是一个不错的开始。我拼凑了几个书签来获取下面的代码,这似乎是一个良好的开始,直到我添加了“ $.fn.colorbox ……“行。我好像打不开色箱。我甚至试着简化它以打开Google Hoppepage,但也没有运气:

    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)
    
    1 回复  |  直到 14 年前
        1
  •  1
  •   Sean Patrick Floyd    14 年前

    var checkIfLoaded = function(){
        if($.fn.colorbox){
            // continue your processing
        }else{
            window.setTimeout(200, checkIfLoaded);
        }
    }
    checkIfLoaded();