Greasy Fork

ddrk优化

1.跳转新标签 2.收藏功能

目前为 2021-10-05 提交的版本。查看 最新版本

// ==UserScript==
// @name         ddrk优化
// @namespace    king
// @version      0.1
// @description  1.跳转新标签 2.收藏功能
// @author       hero-king
// @match        https://ddrk.me/*
// @icon         https://ddrk.me/favicon-32x32.png
// @grant        none
// @require      https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js
// ==/UserScript==

(function () {
  'use strict';
  /*去广告*/
  $('.cfa_popup').css({ "height": "0px" })
  $('#iaujwnefhw').css({ "height": "0", overflow: 'hidden' })
  $('#kasjbgih').css({ "height": "0", overflow: 'hidden' })

  const xxx = `<style>
  .col_list::-webkit-scrollbar {
    width: 5px;
    height: 5px
  }

  .col_list::-webkit-scrollbar-thumb {
    border-radius: 3px;
    -moz-border-radius: 3px;
    -webkit-border-radius: 3px;
    background-color: #999;
  }

  .col_list::-webkit-scrollbar-track {
    background-color: transparent
  }<style>`

  $('head').append(xxx)

  const jsonText = window.localStorage.getItem('ddrk-collection')
  let list = []
  if (jsonText) {
    list = JSON.parse(jsonText)
  }

  const div = $("<div></div>");
  div.css({ "position": "absolute", "left": "0", "top": "0", "right": "0", "bottom": "0", 'text-align': 'right' });
  div.addClass("laksjdflakjsd");
  $(".post-box").on('click', '.btn_default', function (e) {
    e.stopPropagation();
  });
  $(".post-box").on('click', '.btn_add', function (e) {
    const href = $(this).parent().parent().data('href')
    const name = $(this).parent().parent().find('.post-box-title a').text()
    if (!list.find(item => item.href === href)) {
      list.push({ name, href })
      window.localStorage.setItem('ddrk-collection', JSON.stringify(list))
    }
    reloadCollect(1, $(this));
    reloadList()
  });
  $(".post-box").on('click', '.btn_remove', function (e) {
    const href = $(this).parent().parent().data('href')
    const index = list.findIndex(item => item.href === href);
    if (index !== -1) {
      list.splice(index, 1)
      window.localStorage.setItem('ddrk-collection', JSON.stringify(list))
    }
    reloadCollect(0, $(this));
    reloadList()
  });
  $(".post-box").on('click', '.laksjdflakjsd', function (e) {
    window.open($(this).parent().data('href'))
    e.stopPropagation();
  });
  $("body").on('click', '.col_list li', function (e) {
    window.open($(this).data('href'))
    e.stopPropagation();
  });
  $("body").on('click', '.col_list li .icon_del', function (e) {
    const href = $(this).parent().data('href')
    const index = list.findIndex(item => item.href === href);
    if (index !== -1) {
      list.splice(index, 1)
      window.localStorage.setItem('ddrk-collection', JSON.stringify(list))
    }
    reloadCollect();
    reloadList()
    e.stopPropagation();
  });

  const btn = $('<span class="btn_default btn_remove" style="font-size:22px;color: #2EBF8B;padding:6px;background-color: rgba(0,0,0,0.4);position: absolute;top:0;box-shadow: 0px 0px 5px rgba(0,0,0,0.4);line-height: 1.2;user-select:none;">★</span>');
  $('.post-box').each(function () {
    const tempBtn = btn.clone(true)
    if (!list.find(item => item.href === $(this).data('href'))) {
      tempBtn.addClass("btn_add");
      tempBtn.removeClass("btn_remove");
      tempBtn.text('☆')
    }
    div.html(tempBtn)
    $(this).append(div.clone(true));
  });

  const colOuter = $("<ul class='col_list'></ul>");
  colOuter.css({
    "position": "fixed",
    "top": "35px",
    "right": "0",
    width: "30px",
    height: "26px",
    padding: '5px',
    'box-sizing': ' border-box',
    background: "#000",
    "box-shadow": "-1px 1px 5px rgba(0, 0, 0, 0.2)",
    "z-index": "999",
    overflow: "hidden",
    transition: 'all .5s'
  });
  reloadList()
  $('body').append(colOuter);

  colOuter.mouseenter(function () {
    colOuter.css({ width: "auto", height: "300px", overflow: 'auto' });
  }).mouseleave(function () {
    colOuter.css({
      width: "30px",
      height: "26px",
      overflow: "hidden"
    });
  });

  function reloadList() {
    colOuter.html('')
    for (const item of list) {
      const li = $("<li class='col_item' style='cursor: pointer;display: flex;justify-content: space-between;' ></li>");
      const span = $("<span>" + item.name + "</span>");
      const del = $("<span class='icon_del'>x</span>")
      del.css({
        'border-radius': '100%',
        width: '16px',
        height: '16px',
        'line-height': '1',
        display: 'inline-block',
        'text-align': 'center',
      });
      li.append(span)
      li.append(del)
      li.data("href", item.href);
      colOuter.append(li);
    }
  }

  function reloadCollect(tag, tempBtn) {
    if (tempBtn) {
      if (tag === 0) {
        tempBtn.addClass("btn_add");
        tempBtn.removeClass("btn_remove");
        tempBtn.text('☆')
      } else {
        tempBtn.addClass("btn_remove");
        tempBtn.removeClass("btn_add");
        tempBtn.text('★')
      }
    } else {
      $('.post-box').each(function () {
        const tempBtn = $(this).find('.btn_default')
        if (!list.find(item => item.href === $(this).data('href'))) {
          tempBtn.addClass("btn_add");
          tempBtn.removeClass("btn_remove");
          tempBtn.text('☆')
        } else {
          tempBtn.addClass("btn_remove");
          tempBtn.removeClass("btn_add");
          tempBtn.text('★')
        }
      });
    }
  }

})();