Livelib ExternalSearch

Integration with external sources - searching the book on external web-sites

Від 09.01.2015. Дивіться остання версія.

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(У мене вже є менеджер скриптів, дайте мені встановити його!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name        Livelib ExternalSearch
// @version     0.5
// @description Integration with external sources - searching the book on external web-sites
// @namespace   http://greasyfork.org/ru/users/7350-plesk
// @match       http://www.livelib.ru/book/*
// @copyright   2014, plesk
// ==/UserScript==

//Массив со сторонними сайтами
var sources = new Array(); 
//0 - Имя источника
//1 - Ссылка на иконку
//2 - Поисковый запрос
//3 - Тип поискового запроса: 0 - название книги, 1 - автор и название книги

i = sources.length;
sources[i] = new Array();
sources[i][0] = "flibusta.net";                           
sources[i][1] = "http://flibusta.net/favicon.ico";        
sources[i][2] = "http://flibusta.net/booksearch?ask={0}"; 
sources[i][3] = 0; 

i = sources.length;
sources[i] = new Array();
sources[i][0] = "rutracker.org";                          
sources[i][1] = "http://rutracker.org/favicon.ico";       
sources[i][2] = "http://rutracker.org/forum/tracker.php?nm={0}"; 
sources[i][3] = 1;

//Метод format для подстановки параметров в строку
String.prototype.format = String.prototype.f = function () {
    var args = arguments;
    return this.replace(/\{\{|\}\}|\{(\d+)\}/g, function (m, n) {
        if (m == "{{") { return "{"; }
        if (m == "}}") { return "}"; }
        return args[n];
    });
};

//Единый код для поисковых кнопок
buttonHTML = '<a class="action" target="_blank" href="{0}" title="{1}"><span style="margin-right: 4px"><img src="{2}" height=17 width=17 align="top"></span>{3}</a>';

//Автор и название искомой книги
//Внезнапно структура сайта немного поменялась
author = document.getElementById('leftside').childNodes[7].childNodes[1].childNodes[3].childNodes[0].innerText;
title  = document.getElementById('leftside').childNodes[7].childNodes[1].childNodes[1].innerText

// Кнопку прилепим ниже кнопки с предложением загрузить книгу на свою полку
tg0 = document.getElementsByClassName('sources actionbar bar-vertical')
// Такой блок вроде последний
tg1 = tg0[tg0.length-1];
if (tg1.innerHTML.indexOf("externalsearch") == -1){
   // Добавим отбивочку
   tg1.innerHTML = tg1.innerHTML + '<br><hr>';
   tg1.innerHTML = tg1.innerHTML + '<div class="externalsearch">'
   tg1.innerHTML = tg1.innerHTML + '<a class="action" target="_blank">Искать электронную книгу</a>'
   sources.forEach(function(s) {
       // Кнопка поиска
       searchText = "";
       if(s[3] == 0){
         searchText = title;
       }else if(s[3] == 1){
         searchText = author + " " + title;
       }else{
         searchText = "";
       }    
               
       tg1.innerHTML = tg1.innerHTML + buttonHTML.format(s[2].format(searchText.replace(/\s/g, "+")), s[0], s[1], s[0]);
   }); 
   tg1.innerHTML = tg1.innerHTML + '</div>'
}