JavaScript导出csv文件,并使用油猴在网页中插入按钮
// 导出数据模块 function data_to_csv(data, name) { const blob = new Blob(data, {type: 'text/csv,charset=UTF-8'}); const uri = URL.createObjectURL(blob); let downloadLink = document.createElement('a'); downloadLink.href = uri; downloadLink.download = (name+".csv")||"temp.csv"; document.body.appendChild(downloadLink); downloadLink.click(); document.body.removeChild(downloadLink);} //保存数据,注意换行格式 var data_list=Array(); data_list.push(["标题","链接","\n"]); data_list.push(["数据","http://*****","\n"]); data_to_csv(data_list, "fileName.csv");
使用油猴脚本在网页中插入按键
// ==UserScript== // @name test // @namespace AceScript Scripts // @match https://www.baidu.com/ // @grant none
// @require https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js //更新地址:http://cdn.bootcdn.net
// ==/UserScript== (function () { 'use strict'; console.log('我的脚本加载了'); var button = document.createElement("button"); //创建一个input对象(提示框按钮) button.id = "id001"; button.textContent = "百度二下"; button.style.width = "60px"; button.style.height = "20px"; button.style.align = "center"; //绑定按键点击功能 button.onclick = function (){ console.log('点击了按键'); //为所欲为 功能实现处 alert("你好"); return; }; var x = document.getElementsByClassName('btn_wr s_btn_wr bg')[0]; //在浏览器控制台可以查看所有函数,ctrl+shift+I 调出控制台,在Console窗口进行实验测试 x.appendChild(button); //var y = document.getElementById('s_btn_wr'); //y.appendChild(button); })();
等待网页加载完成再执行油猴脚本【已解决】
// @run-at document-end
// @name 等待网页加载完成再执行油猴脚本
// @namespace waitForKeyElements
// @version 0.1
// @description try to take over the world!
// @author You
// @match http://mobile.yangkeduo.com/search_result.html?search_key*
// @grant unsafeWindow
// @require https://code.jquery.com/jquery-3.6.0.min.js
// @require https://greasyfork.org/scripts/31940-waitforkeyelements/code/waitForKeyElements.js?version=209282
// ==/UserScript==
waitForKeyElements('[class="pdd-go-to-app"]', addbut);
function addbut() {
'use strict';
console.log('我的脚本加载了');
var button = document.createElement("button"); //创建一个input对象(提示框按钮)
button.id = "id001";
button.textContent = "百度二下";
button.style.width = "60px";
button.style.height = "40px";
button.style.align = "center";
//绑定按键点击功能
button.onclick = function () {
console.log('点击了按键');
//为所欲为 功能实现处
alert("你好");
return;
};
var x = document.querySelector('[class="pdd-go-to-app"]');
//在浏览器控制台可以查看所有函数,ctrl+shift+I 调出控制台,在Console窗口进行实验测试
x.appendChild(button);
//var y = document.getElementById('s_btn_wr');
//y.appendChild(button);
};
问题解决是根据这篇文章:https://www.thinbug.com/q/12897446获得解决方案
引用的js来自 #引用:https://greasyfork.org/zh-CN/scripts/31940-waitforkeyelements/code
关键点在于:9,10,14,15行做了更改
waitForKeyElements ("YOUR_jQUERY_SELECTOR", actionFunction);
var data_list=Array();data_list.push(["标题","链接","\n"])