注入 jQuery、Colorbox 等通过书签将自定义代码添加到页面中

发布于 2024-10-02 15:18:45 字数 1350 浏览 3 评论 0原文

我受到 Instapaper 书签 的启发,它允许将页面添加到 Instapaper 中,而无需单独的弹出窗口 -窗口,无需重新加载整页。我想将这个想法与用于为页面添加书签的 Delicious bookmarklet 结合起来。

jQuery Colorbox 插件 支持将 iframe 内容加载到 colobox 弹出窗口中,因此它似乎是一个不错的起点。我拼凑了几个书签来获取下面的代码,这似乎是一个良好的开始,直到我添加了“

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)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゝ杯具 2024-10-09 15:18:45

脚本在单独的线程中加载。您需要等到脚本加载后才能使用它。通常是这样的:

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文