Greasy Fork is available in English.
Sin Helper
当前为
此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.icu/scripts/471561/1224582/SinHelper.js
; (function () {
window.sinHelper = {
/* 网络拦截相关 */
Xhr: {
init: function () {
let oldSend = XMLHttpRequest.prototype.send, _this = this;
XMLHttpRequest.prototype.send = function () {
let xhr = this
this.addEventListener("load", function () {
if (xhr.readyState != 4 || xhr.status != 200) return;
if (xhr.responseType != '' && xhr.responseType != 'text') return;
xhr.requestData = arguments
xhr.responseHeders = xhr.getAllResponseHeaders()
xhr.responseData = function () {
try {
return JSON.parse(xhr.responseText)
} catch (e) {
return xhr.responseText
}
}();
_this.dispatchFetch(xhr)
})
oldSend.apply(this, arguments);
}
},
rules: {}, // pathname => function
routes: [], // functions
registRules: function (ruleName, ruleFunc) {
if (typeof (ruleFunc) != 'function') {
console.error("Xhr Rules 必须是可调用的函数", ruleName)
return
}
this.rules[ruleName] = ruleFunc
},
/* 调用注册的 pathname 处理函数 */
dispatchFetch: function (xhr) {
let _this = this
const url = new URL(xhr.responseURL)
if (_this.rules[url.pathname]) {
if (typeof (this.rules[url.pathname]) == 'function') {
this.rules[url.pathname](xhr)
} else {
console.error("Xhr 处理函数错误:", url.pathname)
}
} else {
_this.routes.forEach((route) => {
if (typeof (route) == 'function') {
route(xhr)
}
})
}
}
},
/* 原生JS 下载Excel */
Excel: {
'trans2Base64': function (content) {
return window.btoa(unescape(encodeURIComponent(content)));
},
'exportExcelFromFront': function (params) {
let _this = this
const { cellList, headerList, caption, exportName = 'exportName' } = params;
const captionEle = caption ? `<caption>${caption}</caption>` : ''; // 表格标题
const headerEle = `<tr>${headerList?.map((item) => `<th>${item}</th>`)?.join('')}</tr>`;
const cellEle = cellList
?.map((itemRow) => `<tr>${itemRow?.map((itemCell) => `<td>${itemCell}</td>`)?.join('')}</tr>`)
?.join('');
const excelContent = `${captionEle}${headerEle}${cellEle}`;
let worksheet = '工作表1';
let excelFile =
"<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>";
excelFile +=
'<head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head>';
excelFile += "<body><table width='10%' border='1'>";
excelFile += excelContent;
excelFile += '</table></body>';
excelFile += '</html>';
const link = `data:application/vnd.ms-excel;base64,${_this.trans2Base64(excelFile)}`;
const a = document.createElement('a');
a.download = `${exportName}.xlsx`;
a.href = link;
a.click();
}
}
};
})();