Auto Vote WP

Automatically star all parts of a Wattpad story

目前為 2017-05-12 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Auto Vote WP
// @namespace    http://hermanfassett.me
// @version      0.1
// @description  Automatically star all parts of a Wattpad story
// @author       Herman Fassett
// @match        https://www.wattpad.com/story/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    // On load, create a button
    function main() {
        // Do not add button if no user logged in
        if (wp.user === null) return;
        // Get container
        var container = document.querySelector(".story-controls");
        // Create button
        var button = $("<button/>",
        {
            text: "Auto Vote!",
            click: voteForStory,
            class: "btn btn-orange btn-sm btn-inline",
            id: "auto-vote-button-userscript"
        });
        // Add to container
        $(container).append(button);
    }
    // Get story id from window location pathname
    function getStoryID() {
        var match = window.location.pathname.match(/\/story\/(\d+)/i);
        var storyID = match[1];
        return storyID;
    }
    // Get the parts of a story
    function getParts(storyID, callback) {
        // Construct API url
        var url = "https://www.wattpad.com/api/v3/stories/" + storyID + "?fields=parts";
        // Get the parts of the given story and return it in a callback function
        $.getJSON(url, function(response) {
            callback(response.parts);
        });
    }
    // Vote for a part as a promise
    function voteForPart(storyID, partID) {
        return new Promise(function(resolve, reject) {
            // Construct API call url
            var url = "https://www.wattpad.com/api/v3/stories/" + storyID + "/parts/" + partID + "/votes";
            // Make a POST to url. Some of headers might not be necessary...
            $.ajax({
                url: url,
                type: "POST",
                beforeSend: function(xhr) {
                    xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
                    xhr.setRequestHeader("Accept-Language", "en-US,en;q=0.8");
                    xhr.setRequestHeader("Authorization", "");
                    xhr.setRequestHeader("Accept", "*/*");
                    xhr.setRequestHeader("Authority", "www.wattpad.com");
                }
            }).done(function(data) {
                // console.log("Successfully voted on part " + partID + " of story " + storyID, data);
                resolve(data);
            }).fail(function() {
                reject("Failed to vote on part " + partID + " of story " + storyID + ". You may have run out of your 100 votes for the day.");
            });
        });
    }
    // Vote for all parts of story
    function voteForStory() {
        var storyID = getStoryID();
        // Get parts
        getParts(storyID, function(parts) {
            // Loop through parts
            for (var i = 0; i < parts.length; i++) {
                // Skip if already voted
                if (parts[i].voted) continue;
                // Otherwise, vote
                voteForPart(storyID, parts[i].id).then(function(result) {
                    // Success
                    console.log(result);
                }).catch(function(error) {
                    // Error
                    console.log("Error: " + error);
                });
            }
        });
    }
    // Start main function
    main();
})();