GetPost
// ==UserScript==
// @name GetPost
// @namespace http://tampermonkey.net/
// @version 0.1
// @description 获取post请求内容
// @author haokj
// @match https://fund.cingta.com/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=microsoft.com
// @require http://localhost:8080/gm/libs/jquery/jquery-1.12.1.min.js
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_xmlhttpRequest
// ==/UserScript==
// 获取当前时间戳
function getTime(){
let t = new Date();
console.log(t);
}
// 下载json文件
function downloadJson(fileName, json) {
const jsonStr = (json instanceof Object) ? JSON.stringify(json) : json;
const url = window.URL || window.webkitURL || window;
const blob = new Blob([jsonStr]);
const saveLink = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
saveLink.href = url.createObjectURL(blob);
saveLink.download = fileName;
saveLink.click();
}
function downloadCsv(fileName, jsonData){
//列标题,逗号隔开,每一个逗号就是隔开一个单元格
let str = `资助金额,资助机构,单位,项目编号,关键词,负责人,受资机构,项目状态,申报领域,立项年份,电话,邮箱,项目\n`;
for(let i = 0 ; i < jsonData.length ; i++ ){
for(let item in jsonData[i]){
str+=`${jsonData[i][item] + '\t'},`;
}
str+='\n';
}
//encodeURIComponent解决中文乱码
let uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(str);
//通过创建a标签实现
var link = document.createElement("a");
link.href = uri;
//对下载的文件命名
link.download = fileName;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
(function() {
'use strict';
// 获取btn按钮
var btn = document.querySelector("#app > div:nth-child(2) > div:nth-child(1) > div > div > div.container-left > div.result-content > div.result-search > div.clearfix.search-input-wrp > div.left.p-y.btn-wrap > button");
// 点击按钮事件
btn.onclick = function(){
// 获取检索关键字
var key =document.querySelector(".keyword-input");
// 构造参数信息
var d={
pageNo:1,
pageSize:10000,
keyword:key.value,
orderby:1,
pwd:1
};
// 异步提交请求
$.ajax({
type:"POST",
url:'https://fund.cingta.com/api/query-fund/',
data:d,
dataType:'json',
contentType: 'application/x-www-form-urlencoded',
success:function(req){
downloadCsv(key.value + '.csv',req.data.query.data);
console.log(req.data.query.data);
},
error:function(e){
console.log(e)
}
})
}
// Your code here...
})();