Post comments on Facebook page via console

前端 未结 5 1140
说谎
说谎 2021-02-06 04:10

Like in the image, the Facebook comment box has no submit button, when you write something and press Enter button, the comment posted.

I want to submit the comment via J

5条回答
  •  别跟我提以往
    2021-02-06 04:20

    Here is a working solution after 3 weeks of experimenting (using @Benjamin Solum's fireEvent function):

    • this version posts a comment only for the first post on the page (by using querySelector method)
    • this version can be used only on your personal wall (unless you change the query selectors)

      function fireEvent(type, element, keyCode) {
          var evt;
      
          if(document.createEvent) {
              evt = document.createEvent("HTMLEvents");
              evt.initEvent(type, true, true);
          } else {
              evt = document.createEventObject();
              evt.eventType = type;
          }
      
          evt.eventName = type;
      
          if (keyCode !== undefined){ 
              evt.keyCode = keyCode;
              evt.which = keyCode;
          }
      
          if(document.createEvent) {
              element.dispatchEvent(evt);
          } else {
              element.fireEvent("on" + evt.eventType, evt);
          }
      }
      
      // clicking the comment link - it reveals the combobox
      document.querySelector(".fbTimelineSection .comment_link").click();
      
      setTimeout(function(){
          var combobox = document.querySelector(".fbTimelineSection [role='combobox']");
          var spanWrapper = document.querySelector(".fbTimelineSection [role='combobox'] span");
      
          // add text to the combobox
          spanWrapper.innerHTML = "Thank you!";
      
          var spanElement = document.querySelector(".fbTimelineSection [role='combobox'] span span");
      
          fireEvent("blur", combobox);
          fireEvent("focus", combobox);
          fireEvent("input", combobox);
          fireEvent("keydown", spanElement, 13); // pushing enter
      },2000);
      

提交回复
热议问题