Skip to content

常见技巧

1.数据乱码转gb2312

js
  import iconv from 'iconv';
  import axios from 'axios';
  const resp = await axios.get(url, {
    responseType: 'arraybuffer', // node环境
  });
  return iconv.decode(resp.data, 'gb2312');

2.禁用https证书校验

js
import axios from 'axios';
const config = {
  method: 'POST',
  url: 'https://www.moedm.net/index.php/api/vod',
  headers: {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0',
  },
  rejectUnauthorized:false, //关闭证书校验
  data: {}
};
const resp = await axios(config)

3.字符串转对象

js
const code = '[1,3,4]'; // typeof string
const jsonCode = new Function(`return ${code}`)(); // typeof object

4.获取dom列表转字符串

常用于提取演员数据

js
const html = `
<div class="persons">
  <div class="person" itemprop="actor" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Elijah Wood">
      <div class="img">
          <a href="https://www.4kvm.org/cast/elijah-wood"><img alt="Elijah Wood isFrodo" src="https://gimg0.baidu.com/gimg/app=2001&amp;n=0&amp;g=0n&amp;fmt=jpeg&amp;src=image.tmdb.org/t/p/w92/7UKRbJBNG7mxBl2QQc5XsAh6F8B.jpg"></a>
      </div>
      <div class="data">
          <div class="name"><a itemprop="url" href="https://www.4kvm.org/cast/elijah-wood">Elijah Wood</a></div>
          <div class="caracter">Frodo</div>
      </div>
  </div>
  <div class="person" itemprop="actor" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Ian McKellen">
      <div class="img">
          <a href="https://www.4kvm.org/cast/ian-mckellen"><img alt="Ian McKellen isGandalf" src="https://gimg0.baidu.com/gimg/app=2001&amp;n=0&amp;g=0n&amp;fmt=jpeg&amp;src=image.tmdb.org/t/p/w92/5cnnnpnJG6TiYUSS7qgJheUZgnv.jpg"></a>
      </div>
      <div class="data">
          <div class="name"><a itemprop="url" href="https://www.4kvm.org/cast/ian-mckellen">Ian McKellen</a></div>
          <div class="caracter">Gandalf</div>
      </div>
  </div>
</div>
`;

pdfa(html, ".persons .person")
  .map((item) => pdfh(item, "meta&&content"))
  .join(","),

5.代码串获取内容

常用于截取内容后是一段代码串, 但不想使用正则提取

js
const html = `var name = "zyplayer"; name = "zyfun";`;
eval(html);
console.log(name);

6.代码自执行

常用于加密代码执行补充环境且避免污染全局变量

js
// 加密代码
const code = function() {
  var _0x30e6a9 = "e11ed29b";
  function _0x52e1af(_0x174b0c, _0x4fda13) {
    var _0x550aaa = atob(_0x174b0c);
    for (var _0x488b1e, _0x4bd62c = [], _0x11662e = 0, _0x27b014 = "", _0x428133 = 0; 256 > _0x428133; _0x428133++) {
      _0x4bd62c[_0x428133] = _0x428133;
    }
    for (_0x428133 = 0; 256 > _0x428133; _0x428133++) {
      _0x11662e = (_0x11662e + _0x4bd62c[_0x428133] + _0x4fda13.charCodeAt(_0x428133 % _0x4fda13.length)) % 256;
      _0x488b1e = _0x4bd62c[_0x428133];
      _0x4bd62c[_0x428133] = _0x4bd62c[_0x11662e];
      _0x4bd62c[_0x11662e] = _0x488b1e;
    }
    for (b = _0x11662e = _0x428133 = 0; b < _0x550aaa.length; b++) {
      _0x428133 = (_0x428133 + 1) % 256;
      _0x11662e = (_0x11662e + _0x4bd62c[_0x428133]) % 256;
      _0x488b1e = _0x4bd62c[_0x428133];
      _0x4bd62c[_0x428133] = _0x4bd62c[_0x11662e];
      _0x4bd62c[_0x11662e] = _0x488b1e;
      _0x27b014 += String.fromCharCode(_0x550aaa.charCodeAt(b) ^ _0x4bd62c[(_0x4bd62c[_0x428133] + _0x4bd62c[_0x11662e]) % 256]);
    }
    return _0x27b014;
  }
  if (!JSON.decrypt || typeof JSON.decrypt !== "function") {
    Object.defineProperty(JSON, "decrypt", {
      "value": function (_0x58f264) {
        var _0x2da9b8 = _0x52e1af(_0x58f264, _0x30e6a9);
        return this.parse(_0x2da9b8);
      }
    });
  }
}
(code)(); // 执行后JSON对象会挂载decrypt方法
console.log(JSON);

Released under the MIT License.