Javascript将文件url链接复制到剪贴板,无需用户交互
我正在尝试为tampermonkey创建一个简单的no flash.js文件,它在网页中搜索文件url(例如google drive),并只将第一个链接复制到剪贴板 以下是更新后的代码: 以下是一些参考资料: 这是未经测试的 既然您没有要更正的代码,那么就从这个开始吧——这至少应该非常接近Javascript将文件url链接复制到剪贴板,无需用户交互,javascript,url,copy,clipboard,tampermonkey,Javascript,Url,Copy,Clipboard,Tampermonkey,我正在尝试为tampermonkey创建一个简单的no flash.js文件,它在网页中搜索文件url(例如google drive),并只将第一个链接复制到剪贴板 以下是更新后的代码: 以下是一些参考资料: 这是未经测试的 既然您没有要更正的代码,那么就从这个开始吧——这至少应该非常接近 const urls = Array.from(document.querySelectorAll("a")); //get all link elements into an
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}`));
编辑:
代码中的小错误已被修复。Stackoverflow的目的是帮助解决基于编码的问题,这远远不够广泛,除非有人为您工作,否则无法提供任何真正的帮助。请考虑提交一些代码示例,详细介绍您遇到的故障。2个错误:const URLS=数组。来自(文档QueRealSturTurl(a));//给出错误“a”未定义的var a=“”//在上面添加了这一行。catch(err)console.error(
fail:${err}
)//给出错误eslint:null-解析错误:意外的令牌控制台-我刚才在前面添加了两个//以注释掉。但它并没有将google drive链接复制到剪贴簿中,所以我不确定如何使用查询选择器定位“a”html标记。。。尝试“a”而不是a第二个错误似乎是由于查询失败。第一行应该按原样工作,上面没有变量声明。。。99%确定a只需要用引号括起来。在const url=Array.from(document.querySelectorAll(“a”);//周围添加了双引号第一个错误消失了,但第二个错误仍然存在,这不是因为查询失败,而是调试器给出了错误。。。我的代码有一个输入错误。尝试.catch(err=>console.error(fail:${err}
)…在答案中修复了它。
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}`));