PageSourceSearch

https://ithu.tw/static/util.js

js ithu.tw collected 2026-09-26 03:30:08 UTC 2,921 bytes, 100 lines download raw bytes

1function timeFormatter(unixTimestamp) {
2	if (unixTimestamp === null) {
3		return null;
4	}
5	var d = new Date(unixTimestamp * 1000);
6	d.setTime(d.getTime() - d.getTimezoneOffset() * 60 * 1000);
7	return formatDate(d, '%Y-%M-%d %H:%m');
8}
9
10function formatDate(date, fmt) {
11	function pad(value) {
12		return (value.toString().length < 2) ? '0' + value : value;
13	}
14
15	return fmt.replace(/%([a-zA-Z])/g, function (_, fmtCode) {
16		switch (fmtCode) {
17			case 'Y':
18				return date.getUTCFullYear();
19			case 'M':
20				return pad(date.getUTCMonth() + 1);
21			case 'n':
22				return date.getUTCMonth() + 1;
23			case 'd':
24				return pad(date.getUTCDate());
25			case 'H':
26				return pad(date.getUTCHours());
27			case 'm':
28				return pad(date.getUTCMinutes());
29			case 's':
30				return pad(date.getUTCSeconds());
31			default:
32				throw new Error('Unsupported format code: ' + fmtCode);
33		}
34	});
35}
36
37function long2ip(ip) {
38	//  discuss at: http://locutus.io/php/long2ip/
39	// original by: Waldo Malqui Silva (http://waldo.malqui.info)
40	if (!isFinite(ip)) {
41		return false;
42	}
43	return [ip >>> 24, ip >>> 16 & 0xFF, ip >>> 8 & 0xFF, ip & 0xFF].join('.')
44}
45
46//http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
47function getParameterByName(name, url) {
48	if (!url) {
49		url = window.location.href;
50	}
51	name = name.replace(/[\[\]]/g, "\\$&");
52	var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
53		results = regex.exec(url);
54	if (!results) return null;
55	if (!results[2]) return '';
56	return decodeURIComponent(results[2].replace(/\+/g, " "));
57}
58
59// https://stackoverflow.com/questions/736513/how-do-i-parse-a-url-into-hostname-and-path-in-javascript
60function isURL(url) {
61	var parser = document.createElement('a');
62	parser.href = url;
63
64	var protocal = parser.protocol; // => "http:"
65	var host = parser.host;         // => "example.com:3000"
66	var hostname = parser.hostname; // => "example.com"
67	var port = parser.port;         // => "3000"
68	var pathname = parser.pathname; // => "/pathname/"
69	var hash = parser.hash;         // => "#hash"
70	var search = parser.search;     // => "?search=test"
71	var origin = parser.origin;     // => "http://example.com:3000"
72	if (protocal === "http:" || protocal === "https:") {
73		return hostname !== '';
74	}
75	return false;
76}
77
78function getCookieByName(name) {
79	var nameEQ = name + "=";
80	var ca = document.cookie.split(';');
81	for (var i = 0; i < ca.length; i++) {
82		var c = ca[i];
83		while (c.charAt(0) === ' ') c = c.substring(1, c.length);
84		if (c.indexOf(nameEQ) === 0) return c.substring(nameEQ.length, c.length);
85	}
86	return null;
87}
88
89function isCSRFSafeMethod(method) {
90	// these HTTP methods do not require CSRF protection
91	return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
92}
93
94$.ajaxSetup({
95	beforeSend: function (xhr, settings) {
96		if (!isCSRFSafeMethod(settings.type) && !this.crossDomain) {
97			xhr.setRequestHeader("X-CSRF-Token", getCookieByName('csrf_token'));
98		}
99	}
100});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.