Get chapter links

Dodaje przycisk do skopiowania linkow do rozdzalow

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        Get chapter links
// @namespace   dtm
// @include     https://novelki.pl/projekty/*
// @version     1
// @grant       none
// @description Dodaje przycisk do skopiowania linkow do rozdzalow
// @require     https://code.jquery.com/ui/1.12.1/jquery-ui.js
// ==/UserScript==

function getHref(item){
  return $(item).prop('href');
}

function copyToClipboard(){
      var dummy = document.createElement("textarea");
      document.body.appendChild(dummy);
      dummy.setAttribute("id", "dummy_id");
      document.getElementById("dummy_id").value=$.map(slicedLinks, getHref).join('\r\n');
      dummy.select();
      document.execCommand("copy");
      document.body.removeChild(dummy);
    }


var links = [];
var maxLinksPerPage = 50;
var slicedLinks = [];
var button;
var input;

var tmp = [];

function onlyUnique(value, index, self) {
  if (tmp.indexOf(value.prop('href')) < 0){
    tmp.push(value.prop('href'));
    return true;
  }
  else{
    return false;
  }
}

function findLinks() {
  $tempsA = []
  tmp = [];
  $('table.chapters tr td:first-child a').each(function () {
    $tempsA.push($(this));
  });
  links = $tempsA.filter(onlyUnique).reverse();
}

function changeTitle(title){
  button.prop('title', title);
}

function getText(item) {
  return $(item).text();
}

function linksPageNumberChanged(){
  var pageNumber = input.val()
  if(isNaN(pageNumber) || pageNumber <= 0){
    slicedLinks = links;
    changeTitle('Nieprawidłowy input\r\n Wszystkie linki \n Ilość:' + slicedLinks.length)
  } else {
    var startPosition = (pageNumber-1)*maxLinksPerPage;
    if(startPosition >= links.length){
      slicedLinks = links;
      changeTitle('Strona za duża, brak linków' 
                  + ' \r\nLiczba stron: ' + links.length/maxLinksPerPage 
                  + ' \r\nLinków na stronie: ' + maxLinksPerPage 
                  + " \r\nWszystkich linków: " + links.length)
    } else {
      slicedLinks = links.slice((pageNumber-1)*maxLinksPerPage, pageNumber*maxLinksPerPage);
      changeTitle($.map(slicedLinks, getText).join('<|>'));
    }
  }
}

function getChaptersLinks(){
  var pageNumber = $('#links-page-number').val()
  if(isNaN(pageNumber) || pageNumber <= 0){
    copyToClipboard();
  } else {
    var startPosition = (pageNumber-1)*maxLinksPerPage;
    if(startPosition >= links.length){
      alert('Nie ma takiej strony');
    } else {
      copyToClipboard(); 
    }
  }
}

function addInputsToPage(){
  var $tc = $('table.chapters').get(1);
  $('<input id="links-page-number" />').insertBefore($tc);
  $('<button id="getLinks" type="button" >Pobierz linki</button>').insertBefore($tc);
  button = $('button#getLinks');
  input = $('input#links-page-number');
  button.click(getChaptersLinks);
  input.keyup(linksPageNumberChanged);
}

$(document).ready(function () {
  console.log('test');
  findLinks();
  addInputsToPage();
  linksPageNumberChanged();
  $(document).tooltip({
    open: function (event, ui) {
        ui.tooltip.css("min-width", "800px");
    }
   });
});