沉冰浮水

沉冰浮水

做最终到的事,成为最终成为的人!
github
bilibili
mastodon
zhihu
douban

"GM Script" VIP B Coin Collection Reminder

Most scripts prioritize personal use, so they may contain strange features. It is recommended to extract the necessary parts and organize the complete code yourself.

name: "bilibili" - Export "Watch Later" to .url

desc: Export Bilibili's "Watch Later" list as a .url file

url: https://github.com/wdssmq/userscript/blob/master/bilibili/later.user.js

cdn: https://cdn.jsdelivr.net/gh/wdssmq/userscript@master/bilibili/later.user.js

Also, I just found out that localStorage cannot be used across subdomains, so I used cookies. It seems like using GM_setValue+GM_getValue might work as well.

Related Recommendations

"Tinkering" - Modify Bilibili Anime Links to My Watchlist - Computer Network - Chén Bīng Fú Shuǐ

"Tinkering" - Writing GM Scripts with rollup.js for Modularity - Computer Network - Chén Bīng Fú Shuǐ


The script has been rewritten using rollup.js. Please refer to the above recommendations for details.

Git view of the "B Coin Collection Reminder" section: https://github.com/wdssmq/userscript/blob/master/bilibili/src/_bcoin.js

The following is for reference only and cannot be used directly:

// @grant        GM_notification
// @grant        GM.openInTab

// ==/UserScript==
/* jshint esversion:6 */
import { curUrl, curDate, _log, $n, fnElChange } from './_base.js';

// Cookie encapsulation
const ckeObj = {
  setItem: function (key, value) {
    const Days = 137;
    const exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = key + "=" + encodeURIComponent(value) + ";path=/;domain=.bilibili.com;expires=" + exp.toGMTString();
  },
  getItem: function (key, def = "") {
    const reg = new RegExp("(^| )" + key + "=([^;]*)(;|$)");
    const arr = document.cookie.match(reg);
    if (arr) {
      return decodeURIComponent(arr[2]);
    }
    return def;
  }
};

// Convert date to string
const getDateStr = (date) => {
  const options = { year: 'numeric', month: '2-digit', day: '2-digit' };
  return date.toLocaleDateString("en-US", options);
}

// Calculate date difference
const diffDateDays = (date1, date2) => {
  const diff = date1.getTime() - date2.getTime();
  return diff / (1000 * 60 * 60 * 24);
}

// B Coin Collection Reminder
(() => {
  const ckeName = "bilibili-helper-bcoin-nxtDateStr";
  const curDateStr = getDateStr(curDate);
  const nxtDateStr = ckeObj.getItem(ckeName, curDateStr);
  const bcoinUrl = "https://account.bilibili.com/account/big/myPackage";

  _log(`Current Date: ${curDateStr}`);
  _log(`Next Collection: ${nxtDateStr}`);

  // Notification event encapsulation
  const notify = (title, body) => {
    _log(`Notification Title: ${title}`);
    GM_notification({
      title: title,
      text: body,
      timeout: 0,
      onclick: () => {
        // window.location.href = bcoinUrl;
        GM.openInTab(bcoinUrl, false);
      }
    });
  }

  // Check if already collected
  const fnCheckByDOM = () => {
    const $bcoin = $n(".bcoin-wrapper");
    // _log("fnCheckByDOM", $bcoin);
    // $bcoin && _log("fnCheckByDOM", $bcoin.innerHTML);
    if ($bcoin && $bcoin.innerText.includes("This time has been collected")) {
      const match = $bcoin.innerText.match(/\d{4}\/\d+\/\d+/);
      if (match && match[0] !== nxtDateStr) {
        ckeObj.setItem(ckeName, match[0]);
        _log("Already collected, update next collection date", match[0]);
        return true;
      }
    } else {
      fnElChange($n("#app"), fnCheckByDOM);
    }
    return false;
  }

  // _log($n("body").innerHTML);
  // _log(nxtDateStr, curMonth);

  // Compare dates
  const iniDiff = diffDateDays(curDate, new Date(nxtDateStr));
  if (iniDiff > 0) {
    _log(curUrl, "\n", bcoinUrl);
    if (curUrl.indexOf(bcoinUrl) > -1) {
      fnCheckByDOM();
    } else {
      notify("B Coin Collection Reminder", "Click to view B Coin collection status");
    }
  }
})();
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.