Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. (function() {
  2. 'use strict';
  3.  
  4. var linkList = []
  5. var currentUrl = window.location.href
  6.  
  7. function startGettingAllTheLinks(){
  8.  
  9. Promise.resolve()
  10. .then(function(){
  11. return(1)
  12. })
  13. .then(
  14. function getLinks(pageIndex){
  15. var url = currentUrl+'?page='+pageIndex
  16. var reqPromise = makeRequest('GET', url, pageIndex).then(function(valueHolderArray){
  17. var response = valueHolderArray[0];
  18. var pageIndex = valueHolderArray[1];
  19.  
  20. extractLinks(response)
  21. console.log('parsed page '+pageIndex)
  22. });
  23. var promise = Promise.resolve(reqPromise)
  24. .catch(function (err) {
  25. return(err)
  26. })
  27. .then(
  28. function(err){
  29. if(err){
  30. if(err.status === 404){
  31. console.log('404 for page '+err.pageIndex+'. Looks like '+err.pageIndex-1+' is the last page.')
  32. }else{
  33. console.error('Augh, there was an error!', err.status, err.statusText, err.pageIndex);
  34. }
  35. }else{
  36. return(getLinks(pageIndex + 1)) //recurse
  37. }
  38. }
  39. )
  40.  
  41. return( promise);
  42. }
  43. )
  44. .then(function(){
  45. console.log('found '+linkList.length+' links')
  46. for(var i=0; i<linkList.length; i++){
  47. console.log(linkList[i])
  48. }
  49. })
  50. }
  51.  
  52. startGettingAllTheLinks();
  53.  
  54. function extractLinks(response){
  55. var links = response.querySelectorAll('div.sectionWrapper ul.pornstarsVideos li .title a')
  56. for(var i=0; i<links.length; i++){
  57. linkList.push(links[i].href)
  58. }
  59. }
  60.  
  61. function makeRequest (method, url, pageIndex) {
  62. return new Promise(function (resolve, reject) {
  63. var xhr = new XMLHttpRequest();
  64. xhr.open(method, url);
  65. xhr.responseType = 'document';
  66. xhr.onload = function () {
  67. if (this.status >= 200 && this.status < 300) {
  68. resolve([xhr.response, pageIndex]);
  69. } else {
  70. reject({
  71. status: this.status,
  72. statusText: xhr.statusText,
  73. pageIndex : pageIndex
  74. });
  75. }
  76. };
  77. xhr.onerror = function () {
  78. reject({
  79. status: this.status,
  80. statusText: xhr.statusText,
  81. pageIndex : pageIndex
  82. });
  83. };
  84. xhr.send();
  85. });
  86. }
  87.  
  88. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement