从小书签将文本复制到剪贴板 [英] Copy text to clipboard from bookmarklet

查看:121
本文介绍了从小书签将文本复制到剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着写一个小书签,可以从活动页面中提取一些文本并将其加载到剪贴板。



提取很容易,但我真的被困在剪贴板复制部分。目前,我只是 alert 并按下 Ctrl + C 这是不理想的。



我读过和其他问题,建议我使用 zeroclipboard ,但我不知道如何从一个 bookmarklet 完成这项工作,因为我必须将外部 flash和javascript资源加载到能够使用库。

我没有弄错页面的DOM来完成此操作,或者必须在我的浏览器(Google Chrome)上启用某些权限,因为这只是一个私人书签。

任何指针将不胜感激。

解决方案

一对夫妇免责声明:


  1. 我不会试图发送垃圾邮件

  2. 如果您选择使用此功能,则不会获得任何收益

之后,我制作了一个,以使我更容易创建书签。



它启用了jQuery,但这并不意味着您必须使用jQuery。



您可以查看源了解如何将其他脚本/库导入到



特别是,导入jQuery的行:

  if(!window.zbooks)
{
//如果zbooks尚未设置,初始化

// s用于Script元素
var s = document.createElement('script');
// r用于就绪状态
var r = false;
//将脚本设置为最新版本的jQuery
s.setAttribute('src','http://code.jquery.com/jquery-latest.min.js');
//设置load / readystate事件
s.onload = s.onreadystatechange = function()
{
/ **
* LOAD / READYSTATE LOGIC
*如果脚本尚未准备好,则执行:
* - 未设置就绪状态
* - 就绪状态完成
* - 注意:readyState == 'loaded'在脚本被调用之前执行,所以
*我们跳过这个事件,因为它不会加载init事件。
* /
if(!r&&(!this.readyState || this.readyState =='complete'))
{
//设置就绪标志为true以防止事件再次初始化
r = true;
//通过将jQuery放置在zbooks对象中来防止jQuery冲突
window.zbooks = {'jQuery':jQuery.noConflict()};
//创建一个新的zbook
window.zbooks [n] = new zbooks(c);
}
};
//将jQuery脚本附加到主体
b.appendChild(s);
}

我希望有帮助。


I'm trying to write a little bookmarklet that can extract some text from the active page and load that into the clipboard.

The extraction is easy enough, but I'm really stuck doing the clipboard-copying part. Currently, I'm just alerting the text and hitting Ctrl+C to copy the text from the message-box, which isn't ideal.

I've read How to Copy to Clipboard in JavaScript and other questions that suggest I use zeroclipboard, but I have no idea how one would make that work from a bookmarklet, considering I have to load external flash and javascript resources to be able to use the library.

I have no issues with messing up the page's DOM to accomplish this or having to enable some permissions on my browser (Google Chrome), considering this is just a private bookmarklet.

Any pointers would be appreciated.

解决方案

A couple disclaimers:

  1. I'm not trying to spam you
  2. I gain nothing if you choose to use this

I made a bookmarklet generator a while back to make it easier for me to create bookmarklets.

It's jQuery enabled, but that doesn't mean you have to use jQuery.

You can check out the source to see how to import another script/library into a page via a bookmarklet.

In particular, the lines that import jQuery:

if (!window.zbooks)
  {
    //if zbooks hasn't been set, initialize it

    //s used for the Script element
    var s = document.createElement('script');
    //r used for the Ready state
    var r = false;
    //set the script to the latest version of jQuery
    s.setAttribute('src', 'http://code.jquery.com/jquery-latest.min.js');
    //set the load/readystate events
    s.onload = s.onreadystatechange = function()
    {
/**
 * LOAD/READYSTATE LOGIC
 * execute if the script hasn't been ready yet and:
 * - the ready state isn't set
 * - the ready state is complete
 *   - note: readyState == 'loaded' executes before the script gets called so
 *     we skip this event because it wouldn't have loaded the init event yet.
 */
      if ( !r && (!this.readyState || this.readyState == 'complete' ) )
      {
        //set the ready flag to true to keep the event from initializing again
        r = true;
        //prevent jQuery conflicts by placing jQuery in the zbooks object
        window.zbooks = {'jQuery':jQuery.noConflict()};
        //make a new zbook
        window.zbooks[n] = new zbooks(c);
      }
    };
    //append the jQuery script to the body
    b.appendChild(s);
  }

I hope that helps.

这篇关于从小书签将文本复制到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆