extend.js 4.56 KB
/**
 * String extend
 */
(() => {
  const reflectMap = {
    '大': '小',
    '多': '少',
    '上': '下',
    '左': '右',
    '前': '后',
    '冷': '热',
    '高': '低',
    '进': '退',
    '黑': '白',
    '天': '地',
    '男': '女',
    '里': '外',
    '死': '活',
    '公': '私',
    '快': '慢',
    '矛': '盾',
    '宽': '窄',
    '强': '弱',
    '轻': '重',
    '缓': '急',
    '松': '紧',
    '好': '坏',
    '美': '丑',
    '善': '恶',
    '是': '否',
    '闲': '忙',
    '来': '去',
    '分': '合',
    '存': '亡',
    '动': '静',
    '浓': '淡',
    '偏': '正',
    '饥': '饱',
    '爱': '恨',
    '升': '降',
    '开': '关',
    '始': '终',
    '胖': '瘦',
    '迎': '送',
    '盈': '亏',
    '真': '假',
    '虚': '实',
    '有': '无',
    '雅': '俗',
    '稀': '密',
    '粗': '细',
    '东': '西',
    '巧': '拙',
    '恩': '怨',
    '新': '旧',
    '正': '邪',
    '通': '堵',
    '止': '行',
    '古': '今',
    '张': '弛',
    '曲': '直',
    '亮': '暗',
    '亲': '疏',
    '输': '赢',
    '逆': '顺',
    '苦': '甜',
    '忠': '奸',
    '纵': '横',
    '得': '失',
    '南': '北',
    '薄': '厚',
    '哭': '笑',
    '文': '武',
    '推': '拉',
    '问': '答',
    '主': '仆',
    '买': '卖',
    '深': '浅',
    '聚': '散',
    '干': '湿',
    '彼': '此',
    '生': '熟',
    '单': '双',
    '首': '末',
    '敌': '友',
    '警': '匪',
    '盛': '衰',
    '胜': '败',
    '加': '减',
    '软': '硬',
    '阴': '阳',
    '顺': '逆',
    '反': '正',
    '祸': '福',
    '信': '疑',
    '错': '对',
    '藏': '露',
    '老': '少',
    '断': '续',
    '钝': '锐',
    '雌': '雄',
    '醒': '睡',
    '咸': '淡',
    '凉': '暖',
    '窄': '宽',
    '远': '近',
    '横': '竖',
    '涨': '降',
    '隐': '现',
    '同': '异',
    '浑': '清',
    '紧': '松',
    '无': '有',
    '明': '暗',
    '细': '粗',
    '嫩': '老',
    '凹': '凸',
    '浮': '沉',
    '朝': '暮'
  }

  for (const key in reflectMap) {
    reflectMap[reflectMap[key]] = key
  }

  String.prototype.disorder = function() {
    const chineseNum = ['一', '二', '三', '四', '五', '六', '七', '八', '九', '十']
    const chineseCall = ['他', '她', '它', '你', '我', '汝', '俺']
    const chineseAuxiliary = ['的', '地', '得']

    return this.replace(/\d/g, function() {
      return Math.random().toString().slice(-1)
    }).replace(/[a-z]/g, function(matches) {
      const arrEnMap1 = ['m', 'n']
      const arrEnMap2 = ['b', 'p', 'q']
      const arrEnMap3 = ['i', 'l']
      if (arrEnMap1.includes(matches)) {
        return arrEnMap1[Math.floor(arrEnMap1.length * Math.random())]
      }
      if (arrEnMap2.includes(matches)) {
        return arrEnMap1[Math.floor(arrEnMap2.length * Math.random())]
      }
      if (arrEnMap3.includes(matches)) {
        return arrEnMap3[Math.floor(arrEnMap3.length * Math.random())]
      }

      return matches
    }).replace(/[\u4e00-\u9fa5]/g, function(matches) {
      // 50%概率取反义词
      if (reflectMap[matches] && Math.random() > 0.5) {
        return reflectMap[matches]
      }

      if (chineseNum.includes(matches)) {
        return chineseNum[Math.floor(chineseNum.length * Math.random())]
      }
      if (chineseCall.includes(matches)) {
        return chineseCall[Math.floor(chineseCall.length * Math.random())]
      }
      if (chineseAuxiliary.includes(matches)) {
        return chineseAuxiliary[Math.floor(chineseAuxiliary.length * Math.random())]
      }

      return matches
    })
  }
})()

/**
 * 混淆 copy
 */
const disorderCopy = () => {
  document.addEventListener('copy', function(event) {
    const clipboardData = event.clipboardData || window.clipboardData
    if (!clipboardData) { return }
    const text = window.getSelection().toString()
    if (text) {
      event.preventDefault()
      clipboardData.setData('text/plain', text.disorder())
    }
  })
}

/**
 * 桌面通知
 * @param title
 * @param options
 */
const sendNotification = (title, options) => {
  if (window.Notification) {
    window.Notification.requestPermission().then(() => {
      const notice = new Notification(title, options)
      console.time(`${notice.title}_${notice.timestamp}`)

      notice.onclick = () => {
        notice.close()
      }

      notice.onclose = (e) => {
        console.timeEnd(`${notice.title}_${notice.timestamp}`)
      }

      setTimeout(() => {
        notice.close()
      }, 10000)
    })
  }
}

export {
  sendNotification,
  disorderCopy
}