This commit is contained in:
shanmite 2020-11-11 15:03:34 +08:00
parent c873b6276c
commit acc74fd758
4 changed files with 264 additions and 5 deletions

View File

@ -5,13 +5,10 @@ on:
- main
- master
schedule:
- cron: '51 0-23 * * *'
- cron: '5 0-23 * * *'
jobs:
Start:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
steps:
- name: 'Checkout codes'
uses: actions/checkout@v2

125
.vscode/httpRequest.code-snippets vendored Normal file
View File

@ -0,0 +1,125 @@
{
"HttpRequest-get": {
"scope": "javascript,typescript",
"prefix": "hrget",
"body": [
"HttpRequest({",
" type: 'GET',",
" _url: '$1',",
" _query_string: {",
" $2",
" },",
" headers: {",
" 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',",
" Accept: 'application/json, text/plain, */*',",
" Cookie: '$3',",
" },",
" success: chunk => {",
" $4",
" },",
" error: err => {",
" $5",
" }",
"})$6"
],
"description": "简化http-get请求"
},
"HttpRequest-post": {
"scope": "javascript,typescript",
"prefix": "hrpost",
"body": [
"HttpRequest({",
" type: 'POST',",
" _url: '$1',",
" contents: {",
" $2",
" },",
" headers: {",
" 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',",
" Accept: 'application/json, text/plain, */*',",
" 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',",
" Cookie: '$3',",
" },",
" success: chunk => {",
" $4",
" },",
" error: err => {",
" $5",
" }",
"})$6"
],
"description": "简化http-post请求"
},
"CORSajax-get": {
"scope": "javascript,typescript",
"prefix": "corsget",
"body": [
"CORSajax.get({",
" proxy_link: '$1',",
" proxy_querystring: {",
" $2",
" },",
" proxy_headers: {",
" Cookie: '$3'",
" },",
"},chunk => {",
" $4",
"})$5"
],
"description": "简化跨域Ajax本地nodejsのget代理"
},
"CORSajax-post": {
"scope": "javascript,typescript",
"prefix": "corspost",
"body": [
"CORSajax.post({",
" proxy_link: '$1',",
" proxy_headers: {",
" 'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',",
" Cookie: '$2'",
" },",
" proxy_body: {",
" $3",
" },",
"},chunk => {",
" $4",
"})$5"
],
"description": "简化跨域Ajax本地nodejsのpost代理"
},
"Ajax-get": {
"scope": "javascript,typescript",
"prefix": "myget",
"body": [
"Ajax.get({",
" url: '$1',",
" queryStringsObj: {",
" $2",
" },",
" hasCookies: true,",
" success: responseText => {",
" $3",
" }",
"})",
],
"description": "简化AjaxのGet请求"
},
"Ajax-post": {
"scope": "javascript,typescript",
"prefix": "mypost",
"body": [
"Ajax.post({",
" url: '$1',",
" hasCookies: true,",
" dataType: 'application/x-www-form-urlencoded',",
" data: {",
" $2",
" },",
" success: responseText => {",
" $3",
" }",
"})",
],
"description": "简化Ajaxのpost请求"
},
}

25
main.js
View File

@ -1,2 +1,25 @@
import {HttpRequest} from './node/HttpRequest';
const COOKIE = (process.argv.slice(2))[0].substr(7);
console.log('say: '+COOKIE);
HttpRequest({
type: 'POST',
_url: 'https://api.bilibili.com/x/v2/reply/add',
contents: {
oid:'456295362727813281',
type:17,
message:`这是我的一小步,却是人类的一大步(Github ation)\n${COOKIE}`,
jsonp:'jsonp',
csrf:'e12a61f98c9dc9ae26e68ef4e472a833'
},
headers: {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36',
Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
Cookie: 'SESSDATA=266867a2%2C1612919172%2C085b8*81; ',
},
success: chunk => {
console.log(chunk);
},
error: err => {
console.log(err);
}
})

114
node/HttpRequest.js Normal file
View File

@ -0,0 +1,114 @@
const { request } = require('http');
const { stringify } = require('querystring');
/**
* 简化nodejs发送http请求的步骤
* @param {object} obj
* @member type
* 请求的方法
* @member _url
* 请求的完整链接(若有查询字符串则为'?'之前的内容)
* @member _query_string
* 键值对形式的查询字符串(值为对象)
* @member contents
* 键值对形式的请求体(值为对象)
* @member headers
* 键值对形式的请求头(值为对象)
* @member success
* 请求成功执行的方法
* @member error
* 请求失败执行的方法
*/
export const HttpRequest = obj => {
const type = obj.type;
const _url = obj._url;
const headers = obj.headers;
const _query_string = obj._query_string;
const contents = formatContents(headers['Content-Type'],obj.contents);
(()=>{
const reg = /^([hH][tT]{2}[pP]:\/\/|[hH][tT]{2}[pP][sS]:\/\/)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~/])+$/;
if (!reg.test(_url)) {
console.log('url无效');
return;
}
})()
/**
* 处理options
*/
let options = {
host: /(?<=https?:\/\/)[a-zA-Z.]*(?=\/)/.exec(_url)[0],
path: /(?<=https?:\/\/.*)\/.*/.exec(_url)[0],
headers: headers,
};
let query_string = '';
switch (type) {
case 'get':
case 'GET':
options.method = 'GET';
if (typeof _query_string != 'undefined') {
query_string = stringify(_query_string);
}
if (query_string != '') {
let url = _url + '?' + query_string;
options.path = /(?<=https?:\/\/.*)\/.*/.exec(url)[0];
}
break;
case 'post':
case 'POST':
options.method = 'POST';
options.headers['Content-Length'] = contents.length;
if (typeof headers['Content-Type'] =='undefined') {
options.headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=utf-8';
}
break;
default:
console.log('请检查传入HttpRequest方法的对象中的type属性');
break;
}
/**
* 发送Http请求
*/
let req = request(options, res => {
let protodata = '';
if (res.statusCode == 200) {
// console.log('真·服务器返回的响应头');
// console.log(res.headers);
res.setEncoding('utf8');
res.on('data', chunk => {
protodata += chunk
})
res.on('end', () => {
obj.success(protodata)
})
} else {
console.log(`${res.statusCode} RESPEND ERROR!`);
obj.error(`服务器拒绝了你的请求`);
}
});
if (type == 'POST') {
req.write(contents)
}
req.on('error', () => {
console.error("REQUEST ERROR!")
obj.error(`请求失败\n${_url}是无效的url`);
});
req.end()
}
/**
* 处理请求体
* 默认url编码字符串
* @param {string} contentstype 请求的内容格式
* @param {object} contents 请求体
* @returns 格式化字符串
*/
function formatContents(contentstype,contents) {
if (typeof contents == 'undefined') {
return ''
}
if (/application\/x-www-form-urlencoded/i.test(contentstype)) {
return stringify(contents)
}
if (/application\/json/i.test(contentstype)) {
return JSON.stringify(contents)
}
return stringify(contents);
}