Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. // ==UserScript==
  2. // @name Auto Facebook Marketplace Messanger
  3. // @version 1
  4. // @grant GM.getValue
  5. // @grant GM.setValue
  6. // @run-at document-idle
  7. // @include https://www.facebook.com/marketplace*/search/*
  8. // ==/UserScript==
  9.  
  10.  
  11.  
  12. function sleep (delay) {
  13. return new Promise(resolve => setTimeout(resolve, delay));
  14. }
  15.  
  16. function setNativeValue(element, value) {
  17. let lastValue = element.value;
  18. element.value = value;
  19. let event = new Event("input", { target: element, bubbles: true });
  20. // React 15
  21. event.simulated = true;
  22. // React 16
  23. let tracker = element._valueTracker;
  24. if (tracker) {
  25. tracker.setValue(lastValue);
  26. }
  27. element.dispatchEvent(event);
  28. }
  29.  
  30. var listingsViewed = [];
  31.  
  32.  
  33. var interval;
  34.  
  35.  
  36. var main_script = function() {
  37. var elements = document.querySelectorAll("*[data-testid='marketplace_feed_item']");
  38. console.log(elements);
  39. console.log("Going through " + elements.length + " listings....");
  40.  
  41.  
  42. if (elements.length > 0) {
  43. clearInterval(interval);
  44.  
  45. (async () => {
  46. for(var i=0;i<elements.length; ++i) {
  47. if (elements[i].parentElement.previousSibling != null && elements[i].parentElement.previousSibling.innerHTML == "You Might Also Like") {
  48. console.log("we reached the the extra listing stuff... quitting.");
  49. break;
  50. }
  51.  
  52. if (elements[i].parentElement.parentElement.previousSibling != null && elements[i].parentElement.parentElement.previousSibling.innerText == "Results From Marketplace Stores\nSee All") {
  53. console.log("we reached facebook marketplace store ads... quitting.");
  54. break;
  55. }
  56.  
  57.  
  58. let page_visited = await GM.getValue("visited_"+elements[i].href, false);
  59. if (page_visited) {
  60. console.log("We have visited " + elements[i].title + " - " + elements[i].href + " before - skipping.");
  61. } else {
  62. console.log("We have never visited " + elements[i].title + " - " + elements[i].href + " before, executing script.");
  63. GM.setValue("visited_"+elements[i].href, true);
  64.  
  65. elements[i].click();
  66. await sleep(1000);
  67.  
  68. var fullComponent = document.querySelector("[data-testid='marketplace_pdp_component']");
  69.  
  70. let expandButton = document.querySelector("*[title='More']");
  71.  
  72. if (expandButton !== null) {
  73. expandButton.click();
  74. }
  75.  
  76. let details = "";
  77. if (fullComponent.querySelector("p span") === null) {
  78. details = "no detailed description";
  79. } else {
  80. details = fullComponent.querySelector("p span").innerText;
  81. }
  82.  
  83.  
  84. console.log("Details of this listing: " + details);
  85.  
  86.  
  87. listingsViewed.push({ "title": elements[i].title, "details": details, url: elements[i].href })
  88.  
  89. console.log("selecting text box");
  90. var inputBox = document.querySelector("*[placeholder='Send a private message...']");
  91.  
  92. if (inputBox !== null) {
  93. console.log("filling in text box");
  94. setNativeValue(inputBox, "how old is this?");
  95. console.log("Firing reactJS update to run");
  96. inputBox.dispatchEvent(new Event('input', { bubbles: true }));
  97.  
  98. await sleep(1000);
  99. console.log("hitting submit button");
  100. var submitButton = fullComponent.querySelectorAll("button")[1];
  101. //submitButton.click();
  102. await sleep(1000);
  103.  
  104. } else {
  105. console.log("no text box - did you already manually message this posting already?");
  106. }
  107.  
  108.  
  109. //var close_button = document.querySelector("*[title='Close']");
  110. //close_button.click();
  111. await sleep(500);
  112. }
  113. }
  114.  
  115. console.log(listingsViewed);
  116. console.log(JSON.stringify(listingsViewed));
  117. })();
  118. }
  119. }
  120.  
  121. console.log("greasemonkey script firing..");
  122.  
  123. if (window.confirm("Do you really want to run the auto-responder?")) {
  124. interval = setInterval(main_script, 5000);
  125. }
  126.  
  127.  
  128. //
  129. /*
  130. function resolved(result) {
  131. console.log('Resolved');
  132. }
  133.  
  134. function rejected(result) {
  135. console.error(result);
  136. }*/
  137.  
  138.  
  139. //GM.setValue("visited_"+window.location.href, true);
  140. /*(async () => {
  141. let page_visited = await GM.getValue("visited_"+window.location.href, false);
  142. if (page_visited) {
  143. alert("We have visited " + window.location.href + " before.");
  144. } else {
  145. alert("We have never visited " + window.location.href + " before.");
  146. GM.setValue("visited_"+window.location.href, true);
  147. }
  148.  
  149. window.addEventListener('popstate', (event) => {
  150. console.log("location: " + document.location + ", state: " + JSON.stringify(event.state));
  151. });
  152. })();*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement