ic2api

虽然作者懒得写描述,但是他至少记得添加过一个默认描述……

Tento skript by neměl být instalován přímo. Jedná se o knihovnu, kterou by měly jiné skripty využívat pomocí meta příkazu // @require https://update.greasyfork.org/scripts/481658/1292161/ic2api.js

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

You will need to install an extension such as Tampermonkey to install this script.

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

/* eslint-disable no-multi-spaces */
/* eslint-disable no-return-assign */

// ==UserScript==
// @name               ic2api
// @namespace          PY-DNG userscripts
// @version            0.1
// @description        虽然作者懒得写描述,但是他至少记得添加过一个默认描述……
// @author             PY-DNG
// @license            GPL-3.0-or-later
// ==/UserScript==

/* global LogLevel DoLog Err $ $All $CrE $AEL $$CrE addStyle detectDom destroyEvent copyProp copyProps parseArgs escJsStr replaceText getUrlArgv dl_browser dl_GM AsyncManager */

let IC2API = (function __MAIN__() {
    'use strict';

	return {
		phoneSeatReserve: {
			duration() {
				return get('ic-web/phoneSeatReserve/duration');
			},
			reserve(duration) {
				return post('ic-web/phoneSeatReserve/duration', JSON.stringify({ duration }));
			}
		},
		seatMenu() {
			return get('ic-web/seatMenu');
		},
		reserve(roomIds, resvDates, sysKind) {
			return get('ic-web/reserve', { roomIds, resvDates, sysKind });
		},
	};

	function get(url, params) {
		return new Promise((resolve, reject) => {
			GM_xmlhttpRequest({
				method: 'GET',
				url: toAbsURL(url, params),
				headers: { 'Token': getToken() || '' },
				responseType: 'json',
				onload: res => {
					const data = res.response;
					res.status === 200 && data.code === 0 ? resolve(data) : reject(data);
				},
				onerror: err => {
					reject(err);
				}
			});
		});
	}

	function post(url, data) {
		return new Promise((resolve, reject) => {
			GM_xmlhttpRequest({
				method: 'POST',
				url: toAbsURL(url),
				headers: { 'Token': getToken() || '' },
				responseType: 'json',
				data,
				onload: res => {
					const data = res.response;
					res.status === 200 && data.code === 0 ? resolve(data) : reject(data);
				},
				onerror: err => {
					reject(err);
				}
			});
		});
	}

	function getToken() {
		const storage = sessionStorage.getItem("userInfo");
		const userInfo = storage ? JSON.parse(storage) : {}
		return userInfo.token;
	}

	function toAbsURL(pathname, searchOptions) {
		return new URL(pathname, `${location.protocol}//${location.host}`).href + (searchOptions ? `?${toSearch(searchOptions)}` : '');
	}

	function toSearch(options) {
		return new URLSearchParams(options).toString()
	}
})();