Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function() {
- 'use strict';
- var linkList = []
- var currentUrl = window.location.href
- function startGettingAllTheLinks(){
- Promise.resolve()
- .then(function(){
- return(1)
- })
- .then(
- function getLinks(pageIndex){
- var url = currentUrl+'?page='+pageIndex
- var reqPromise = makeRequest('GET', url, pageIndex).then(function(valueHolderArray){
- var response = valueHolderArray[0];
- var pageIndex = valueHolderArray[1];
- extractLinks(response)
- console.log('parsed page '+pageIndex)
- });
- var promise = Promise.resolve(reqPromise)
- .catch(function (err) {
- return(err)
- })
- .then(
- function(err){
- if(err){
- if(err.status === 404){
- console.log('404 for page '+err.pageIndex+'. Looks like '+err.pageIndex-1+' is the last page.')
- }else{
- console.error('Augh, there was an error!', err.status, err.statusText, err.pageIndex);
- }
- }else{
- return(getLinks(pageIndex + 1)) //recurse
- }
- }
- )
- return( promise);
- }
- )
- .then(function(){
- console.log('found '+linkList.length+' links')
- for(var i=0; i<linkList.length; i++){
- console.log(linkList[i])
- }
- })
- }
- startGettingAllTheLinks();
- function extractLinks(response){
- var links = response.querySelectorAll('div.sectionWrapper ul.pornstarsVideos li .title a')
- for(var i=0; i<links.length; i++){
- linkList.push(links[i].href)
- }
- }
- function makeRequest (method, url, pageIndex) {
- return new Promise(function (resolve, reject) {
- var xhr = new XMLHttpRequest();
- xhr.open(method, url);
- xhr.responseType = 'document';
- xhr.onload = function () {
- if (this.status >= 200 && this.status < 300) {
- resolve([xhr.response, pageIndex]);
- } else {
- reject({
- status: this.status,
- statusText: xhr.statusText,
- pageIndex : pageIndex
- });
- }
- };
- xhr.onerror = function () {
- reject({
- status: this.status,
- statusText: xhr.statusText,
- pageIndex : pageIndex
- });
- };
- xhr.send();
- });
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement