javascript – 从bookmarklet将文本复制到剪贴板
作者:互联网
我正在尝试编写一个小书签,可以从活动页面中提取一些文本并将其加载到剪贴板中.
提取很容易,但我真的很难做剪贴板复制部分.目前,我只是提醒文本并按Ctrl C复制消息框中的文本,这是不理想的.
我已经阅读了How to Copy to Clipboard in JavaScript以及其他建议我使用zeroclipboard的问题,但我不知道如何使用bookmarklet来完成这项工作,考虑到我必须加载外部flash和javascript资源才能使用该库.
我没有弄乱页面的DOM来实现这一点,或者必须在我的浏览器(谷歌浏览器)上启用一些权限,考虑到这只是一个私人书签.
任何指针将不胜感激.
解决方法:
几个免责声明:
>我不是想欺骗你
>如果你选择使用它,我什么也得不到
我做了一个bookmarklet generator,让我更容易创建bookmarklet.
它启用了jQuery,但这并不意味着你必须使用jQuery.
您可以check out the source查看如何通过书签将另一个脚本/库导入页面.
特别是,导入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);
}
我希望有所帮助.
标签:bookmarklet,javascript,google-chrome,clipboard 来源: https://codeday.me/bug/20190928/1827886.html