MCPcopy
hub / github.com/hoothin/UserScripts / downloadPage

Function downloadPage

Picviewer CE+/Picviewer CE+.user.js:25557–25596  ·  view source on GitHub ↗
(url, post, headers, cb)

Source from the content-addressed store, hash-verified

25555 }
25556
25557 async function downloadPage(url, post, headers, cb) {
25558 var opts = {
25559 method: 'GET',
25560 url: url,
25561 onload: function(req) {
25562 try {
25563 if(req.status > 399) throw 'Server error: ' + req.status;
25564 else cb(req.responseText, req.finalUrl || url);
25565 } catch(ex) {
25566 if (!useFetch && req.responseHeaders.indexOf("cloudflare") != -1) {
25567 useFetch = true;
25568 return downloadPage(url, post, headers, cb);
25569 }
25570 handleError(ex);
25571 }
25572 },
25573 onerror: handleError
25574 };
25575 if (post) {
25576 opts.method = 'POST';
25577 opts.data = post;
25578 opts.headers = {'Content-Type':'application/x-www-form-urlencoded','Referer':url};
25579 }
25580 if (headers) {
25581 if (typeof headers == 'function') {
25582 headers = await headers(url + (post ? `#p{${post}}` : ""), xhr, getCookie);
25583 }
25584 opts.headers = headers;
25585 }
25586
25587 if (useFetch) {
25588 let fetchOption = {method: opts.method || 'GET', headers: opts.headers};
25589 if (opts.method && opts.method != 'GET') {
25590 fetchOption.body = opts.data || '';
25591 }
25592 fetch(opts.url, fetchOption).then(response => response.text()).then(data => {opts.onload({responseText: data})}).catch(e => opts.onerror(e));
25593 } else {
25594 _GM_xmlhttpRequest(opts);
25595 }
25596 }
25597
25598 function createDoc(text) {
25599 var doc = document.implementation.createHTMLDocument('PicViewerCE');

Callers 1

parsePageFunction · 0.70

Calls 1

fetchFunction · 0.85

Tested by

no test coverage detected