CF Gift Code

Auto enter Crossfire Gift Code.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name            CF Gift Code
// @name:vi         Gift Code CF
// @namespace       https://lelinhtinh.github.io
// @description     Auto enter Crossfire Gift Code.
// @description:vi  Tự động nhập Gift Code Đột Kích.
// @version         1.3.0
// @icon            https://raw.githubusercontent.com/lelinhtinh/Userscript/master/cf_giftcode/icon.png
// @author          lelinhtinh
// @oujs:author     baivong
// @license         MIT; https://baivong.mit-license.org/license.txt
// @match           https://dotkich.goplay.vn/shop/giftcode
// @noframes
// @supportURL      https://github.com/lelinhtinh/Userscript/issues
// @run-at          document-idle
// @grant           none
// ==/UserScript==

function autoGiftcode() {
  if (!gcClipboard.length) return;
  const gc = gcClipboard.pop();
  console.log('Giftcode', gc);

  $gcInput.val(gc);
  $gcInput.trigger('focus');

  if (/\(.+?\)/.test(gc)) {
    const beginRange = gc.search(/\s?\(/);
    let endRange = gc.search(/\)\s/);
    endRange = endRange === -1 ? gc.search(/\)/) + 1 : endRange + 2;
    $gcInput.get(0).setSelectionRange(beginRange, endRange);
    return;
  }

  sendGiftcode(gc);
}

function sendGiftcode(gc) {
  $.ajax({
    url: '/api/ajaxapi/GiftCode/CheckCode',
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    processData: true,
    cache: false,
    data: JSON.stringify({
      GiftCode: gc,
      ServiceCode: 'CF',
      UserId: userId,
      UserName: userName,
    }),
  })
    .done((data) => {
      $helpText.append(`${gc}: ${data.message}\n`);
    })
    .always(autoGiftcode);
}

function onSubmit(e) {
  e.preventDefault();
  e.stopPropagation();
  sendGiftcode($gcInput.val());
}

function validateClipboard(clipText) {
  if (!clipText) return;
  const gcPattern = /\bCFS?[A-Z0-9]{2,}(\s?\(.+?\)\s?)?([A-Z0-9]+)?(\b|\B)/;

  gcClipboard = clipText
    .split('\n')
    .map((gc) => gc.trim())
    .filter((gc) => gc && gcPattern.test(gc))
    .map((gc) => {
      gc = gc.match(gcPattern)[0];
      const mathPattern = /\s?\(([0-9+\-*/x:]{2,}[0-9]+)=\?\)\s?/;
      if (mathPattern.test(gc)) {
        return gc.replace(mathPattern, (m) => {
          let expression = m.match(mathPattern)[1];
          expression = expression.replaceAll('x', '*').replaceAll(':', '/');
          return eval(expression);
        });
      }
      return gc;
    });

  if (!gcClipboard.length) {
    $helpText.removeClass('text-muted').addClass('text-danger').text('Clipboard không có Gift Code');
    return;
  }

  autoGiftcode();
}

let gcClipboard = [];

const $gcAutoBtn = $('<button />', {
  type: 'button',
  id: 'btn_giftcode_auto',
  class: 'btn btn-lg btn-primary btn-block mb-4',
  text: 'Tự động nhập từ bộ nhớ đệm',
});

const $helpText = $('<pre />', {
  id: 'alert_giftcode_auto',
  class: 'form-text text-muted small',
});

const $gcInput = $('#input_giftcode');
const $gcForm = $gcInput.closest('form');
const $gcSubmit = $gcForm.find('a');

const userInfo = $('[class="text-[#47bac1]"]').text().split(/:|-/);
const userName = userInfo[4].trim();
const userId = userInfo[6].trim();
console.log(userName, userId);

$gcForm.append($helpText);
$gcAutoBtn.insertBefore($gcInput);

$gcAutoBtn.on('click', (e) => {
  e.preventDefault();

  $helpText.removeClass('text-danger').addClass('text-muted').empty();
  navigator.clipboard.readText().then((clipText) => validateClipboard(clipText));
});

$gcInput.on('keydown', (e) => {
  if (e.which !== 13) return;
  onSubmit(e);
});
$gcSubmit.on('click', onSubmit);
$gcForm.on('submit', onSubmit);

$('head').append(`<style>
#myModal,
#myModal + .modal-backdrop {
  display: none !important;
}
</style>`);