// ==UserScript== // @name cda.pl, vimeo - lista wideo w folderze i wyszukiwarce // @namespace http://tampermonkey.net/ // @version 0.3 // @description Pokazuje listę linków do filmów z wszystkich stron danego folderu (ale nie w podfolderach) lub wyniku wyszukiwania (bez folderów). Linki nie prowadzą bezpośrednio do filmu, lecz do jego strony. // @author Dove6 // @include https://www.cda.pl/*/folder* // @include https://www.cda.pl/info/*?section=video // @include https://www.cda.pl/video/show/* // @include https://vimeo.com/search* // @grant none // @downloadURL none // ==/UserScript== /* jshint esversion: 6 */ (function() { 'use strict'; const dom_parser = new DOMParser(); class SiteLinksFetcher { setReachedEnd(direction) { this.reached_end[direction] = true; if (this.reached_end.left && this.reached_end.right) { this.callback(this.links); } } callback(links) { console.log(links.join(' ')); } constructor(folder_url, callback = null) { this.links = []; this.reached_end = {'left': false, 'right': false}; this.folder_url = folder_url; if (callback !== null) { this.callback = callback; } this.reached_end.callback = this.callback; } fetchLinks(folder_url = this.folder_url, direction = null) { throw new Error('This method is abstract'); } static createButton(name) { let s = document.createElement('span'); s.class = 'fetch-links-button'; s.innerHTML = name; s.style.border = '2px solid green'; s.style.borderRadius = '2px'; s.style.padding = '5px'; s.style.margin = '10px'; s.style.fontWeight = 'bold'; s.style.cursor = 'pointer'; return s; } } class CDALinksFetcher extends SiteLinksFetcher { fetchLinks(folder_url = this.folder_url, direction = null) { fetch(folder_url).then( (result) => { let fetched_page = result.text().then( (result_text) => { let fetched_DOM = dom_parser.parseFromString(result_text, 'text/html'); this.links = this.links.concat(this.getLinksFromDOM(fetched_DOM)); let buttons = { 'next': this.getNextPageFromDOM(fetched_DOM), 'previous': this.getPreviousPageFromDOM(fetched_DOM) }; if (direction === 'right' || direction === null) { if (buttons.next !== undefined && buttons.next !== null) { console.log('Next page: ' + buttons.next.href); this.fetchLinks(buttons.next.href, 'right'); } else { this.setReachedEnd('right'); } } if (direction === 'left' || direction === null) { if (buttons.previous !== undefined && buttons.previous !== null) { console.log('Previous page: ' + buttons.previous.href); this.fetchLinks(buttons.previous.href, 'left'); } else { this.setReachedEnd('left'); } } } ); } ); } getLinksFromDOM(dom) { throw new Error('This method is abstract'); } getNextPageFromDOM(dom) { throw new Error('This method is abstract'); } getPreviousPageFromDOM(dom) { throw new Error('This method is abstract'); } static createButton(name) { let s = super.createButton(name); s.style.backgroundColor = 'black'; return s; } } class CDASearchLinksFetcher extends CDALinksFetcher { getLinksFromDOM(dom) { return Array.from(dom.querySelectorAll('.text.text-video > .link-title-visit')).map(function(e) { return e.href; }); } getNextPageFromDOM(dom) { return dom.querySelectorAll('.btnRed.sbmNext')[0]; } getPreviousPageFromDOM(dom) { return dom.querySelectorAll('.btnRed.sbmPrev')[0]; } static createButton(url, callback = null, name = 'Pobierz linki') { let s = super.createButton(name); s.onclick = function() { if (document.getElementsByClassName('fetch-links-result').length === 0) { let links_getter = new CDASearchLinksFetcher(url, callback); links_getter.fetchLinks(); } }; return s; } } class CDAFolderLinksFetcher extends CDALinksFetcher { getLinksFromDOM(dom) { return Array.from(dom.getElementsByClassName('link-title-visit')).map(function(e) { return e.href; }); } getNextPageFromDOM(dom) { return dom.getElementsByClassName('next')[0]; } getPreviousPageFromDOM(dom) { return dom.getElementsByClassName('previous')[0]; } static createButton(url, callback = null, name = 'Pobierz linki') { let s = super.createButton(name); s.onclick = function() { if (document.getElementsByClassName('fetch-links-result').length === 0) { let links_getter = new CDAFolderLinksFetcher(url, callback); links_getter.fetchLinks(); } }; return s; } } class VimeoLinksFetcher extends SiteLinksFetcher { fetchLinks(folder_url = this.folder_url, direction = null) { fetch(folder_url).then( (result) => { let fetched_page = result.text().then( (result_text) => { let fetched_JSON = JSON.parse(result_text.match(/var data \= (.+?);/)[1]); this.links = this.links.concat(this.getLinksFromJSON(fetched_JSON)); let pagination = { 'next': this.getNextPageFromJSON(fetched_JSON), 'previous': this.getPreviousPageFromJSON(fetched_JSON) }; if (direction === 'right' || direction === null) { if (pagination.next !== undefined && pagination.next !== null) { console.log('Next page: ' + pagination.next); this.fetchLinks(pagination.next, 'right'); } else { this.setReachedEnd('right'); } } if (direction === 'left' || direction === null) { if (pagination.previous !== undefined && pagination.previous !== null) { console.log('Previous page: ' + pagination.previous); this.fetchLinks(pagination.previous, 'left'); } else { this.setReachedEnd('left'); } } } ); } ); } getLinksFromJSON(json) { throw new Error('This method is abstract'); } getNextPageFromJSON(json) { throw new Error('This method is abstract'); } getPreviousPageFromJSON(json) { throw new Error('This method is abstract'); } static createButton(url, callback = null, name = 'Pobierz linki') { let s = super.createButton(name); s.style.color = '#456'; s.style.backgroundColor = '#efe'; return s; } } class VimeoSearchLinksFetcher extends VimeoLinksFetcher { getLinksFromJSON(json) { return json.filtered.data.map(function(e) { return e.clip.link; }); } getNextPageFromJSON(json) { if (json.filtered.paging.next !== undefined && json.filtered.paging.next !== null) { let params_to_update = new URLSearchParams(json.filtered.paging.next); let destination_url = new URL(location.href); let destination_params = destination_url.searchParams; for (let pair of params_to_update) { destination_params.set(pair[0], pair[1]); } destination_url.search = destination_params.toString(); return destination_url.toString(); } else { return json.filtered.paging.next; } } getPreviousPageFromJSON(json) { if (json.filtered.paging.previous !== undefined && json.filtered.paging.previous !== null) { let params_to_update = new URLSearchParams(json.filtered.paging.previous); let destination_url = new URL(location.href); let destination_params = destination_url.searchParams; for (let pair of params_to_update) { destination_params.set(pair[0], pair[1]); } destination_url.search = destination_params.toString(); return destination_url.toString(); } else { return json.filtered.paging.previous; } } static createButton(url, callback = null, name = 'Pobierz linki') { let s = super.createButton(name); s.onclick = function() { if (document.getElementsByClassName('fetch-links-result').length === 0) { let links_getter = new VimeoSearchLinksFetcher(url, callback); links_getter.fetchLinks(); } }; return s; } } if ((/vimeo\.com\/search/).test(location.href)) { //Vimeo search if (document.getElementsByClassName('fetch-links-button').length === 0 && document.getElementsByClassName('iris_link-box').length > 0) { let parent_div = document.querySelector('.header_region'); if (parent_div !== null) { let result_div = document.createElement('div'); result_div.style.border = '2px solid gray'; result_div.style.borderRadius = '5px'; result_div.style.padding = '5px'; result_div.style.margin = '10px'; result_div.fontFamily = 'monospace'; result_div.style.display = 'none'; parent_div.insertBefore(result_div, parent_div.children[1]); parent_div.insertBefore(VimeoSearchLinksFetcher.createButton(location.href, function(links) { result_div.innerHTML = links.join(' '); result_div.style.display = 'block'; }), result_div); } } } else if ((/cda\.pl\/.+\/folder/).test(location.href)) { //cda folder if (document.getElementsByClassName('fetch-links-button').length === 0 && document.getElementsByClassName('link-title-visit').length > 0) { let parent_div = document.querySelector('#folder-replace .panel-body'); if (parent_div !== null) { let result_div = document.createElement('div'); result_div.style.border = '2px solid gray'; result_div.style.borderRadius = '5px'; result_div.style.padding = '5px'; result_div.style.margin = '10px'; result_div.style.backgroundColor = 'black'; result_div.fontFamily = 'monospace'; result_div.style.display = 'none'; parent_div.insertBefore(result_div, parent_div.children[1]); parent_div.insertBefore(CDAFolderLinksFetcher.createButton(location.href, function(links) { result_div.innerHTML = links.join(' '); result_div.style.display = 'block'; }), result_div); } } } else if ((/cda\.pl\/info\/.+\?section\=video/).test(location.href)) { //cda general search if (document.getElementsByClassName('fetch-links-button').length === 0 && document.querySelectorAll('.text-video > .link-title-visit').length) { let sought_phrase = location.href.match(/cda\.pl\/info\/(.+)\?section\=video/)[1]; if (sought_phrase === null || sought_phrase === undefined) { throw new Error('Error getting the sought phrase from link'); } let video_search_link = 'https://www.cda.pl/video/show/' + sought_phrase; let parent_div = document.querySelector('.nogry.nogiwera.nodownload.video .bttn-grp-const-width-md'); if (parent_div !== null) { let intermediate_div = document.createElement('div'); intermediate_div.style.position = 'absolute'; intermediate_div.style.margin = '5px 0'; parent_div.insertBefore(intermediate_div, parent_div.children[0]); let result_div = document.createElement('div'); result_div.class = 'fetch-links-result'; result_div.style.border = '2px solid gray'; result_div.style.borderRadius = '5px'; result_div.style.padding = '5px'; result_div.style.margin = '10px'; result_div.style.backgroundColor = 'black'; result_div.fontFamily = 'monospace'; result_div.style.display = 'none'; result_div.style.textAlign = 'left'; parent_div.appendChild(result_div); intermediate_div.appendChild(CDASearchLinksFetcher.createButton(video_search_link, function(links) { result_div.innerHTML = links.join(' '); result_div.style.display = 'block'; }), result_div); } } } else if ((/cda\.pl\/video\/show/).test(location.href)) { //cda video search if (document.getElementsByClassName('fetch-links-button').length === 0 && document.querySelectorAll('.text-video > .link-title-visit').length) { let parent_div = document.querySelector('.bttn-grp-const-width-md'); if (parent_div !== null) { let intermediate_div = document.createElement('div'); intermediate_div.style.position = 'absolute'; intermediate_div.style.margin = '5px 0'; parent_div.insertBefore(intermediate_div, parent_div.children[0]); let result_div = document.createElement('div'); result_div.class = 'fetch-links-result'; result_div.style.border = '2px solid gray'; result_div.style.borderRadius = '5px'; result_div.style.padding = '5px'; result_div.style.margin = '10px'; result_div.style.backgroundColor = 'black'; result_div.fontFamily = 'monospace'; result_div.style.display = 'none'; result_div.style.textAlign = 'left'; parent_div.appendChild(result_div); intermediate_div.appendChild(CDASearchLinksFetcher.createButton(location.href, function(links) { result_div.innerHTML = links.join(' '); result_div.style.display = 'block'; }), result_div); } } } })();