PostItLater

Schedule reddit comments and posts. Requires PostItLater local server.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्क्रिप्ट व्यवस्थापक एक्स्टेंशन इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्क्रिप्ट व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला Stylus सारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

ही स्टाईल इंस्टॉल करण्यासाठी तुम्हाला एक युझर स्टाईल व्यवस्थापक इंस्टॉल करावे लागेल.

(माझ्याकडे आधीच युझर स्टाईल व्यवस्थापक आहे, मला इंस्टॉल करू द्या!)

// ==UserScript==
// @name         PostItLater
// @namespace    http://lyxi.ca
// @version      0.1
// @homepage     https://github.com/Lyxica/PostItLater
// @description  Schedule reddit comments and posts. Requires PostItLater local server.
// @author       Lyxica
// @match        *.reddit.com/*
// @grant unsafeWindow
// @grant GM_xmlhttpRequest
// @connect localhost
// ==/UserScript==

(function() {
    'use strict';
    /* Setup link pages */
    if (document.URL.split("?")[0].endsWith("submit")) {
        SetupForLink();
    } else {
		SetupForComments();
	}
	/*
	oldReply = reply
	reply = function(e) { console.log("hooked"); oldReply(e); }
	*/
})();


function SetupForComments() {
	unsafeWindow.commentLater = function(e) {
		console.log(1);
		var form = e.parentNode.parentNode.parentNode.parentNode;
		var content = form.querySelector("textarea").value;
		var date = new Date(form.querySelector("input[type=datetime-local]").value);
		var thing_id = form.querySelector("input[name=thing_id]").value
		if (content == "") { return; }
           GM_xmlhttpRequest({
               method: "POST",
               url: "http://localhost:4958/",
               data: JSON.stringify({
					"type": "comment",
					"epoch": Math.floor(date / 1000),
					"content": content,
					"thing": thing_id
            })
        });
	}

	/* Setup link reply form */
	var form = document.querySelector("form.usertext.cloneable");
	var buttons = form.querySelector(".usertext-buttons");
	buttons.innerHTML = '<button class="btn" name="later" value="form" onclick="commentLater(this)" type="button">later</button>' + buttons.innerHTML;
	buttons.insertAdjacentHTML("afterEnd", '<input type="datetime-local" id="posting-time">');
	var now=new Date();
    var real_now = new Date(now.getTime()-now.getTimezoneOffset()*60000).toISOString().substring(0,17) + "00"
    document.querySelector("#posting-time").value = real_now;
}
function SetupForLink() {
	document.querySelector("#newlink > div.spacer").innerHTML = '<button class="btn" name="later" value="form" onclick="submitLater(this)" type="button">later</button>' + document.querySelector("#newlink > div.spacer").innerHTML;
        document.querySelector("#newlink > div.spacer").insertAdjacentHTML("afterEnd", '<input type="datetime-local" id="posting-time">')
        var now=new Date();
        var real_now = new Date(now.getTime()-now.getTimezoneOffset()*60000).toISOString().substring(0,17) + "00"
        document.querySelector("#posting-time").value = real_now;
        unsafeWindow.traverseForFormNode = function(e) {
            var node = e;
            for (var i = 0; i < 10000; i++ ) {
                if (node.tagName == "FORM") return node;
                node = node.parentNode
            }
            return null;
        }
        unsafeWindow.submitLater = function(e) {
            var node = unsafeWindow.traverseForFormNode(e);
            var subreddit = "r/" + node.querySelector("#sr-autocomplete").value;
            var title = node.querySelector("textarea.title").value;
            var type = node.querySelector("li.selected > a").text;
			var date = new Date(node.querySelector("#posting-time").value);
            var content;
            if (type == "link") {
                content = node.querySelector("input#url").value
            } else if (type == "text") {
                content = node.querySelector("textarea[name=text]").value
                type = "self"; // Only element I found to distinguish between link types refers to "self" posts as "text"
            } else {
                throw new Error("Unknown link type");
            }
            if (subreddit == "" || title == "" || content == "") { return; }
            GM_xmlhttpRequest({
                method: "POST",
                url: "http://localhost:4958/",
                data: JSON.stringify({
					"type": type,
					"epoch": Math.floor(date / 1000),
					"content": content,
                    "thing": subreddit,
                    "title": title,
                })
            });
        }
}