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
// ==/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);
})();

 等待網頁加載完成再執行油猴腳本【已解決】

 1 // ==UserScript==
 2 // @run-at document-end
 3 // @name         等待網頁加載完成再執行油猴腳本
 4 // @namespace    waitForKeyElements
 5 // @version      0.1
 6 // @description  try to take over the world!
 7 // @author       You
 8 // @match        http://mobile.yangkeduo.com/search_result.html?search_key*
 9 // @grant        unsafeWindow
10 // @require  https://code.jquery.com/jquery-3.6.0.min.js
11 // @require https://greasyfork.org/scripts/31940-waitforkeyelements/code/waitForKeyElements.js?version=209282 
12 
13 // ==/UserScript==
14 
15 waitForKeyElements ('[class="pdd-go-to-app"]', addbut);
16 function addbut() {
17     'use strict';
18     console.log('我的腳本加載了');
19     var button = document.createElement("button"); //創建一個input對象(提示框按鈕)
20     button.id = "id001";
21     button.textContent = "百度二下";
22     button.style.width = "60px";
23     button.style.height = "40px";
24     button.style.align = "center";
25 
26     //綁定按鍵點擊功能
27     button.onclick = function (){
28         console.log('點擊了按鍵');
29         //為所欲為 功能實現處
30         alert("你好");
31         return;
32     };
33 
34     var x = document.querySelector('[class="pdd-go-to-app"]');
35     //在瀏覽器控制台可以查看所有函數,ctrl+shift+I 調出控制台,在Console窗口進行實驗測試
36     x.appendChild(button);
37 
38     //var y = document.getElementById('s_btn_wr');
39     //y.appendChild(button);
40 };

問題解決是根據這篇文章:https://www.thinbug.com/q/12897446獲得解決方案

引用的js來自  #引用:https://greasyfork.org/zh-CN/scripts/31940-waitforkeyelements/code

關鍵點在於:10,11,15,16行做了更改

waitForKeyElements ("YOUR_jQUERY_SELECTOR", actionFunction);

  

var data_list=Array();data_list.push(["標題","鏈接","\n"])


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM