Last active
August 18, 2022 04:55
-
-
Save towry/f61b199365ddaa91e7d32230638bf94e to your computer and use it in GitHub Desktop.
[油猴脚本] 下载小程序绑定的公众号数据.tampermonkey
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name DownloadWeappLinkPage | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description 下载小程序绑定的公众号数据 | |
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js | |
// @author Towry Wang (github.com/towry) | |
// @match https://mp.weixin.qq.com/wxamp/basicprofile/relation?* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=qq.com | |
// @grant GM_notification | |
// ==/UserScript== | |
const KeyEle = '.js_qywechat_question'; | |
const apiUrl = "https://mp.weixin.qq.com/wxamp/cgi/route"; | |
waitForKeyElements (KeyEle, function(jNode) { | |
'use strict'; | |
if (!jNode.closest('.mod_default_hd').find('h4:contains("关联的公众号")').length) { | |
return; | |
} | |
var btn = document.createElement('button'); | |
btn.setAttribute('id', 'download-binded-wechat-account'); | |
btn.addEventListener('click', download); | |
btn.style.display = 'inline-block'; | |
btn.setAttribute('class', 'btn btn_default'); | |
btn.textContent = "下载数据"; | |
btn.style.marginLeft = '10px'; | |
jNode.closest('.mod_default_hd').append(btn); | |
let isDownloading = false; | |
let offset = 0; | |
let dataSet = []; | |
const query = window.location.search; | |
function updateDownloadInfoOnBtn(pageNo) { | |
btn.textContent = "正在下载第 " + pageNo + " 页"; | |
} | |
function resetBtnText() { | |
btn.textContent = "下载数据"; | |
} | |
function exportData(data) { | |
var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(data)); | |
var dlAnchorElem = document.createElement('a'); | |
dlAnchorElem.setAttribute("href", dataStr ); | |
dlAnchorElem.setAttribute("download", "weapp-link-page.json"); | |
dlAnchorElem.click(); | |
setTimeout(() => { | |
dlAnchorElem.remove(); | |
}, 600); | |
} | |
function download() { | |
if (isDownloading) { | |
return; | |
} | |
dataSet = []; | |
isDownloading = true; | |
GM_notification({ | |
title: "提示", | |
text: "下载中...", | |
timeout: 5000, | |
highlight: true, | |
}); | |
function doGet() { | |
return new Promise((resolve, reject) => { | |
let apiQuery = '/wxopen/basicprofile' + query + '&limit=10&action=weapp_link_page' + (offset ? '&offset=' + offset : ''); | |
let apiFullUrl = apiUrl + query + '&path=' + encodeURIComponent(apiQuery); | |
updateDownloadInfoOnBtn(offset / 10 + 1); | |
$.ajax({ | |
url: apiFullUrl, | |
dataType: "json", | |
type: "GET", | |
xhrFields: { | |
withCredentials: true, | |
} | |
}).done((res) => { | |
if (!res || !res.items || !res.items.length) { | |
return reject(new Error('empty')); | |
} | |
const items = res.items; | |
resolve(items); | |
}).fail((err) => { | |
reject(err); | |
}); | |
}); | |
}; | |
function doWhile() { | |
return doGet().then(items => { | |
dataSet = dataSet.concat(items); | |
offset += 10; | |
return doWhile(); | |
}); | |
} | |
doWhile().catch(() => { | |
isDownloading = false; | |
resetBtnText(); | |
GM_notification({ | |
title: "✅ 提示", | |
text: "下载完成,开始下载...", | |
timeout: 5000, | |
highlight: true, | |
}); | |
exportData(dataSet); | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment