Auto Vote WP

Automatically star all parts of a Wattpad story

  1. // ==UserScript==
  2. // @name Auto Vote WP
  3. // @namespace http://hermanfassett.me
  4. // @version 0.2
  5. // @description Automatically star all parts of a Wattpad story
  6. // @author Herman Fassett
  7. // @match https://www.wattpad.com/story/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13. // On load, create a button
  14. function main() {
  15. // Do not add button if no user logged in
  16. if (wp.user === null) return;
  17. // Get container
  18. var container = document.querySelector(".story-controls");
  19. // Create button
  20. var button = $("<button/>",
  21. {
  22. text: "Auto Vote!",
  23. click: voteForStory,
  24. class: "btn btn-orange btn-sm btn-inline",
  25. id: "auto-vote-button-userscript"
  26. });
  27. // Add to container
  28. $(container).append(button);
  29. }
  30. // Get story id from window location pathname
  31. function getStoryID() {
  32. var match = window.location.pathname.match(/\/story\/(\d+)/i);
  33. var storyID = match[1];
  34. return storyID;
  35. }
  36. // Get the parts of a story
  37. function getParts(storyID, callback) {
  38. // Construct API url
  39. var url = "https://www.wattpad.com/api/v3/stories/" + storyID + "?fields=parts";
  40. // Get the parts of the given story and return it in a callback function
  41. $.getJSON(url, function(response) {
  42. callback(response.parts);
  43. });
  44. }
  45. // Vote for a part as a promise
  46. function voteForPart(storyID, partID) {
  47. return new Promise(function(resolve, reject) {
  48. // Construct API call url
  49. var url = "https://www.wattpad.com/api/v3/stories/" + storyID + "/parts/" + partID + "/votes";
  50. // Make a POST to url. Some of headers might not be necessary...
  51. $.ajax({
  52. url: url,
  53. type: "POST",
  54. beforeSend: function(xhr) {
  55. xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
  56. xhr.setRequestHeader("Accept-Language", "en-US,en;q=0.8");
  57. xhr.setRequestHeader("Authorization", "");
  58. xhr.setRequestHeader("Accept", "*/*");
  59. xhr.setRequestHeader("Authority", "www.wattpad.com");
  60. }
  61. }).done(function(data) {
  62. // console.log("Successfully voted on part " + partID + " of story " + storyID, data);
  63. resolve("Successfully voted on part " + partID + " of story with " + data.votes + " votes");
  64. }).fail(function() {
  65. reject("Failed to vote on part " + partID + " of story " + storyID + ". You may have run out of your 100 votes for the day.");
  66. });
  67. });
  68. }
  69. // Vote for all parts of story
  70. function voteForStory() {
  71. var storyID = getStoryID();
  72. // Get parts
  73. getParts(storyID, function(parts) {
  74. // Loop through parts
  75. for (var i = 0; i < parts.length; i++) {
  76. // Skip if already voted
  77. if (parts[i].voted) continue;
  78. // Otherwise, vote
  79. voteForPart(storyID, parts[i].id).then(function(result) {
  80. // Success
  81. console.log(result);
  82. }).catch(function(error) {
  83. // Error
  84. console.log("Error: " + error);
  85. });
  86. }
  87. });
  88. }
  89. // Start main function
  90. main();
  91. })();