如何解决无需用户干预即可将文件url链接复制到剪贴板的Javascript
我正在尝试为tampermonkey创建一个简单无Flash .js文件,该文件在网页上搜索文件url(例如Google驱动器),仅将第一个链接复制到剪贴板。
以下是代码 已更新 :
(function() {
'use strict';
const urls = Array.from(document.querySelectorAll("a")); //get all link elements into an array.
const fileLinks = urls.filter(x=>
{
const test = x.src.indexOf("drive.google.com/file") || undefined
// add `|| x.src.indexOf("other search conditions")` before the last undefined to add search conditions;
if(test){return x}
//return only those that have the correct info in the src attribute
});
console.log(fileLinks);
navigator.clipboard.writeText(fileLinks[0])
.then(()=> console.log('success!'))
.catch(err=>console.error(`fail: ${err}`));
})();
以下是一些参考资料:
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Interact_with_the_clipboard
https://w3c.github.io/clipboard-apis/ https://developers.google.com/web/updates/2018/03/clipboardapi
Copy output of a JavaScript variable to the clipboard
Javascript copy text to clipboard without click any button on page load
How do I copy to the clipboard in JavaScript?
https://github.com/lgarron/clipboard-polyfill
解决方法
这未经测试。
由于您没有要纠正的代码,因此请从此开始-至少应该非常接近。
true
编辑: 代码中的小错误已修复。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。