Skip to content

Commit

Permalink
Add basic request support for node
Browse files Browse the repository at this point in the history
  • Loading branch information
qsniyg committed Dec 1, 2024
1 parent b0a51b9 commit 6a611ad
Show file tree
Hide file tree
Showing 2 changed files with 201 additions and 2 deletions.
109 changes: 108 additions & 1 deletion src/userscript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1967,6 +1967,104 @@ var $$IMU_EXPORT$$;
}
} else if (typeof(GM) !== "undefined" && typeof(GM.xmlHttpRequest) !== "undefined") {
do_request_raw = GM.xmlHttpRequest;
} else if (is_node) {
do_request_raw = function(request) {
if (_nir_debug_) {
console_log("do_request_node", request);
}

let http = require("http");
let https = require("https");

var method = request.method || "GET";

let our_http = http;
if (/^https:/.test(request.url))
our_http = https;

let hostinfo = request.url.match(/^[a-z]+:\/\/([^/]+)(?:\/+.*)?$/)[1];
let port = void 0;
let hostname = hostinfo;
let portmatch = hostinfo.match(/^(.*):([0-9]+)$/);
if (portmatch) {
hostname = portmatch[1];
port = portmatch[2]|0;
}
let path = request.url.match(/^[a-z]+:\/\/[^/]+(\/.*)?$/)[1] || "/";

let req = our_http.request({
hostname: hostname,
port: port,
method: method,
path: path,
// FIXME: add realistic browser headers, cookie jar
headers: request.headers
}, res => {
let chunks = [];
res.on("data", chunk => {
chunks.push(chunk);
});
let done_final = false;
let finalcb = () => {
if (done_final)
return;
done_final = true;

let rawdata = Buffer.concat(chunks);

let headersstrs = [];
for (let i = 0; i < res.rawHeaders.length; i += 2) {
headersstrs.push([res.rawHeaders[i] + ": " + res.rawHeaders[i+1]].join(""));
}
let headersstr = headersstrs.join("\r\n");

let resp = {
readyState: 4,
finalUrl: request.url, // FIXME
responseHeaders: headersstr,
responseType: "buffer",
response: null,
responseText: null,
status: res.statusCode,
statusText: res.statusMessage
};

resp.response = rawdata;

try {
resp.responseText = rawdata.toString("utf8"); // FIXME
} catch (e) {}

if (req.destroyed) {
request.onabort();
} else if (!req.complete) {
request.onerror(resp);
} else {
// FIXME: more onerror?
request.onload(resp);
}
};
// TODO: onprogress
// TODO: ontimeout
res.on("close", () => {
finalcb();
});
res.on("end", () => {
finalcb();
});
});

if (request.data)
req.write(request.data);

req.end();

return {
abort: function() {
req.abort();
}
};
};
}

if (is_extension && !is_extension_bg) {
Expand Down Expand Up @@ -137568,11 +137666,20 @@ var $$IMU_EXPORT$$;

let url = parsed._positional[0];

bigimage_recursive(url, {
let window = {
location: {
href: url
}
};

bigimage_recursive_loop(url, {
fill_object: true,
window: window,
cb: function(obj) {
console.log(JSON_stringify(obj, null, '\t'));
}
}, function(obj, finalcb) {
console.log("BRL", obj);
});
};

Expand Down
94 changes: 93 additions & 1 deletion userscript.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -1640,6 +1640,90 @@ var $$IMU_EXPORT$$;
}
} else if (typeof (GM) !== "undefined" && typeof (GM.xmlHttpRequest) !== "undefined") {
do_request_raw = GM.xmlHttpRequest;
} else if (is_node) {
do_request_raw = function(request) {
if (_nir_debug_) {
console_log("do_request_node", request);
}
var http = require("http");
var https = require("https");
var method = request.method || "GET";
var our_http = http;
if (/^https:/.test(request.url))
our_http = https;
var hostinfo = request.url.match(/^[a-z]+:\/\/([^/]+)(?:\/+.*)?$/)[1];
var port = void 0;
var hostname = hostinfo;
var portmatch = hostinfo.match(/^(.*):([0-9]+)$/);
if (portmatch) {
hostname = portmatch[1];
port = portmatch[2] | 0;
}
var path = request.url.match(/^[a-z]+:\/\/[^/]+(\/.*)?$/)[1] || "/";
var req = our_http.request({
hostname: hostname,
port: port,
method: method,
path: path,
// FIXME: add realistic browser headers, cookie jar
headers: request.headers
}, function(res) {
var chunks = [];
res.on("data", function(chunk) {
chunks.push(chunk);
});
var done_final = false;
var finalcb = function() {
if (done_final)
return;
done_final = true;
var rawdata = Buffer.concat(chunks);
var headersstrs = [];
for (var i = 0; i < res.rawHeaders.length; i += 2) {
headersstrs.push([res.rawHeaders[i] + ": " + res.rawHeaders[i + 1]].join(""));
}
var headersstr = headersstrs.join("\r\n");
var resp = {
readyState: 4,
finalUrl: request.url, // FIXME
responseHeaders: headersstr,
responseType: "buffer",
response: null,
responseText: null,
status: res.statusCode,
statusText: res.statusMessage
};
resp.response = rawdata;
try {
resp.responseText = rawdata.toString("utf8"); // FIXME
} catch (e) { }
if (req.destroyed) {
request.onabort();
} else if (!req.complete) {
request.onerror(resp);
} else {
// FIXME: more onerror?
request.onload(resp);
}
};
// TODO: onprogress
// TODO: ontimeout
res.on("close", function() {
finalcb();
});
res.on("end", function() {
finalcb();
});
});
if (request.data)
req.write(request.data);
req.end();
return {
abort: function() {
req.abort();
}
};
};
}
if (is_extension && !is_extension_bg) {
// used for clearing menu items
Expand Down Expand Up @@ -121990,11 +122074,19 @@ var $$IMU_EXPORT$$;
return;
}
var url = parsed._positional[0];
bigimage_recursive(url, {
var window = {
location: {
href: url
}
};
bigimage_recursive_loop(url, {
fill_object: true,
window: window,
cb: function(obj) {
console.log(JSON_stringify(obj, null, '\t'));
}
}, function(obj, finalcb) {
console.log("BRL", obj);
});
};
function start() {
Expand Down

0 comments on commit 6a611ad

Please sign in to comment.