代码片段

js代码片段view raw
/*
* @Author: nooldey
* @Date: 2018-01-11 13:27:47
* @Last Modified by: nooldey
* @Last Modified time: 2018-03-03 16:06:58
*/
/***功能:屏蔽鼠标右键、复制、粘贴、CTRL、ALT、SHIFT、F2~F12等,仅建议对文章内容独特性要求或不愿意被随意复制内容的站点***/
/* ---------jQuery-------- */
//屏蔽鼠标右键、ALT翻页、CTRL+N、CTRL+R、F2~F12、SHIFT+左键
jQuery(document).ready(function($){
$(document).bind("contextmenu",function(){return false;});
$(document).bind("selectstart",function(){return false;});
$(document).keydown(function(){return key(arguments[0])});
function key(e){
var keyCode;
if(window.event) //IE
{
keyCode = e.keyCode;
}
else if(e.which) //firefox/opera/chrome/netscape
{
keyCode = e.which;
}
if(
(keyCode==112)|| //F2
(keyCode==113)|| //F2
(keyCode==114)|| //F3
(keyCode==115)|| //F4
// (keyCode==116)|| //F5
(keyCode==117)|| //F6
(keyCode==118)|| //F7
(keyCode==119)|| //F8
(keyCode==120)|| //F9
(keyCode==121)|| //F10
// (keyCode==122)|| //F11
(keyCode==123)|| //F12
(keyCode==17)|| //CTRL
(keyCode==16) //shift
//此处填写后续条件
){
alert("别再按了,你节操碎了!");
return false;
}
}
});

/* -----------原生------------ */
/*禁止复制*/
function noCopy(){
/* 禁止右键复制粘贴 */
document.oncontextmenu = function(){return false;}
document.onselectstart = function(){return false;}
function key(e) {
var keyCode;
if (window.event) {
//IE
keyCode = e.keyCode;
} else if (e.which) {
//firefox/opera/chrome/netscape
keyCode = e.which;
}
/*
112 //F2
113 //F2
114 //F3
115 //F4
116 //F5
117 //F6
118 //F7
119 //F8
120 //F9
121 //F10
122 //F11
123 //F12
17 //CTRL
16 //shift
*/
//此处填写后续条件
if([112,113,114,115,117,118,119,120,121,122,123,17,16].indexOf(keyCode) > -1){
console.log("别再按了,你节操碎了!");
return false;
}
}
document.onkeydown = function(){
return key(arguments[0])
}
}
js代码片段view raw
/*
* @Author: zws <nooldey@gmail.com>
* @Date: 2018-04-29 17:06:20
* @Last Modified by: zws
* @Description: 生成随机密码串
*/

const generate = (l) => {
const
{ floor, random } = Math,
zd = ['ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz', '0123456789'],
z = zd.join(''),
len = z.length
let
r1 = zd[0][floor(random() * zd[0].length)],
r2 = floor(random() * 10),
r3 = ''+r1+r2,
result = ''

for (let i = 0; i < l - 2; i++) {
let rand = floor(random() * len)
r3 += z[rand]
}

for (let j = 0; j < l; j++) {
let n = floor(random() * r3.length)
result += r3[n]
r3.slice(n,1)
}

return result
}

console.log(generate(8))
js代码片段view raw
/*
* @Author: zws <nooldey@gmail.com>
* @Date: 2018-01-10 09:06:09
* @Last Modified by: zws
* @Last Modified time: 2021-04-29 17:02:56
* @Description: 获取历史日期的各种格式和计算
*/

/*1. 获取去年的今天*/

function zws_last() {
/*获取去年今日*/
let t = new Date();
let nt = new Date();
if (t.getMonth() + 1 == 2 && t.getDate() == 29) {
nt.setMonth(t.getMonth() + 1)
nt.setDate(1)
}
nt.setFullYear(t.getFullYear() - 1)
return formatDate(nt, 'y-M-d');
}

/*2. 获取前N天的日期*/

function zws_before(n) {
/*获取几天前*/
return zeroTime = Math.round(Date.now() - 1000 * 3600 * 24 * n);
}

/*3. 将时间戳转为文字-N久前*/

function zws_along(t) {
// 计算距离当前日期为:X天前
//设置基准参数
let minute = 1000 * 60;
let hour = minute * 60;
let day = hour * 24;
let month = day * 30;
//获取当前时间,计算区间
let n = new Date().getTime();
let dif = n - t;
if (dif < 0) return;
let m = dif / month;
let w = dif / (7 * day);
let d = dif / day;
let h = dif / hour;
let min = dif / minute;
let txt = '';
// FROM: ZHUWEISHENG.COM.CN
if (m >= 1) {
txt = parseInt(m) + '月前';
} else if (w >= 1) {
txt = parseInt(w) + '周前';
} else if (d >= 1) {
txt = parseInt(d) + '天前';
} else if (h >= 1) {
txt = parseInt(h) + '小时前';
} else if (min >= 1) {
txt = parseInt(min) + '分钟前';
} else {
txt = '刚刚'
}
return txt;
}
js代码片段view raw
/*
* @Author: nooldey
* @Date: 2018-01-06 16:57:15
* @Last Modified by: zws
* @Last Modified time: 2021-04-29 17:05:09
* @description: 针对时间的各种格式化
*/

/* 时间格式化 */
const formatDate = (stamp, format = 'y-M-d') => {
/* 日期格式化,不转时区 */
if (!stamp) return;
try {
let d = new Date(stamp);
const fillzero = t => t > 9 ? t : ('0' + t);
let year = d.getFullYear(),
month = fillzero(d.getMonth() + 1),
date = fillzero(d.getDate()),
hour = fillzero(d.getHours()),
min = fillzero(d.getMinutes()),
sec = fillzero(d.getSeconds());
let pre = '',
aft = ''; // 输出时间的两个部分,pre:日期部分,aft:时间部分
let ymd = format.match(/^y(.+?)M(.+?)d/); // 年月日 ymd
let hm = format.match(/h\:m$/); // 时分 hm
let hms = format.match(/h\:m\:s$/); // 时分秒 hms
if (ymd) {
let line = ymd[2] || '';
pre = [year, month, date].join(line);
}
if (hm) {
aft = [hour, min].join(':');
}
if (hms) {
aft = [hour, min, sec].join(':');
}
let output = [pre, aft].join(' ');
return output.trim();
} catch (error) {
console.log('time error', error)
return stamp;
}

}

/* 时间国际化处理 */
const zeroToLocal = (stamp, format = 'y-M-d') => {
/* 将传入的零时区的时间转化为当地时间显示,默认输出年月日 */
/*
* 参数:
* offset: 当前时区时间差 = 零时区时间-当地时间 单位:min
* eg: 中国时区+0800 时间差为-480(min),-480/60 = -8小时
* localtime: 转换后输出的当地时间
*/
if (!stamp) return;
/* 零时区转当地时间 */
try {
let d = new Date(stamp);
if (!d || d == 'Invalid Date') return stamp;
let offset = new Date().getTimezoneOffset();
let localtime = d.getTime() - offset * 60 * 1000;
let output = formatDate(localtime, format);
return output;
} catch (error) {
console.log('time error', error)
return stamp;
}
}


const localToZero = (stamp, format = 'y-M-d') => {
/* 将当地时间转化为零时区时间 */
/*
* 参数:
* offset: 当前时区时间差 = 零时区时间-当地时间 单位:min
* eg: 中国时区+0800 时间差为-480(min),-480/60 = -8小时
* zeroTime: 转换后输出的当地时间
*/
if (!stamp) return;
let d = new Date(stamp);
if (!d || d == 'Invalid Date') return stamp;
d = d.toJSON().substr(0, 19);
let [date, time] = d.split('T');
let [year, month, day] = date.split('-');
let [hour, min, sec] = time.split(':');
let output = '';
let pre = '',
aft = ''; // 输出时间的两个部分,pre:日期部分,aft:时间部分
let ymd = format.match(/^y(.+?)M(.+?)d/); // 年月日 ymd
let hm = format.match(/h\:m$/); // 时分 hm
let hms = format.match(/h\:m\:s$/); // 时分秒 hms
if (ymd) {
let line = ymd[2] || '';
pre = [year, month, day].join(line);
}
if (hm) {
aft = [hour, min].join(':');
}
if (hms) {
aft = [hour, min, sec].join(':');
}
output = [pre, aft].join(' ');
return output.trim();
}
js代码片段view raw
/*
* @Author: zws <nooldey@gmail.com>
* @Date: 2021-04-29 17:05:30
* @Last Modified by: zws
* @Description: 复制本地存储的信息到剪贴板
*/

javascript: void((function () {
var allKeys = Object.keys(localStorage).filter(item => !/(:\/\/|preview|swagger|webpack|null)/i.test(item));
var key = allKeys.length ? allKeys : ['devToken', 'token', 'devInfo', 'user'];
var result = {};
for (let index = 0; index < key.length; index++) {
var k = key[index];
var ctx = localStorage.getItem(k) || '';
result[k] = ctx;
};
var contentValue = JSON.stringify(result);
if (window.clipboardData) {
window.clipboardData.setData("Text", contentValue);
console.log('已复制' + key);
} else if (document.execCommand) {
var el = document.createElement('textarea');
el.setAttribute('style', 'width:0;height:0;');
el.value = contentValue;
document.body.appendChild(el);
el.focus();
if (el.setSelectionRange) {
el.setSelectionRange(0, contentValue.length);
} else {
el.select();
}
document.execCommand('copy');
document.body.removeChild(el);
console.log('已复制' + key);
} else {
console.clear();
console.log(key + '的值:');
console.log(contentValue);
alert('无法自动复制,请打开控制台复制输出的内容');
}
})())
js代码片段view raw
/*
* @Author: zws <nooldey@gmail.com>
* @Date: 2021-04-29 17:06:03
* @Last Modified by: zws
* @Description: 将剪贴板信息写入到本地存储
*/

javascript: void((function () {
if (window.clipboardData && window.clipboardData.items) {
/* IE */
var items = window.clipboardData.items;
for (var i = 0; i < items.length; i++) {
var item = items[i];
if (item.kind === 'string') {
item.getAsString(function (str) {
var result = JSON.parse(str);
Object.keys(result).filter(item => !/:\/\//.test(item)).forEach(k => {
localStorage.setItem(k, result[k]);
console.log('设置' + k + '成功!');
});
});
};
};
} else {
/* Other */
var title = '请粘贴内容:';
var content = prompt(title, '');
if (content && content.length) {
var result = JSON.parse(content);
Object.keys(result).forEach(k => {
localStorage.setItem(k, result[k]);
console.log('设置' + k + '成功!');
});
};
}
})())
css代码片段view raw
/*
* @Author: zws <nooldey@gmail.com>
* @Date: 2021-04-29 16:59:05
* @Last Modified by: zws
* @Description: 隐藏input type=number 的输入框上下箭头
*/

input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button {
appearance: none;
-webkit-appearance: none;
}

input[type="number"] {
-moz-appearance: textfield;
}

文章目录