Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Auto Facebook Marketplace Messanger
- // @version 1
- // @grant GM.getValue
- // @grant GM.setValue
- // @run-at document-idle
- // @include https://www.facebook.com/marketplace*/search/*
- // ==/UserScript==
- function sleep (delay) {
- return new Promise(resolve => setTimeout(resolve, delay));
- }
- function setNativeValue(element, value) {
- let lastValue = element.value;
- element.value = value;
- let event = new Event("input", { target: element, bubbles: true });
- // React 15
- event.simulated = true;
- // React 16
- let tracker = element._valueTracker;
- if (tracker) {
- tracker.setValue(lastValue);
- }
- element.dispatchEvent(event);
- }
- var listingsViewed = [];
- var interval;
- var main_script = function() {
- var elements = document.querySelectorAll("*[data-testid='marketplace_feed_item']");
- console.log(elements);
- console.log("Going through " + elements.length + " listings....");
- if (elements.length > 0) {
- clearInterval(interval);
- (async () => {
- for(var i=0;i<elements.length; ++i) {
- if (elements[i].parentElement.previousSibling != null && elements[i].parentElement.previousSibling.innerHTML == "You Might Also Like") {
- console.log("we reached the the extra listing stuff... quitting.");
- break;
- }
- if (elements[i].parentElement.parentElement.previousSibling != null && elements[i].parentElement.parentElement.previousSibling.innerText == "Results From Marketplace Stores\nSee All") {
- console.log("we reached facebook marketplace store ads... quitting.");
- break;
- }
- let page_visited = await GM.getValue("visited_"+elements[i].href, false);
- if (page_visited) {
- console.log("We have visited " + elements[i].title + " - " + elements[i].href + " before - skipping.");
- } else {
- console.log("We have never visited " + elements[i].title + " - " + elements[i].href + " before, executing script.");
- GM.setValue("visited_"+elements[i].href, true);
- elements[i].click();
- await sleep(1000);
- var fullComponent = document.querySelector("[data-testid='marketplace_pdp_component']");
- let expandButton = document.querySelector("*[title='More']");
- if (expandButton !== null) {
- expandButton.click();
- }
- let details = "";
- if (fullComponent.querySelector("p span") === null) {
- details = "no detailed description";
- } else {
- details = fullComponent.querySelector("p span").innerText;
- }
- console.log("Details of this listing: " + details);
- listingsViewed.push({ "title": elements[i].title, "details": details, url: elements[i].href })
- console.log("selecting text box");
- var inputBox = document.querySelector("*[placeholder='Send a private message...']");
- if (inputBox !== null) {
- console.log("filling in text box");
- setNativeValue(inputBox, "how old is this?");
- console.log("Firing reactJS update to run");
- inputBox.dispatchEvent(new Event('input', { bubbles: true }));
- await sleep(1000);
- console.log("hitting submit button");
- var submitButton = fullComponent.querySelectorAll("button")[1];
- //submitButton.click();
- await sleep(1000);
- } else {
- console.log("no text box - did you already manually message this posting already?");
- }
- //var close_button = document.querySelector("*[title='Close']");
- //close_button.click();
- await sleep(500);
- }
- }
- console.log(listingsViewed);
- console.log(JSON.stringify(listingsViewed));
- })();
- }
- }
- console.log("greasemonkey script firing..");
- if (window.confirm("Do you really want to run the auto-responder?")) {
- interval = setInterval(main_script, 5000);
- }
- //
- /*
- function resolved(result) {
- console.log('Resolved');
- }
- function rejected(result) {
- console.error(result);
- }*/
- //GM.setValue("visited_"+window.location.href, true);
- /*(async () => {
- let page_visited = await GM.getValue("visited_"+window.location.href, false);
- if (page_visited) {
- alert("We have visited " + window.location.href + " before.");
- } else {
- alert("We have never visited " + window.location.href + " before.");
- GM.setValue("visited_"+window.location.href, true);
- }
- window.addEventListener('popstate', (event) => {
- console.log("location: " + document.location + ", state: " + JSON.stringify(event.state));
- });
- })();*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement