Leave a Comment

16

2

Task:

Make a program that leaves a comment on this question. The content should be 1234567890123456.

Edits:

  1. You may leave your usernames and passwords as U and P if you are using username and password. (if applicable)

  2. No URL Shorteners (if applicable)

  3. You may not use the browser console.

Meinkraft

Posted 2016-07-05T19:56:29.260

Reputation: 279

Welcome to PPCG! Interesting first post! Seems a bit hard in most languages though, but I think it's good anyway. – Rɪᴋᴇʀ – 2016-07-05T20:03:19.707

27Please post a screenshot of your inbox at the end of this challenge... – Martin Ender – 2016-07-05T20:03:30.843

You should maybe make a post on Meta where people can test their submissions, but this is completely optional. – Loovjo – 2016-07-05T20:05:11.100

@Loovjo You can always delete your comment – Bálint – 2016-07-05T20:06:05.000

@Bálint That may be a bit tedious to do every time you test the program. – Loovjo – 2016-07-05T20:07:11.723

@Loovjo I can only post once every 40 minutes :( but I made the page. – Meinkraft – 2016-07-05T20:08:54.250

Oh, by the way, if you decide to post on meta, you should maybe use another account, since you'll be getting a lot of unnecessary notifications otherwise. – Loovjo – 2016-07-05T20:19:15.557

I don't think creating a meta post to test answers is a good idea. – Dennis – 2016-07-05T22:42:36.390

1To the people testing their code: please delete your comment(s) after you submit them, to keep the comments on this challenge from getting unnecessarily long and spammy, and to make the mods' jobs easier. – Mego – 2016-07-05T23:15:45.967

Also, retrieving the access_token from a browser session is difficult (if not impossible) for some languages. – LegionMammal978 – 2016-07-06T00:10:20.060

1Does nobody else think this is underspecified? Like, how the hell do I do that? Aren't questions meant to be self-contained? Where's the link to the API docs? – cat – 2016-07-06T00:22:46.987

4@cat While I agree that the specifications are rather sparse, I think it's because there just isn't a whole that needs to be said. Also note that using the Stack Exchange API isn't required by the question, so including API docs in the question would be rather superfluous. – Alex A. – 2016-07-06T01:03:04.650

This works on specific languages only. Many languages do not support Internet. – None – 2016-07-06T05:35:34.600

3Voting to close as unclear because looking at the existing answers and comments it seems to me that this needs a) to explicitly white-list the permitted inputs (on a plain reading answers may take no input, but some of them seem to be taking auth details and bypassing the implied requirement to use username and password to log in); b) to clarify what is meant by "You may leave your usernames and passwords as U and P, respectively" (which I would interpret as telling us to hardcode "U" and "P", but which others have clearly interpreted differently). – Peter Taylor – 2016-07-06T08:21:28.053

@PeterTaylor Thanks for the suggestions. I edited the question just now. Are there any other suggestions you would like me to add? – Meinkraft – 2016-07-06T17:00:12.033

Can you clarify what you mean by URL shorteners? Clearly bit.ly and goo.gl would not be disallowed, but what about http://ppcg.lol, which is a custom domain owned by Quill that redirects to this site?

– Mego – 2016-07-08T03:17:23.047

@Mego if you want to access this question, you have to use codegolf.stackexchange.com/questions/84546 and not ppcg.lol/q/84546 (I commented to Sirens to change his answer because he used ppcg) – Meinkraft – 2016-07-08T18:58:53.977

11234567890123456 (this is not sent by script :P) – Matthew Roh – 2017-03-18T07:46:01.360

12345678901‌​23456 – None – 2019-08-24T07:49:14.193

12345678901‌​23456 – None – 2019-08-24T07:49:36.490

123456789023456 – None – 2019-08-24T07:49:43.020

123456789023456 – None – 2019-08-24T07:49:44.380

Answers

11

Javascript with jQuery, 127 bytes

$.post("//codegolf.stackexchange.com/posts/84546/comments",{comment:"12345678901‌​23456",fkey:StackExchange.options.user.fkey})

Thanks to Ismael Miguel and nicael for a few bytes.

The fkey parameter is unique to your account, and can be found by examining local storage contents of your browser with a StackExchange page open. This code must be run in a browser with an open StackExchange session present. It automatically loads the fkey parameter from the browser's local storage (previous versions of this submission required it to be manually entered).

Unfortunately, the ppcg.lol URL can't be used, because it doesn't pass POST requests through.

Fun fact: if you attempt to run this code without the proper fkey value, you get an HTTP 418 response:

teapot

Example of how to find the fkey value in Chrome:

fkey

Apparently Winterbash stuff is still being stored. Neat.

For reference, the same thing in vanilla Javascript would be 314 bytes (thanks again to Ismael Miguel and nicael for some bytes off):

with(new XMLHttpRequest()){
open("POST","//codegolf.stackexchange.com/posts/84546/comments",1)
setRequestHeader("Content-type","application/x-www-form-urlencoded")
setRequestHeader("Content-length",62)
setRequestHeader("Connection","close")
send("comment=1234567890123456&fkey="+StackExchange.options.user.fkey}))}

Mego

Posted 2016-07-05T19:56:29.260

Reputation: 32 998

I was just about to write this. Please add a Stack Snippet. – NoOneIsHere – 2016-07-06T02:28:31.260

I like how you use the request to access the site instead of using selenium! – Meinkraft – 2016-07-06T02:31:28.740

1@NoOneIsHere A Stack Snippet isn't possible, because they run sandboxed, without access to the browser's cookie/session data. Additionally, I'm not certain if Stack Snippets even allow POST requests at all. – Mego – 2016-07-06T05:04:15.047

Instead of var http=new XMLHttpRequest();with(http){, you can do with(new XMLHttpRequest()){. Alternativelly, you can drop the http: from your URLs. Also, {"comment":"1234567890123456","fkey":localstorage.getItem('se:fkey')} can be just {comment:1234567890123456,fkey:localStorage.getItem`se:fkey`}. Notice that you wrote localstorage, when it MUST be localStorage. Final result: $.post("//codegolf.stackexchange.com/posts/84546/comments",{comment:1234567890123456,fkey:localStorage.getItem`se:fkey`}) (121 bytes) – Ismael Miguel – 2016-07-06T10:55:23.483

I'm downvoting this answer. Not because my solution was completelly disregarded, but because it just simply doesn't work with localstorage. You MUST have localStorage! – Ismael Miguel – 2016-07-08T00:38:11.117

3@IsmaelMiguel Downvoting for accidentally missing the capitalization on one letter in code seems a bit ridiculous, but that's your call. It's fixed now. – Mego – 2016-07-08T00:40:04.663

@Mego No. Downvoting because it is broken is NOT ridiculous. – Ismael Miguel – 2016-07-08T00:40:49.837

2@IsmaelMiguel Downvoting because of a slight typo is. – Mego – 2016-07-08T00:41:08.350

@Mego Still broken. Javascript is case-sensitive. – Ismael Miguel – 2016-07-08T00:41:30.040

@IsmaelMiguel Try it now - it was actually some of your golfing suggestions that broke it, along with me missing that the se:fkey parameter from localStorage needed to be sliced. – Mego – 2016-07-08T03:14:40.247

@Mego The "broken" part refered to localstorage not working. My suggestions weren't perfect, but, I wasn't expecting you to use them anyway. The only one I expected was changing localstorage to have the correct spelling. Incorrect spelling of it means that the answer won't work as is broken. – Ismael Miguel – 2016-07-08T08:09:53.237

StackExchange.options.user.fkey is shorter than localStorage["se:fkey"].slice(0,32) :) – nicael – 2016-07-09T12:52:24.747

14

AppleScript with Safari, 269 287 bytes

tell application "Safari"
activate
tell window 1
set current tab to (make new tab with properties {URL:"http://codegolf.stackexchange.com/q/84546"})
delay 5
do JavaScript "$('a')[66].click();$('textarea')[0].val('1234567890123456');$('input')[5].click()" in current tab
end tell
end tell

To use this you need to enable Safari Developer Settings and then enable Allow JavaScript from Apple Events. I'm not sure if it's cheating or not to use the existing keychain + cookies but oh well.

I also managed to do this in OSX's Automator by just automating the clicks and keystrokes however I didn't think it stayed true to the idea of the challenge

Sirens

Posted 2016-07-05T19:56:29.260

Reputation: 241

2Hello, and welcome to PPCG! This is great! That's not cheating. You also need to enable JS for all JavaScript answers. – NoOneIsHere – 2016-07-06T03:32:09.083

Welcome to PPCG! While this is certainly a creative answer, it is not self-contained, as it does not take the username and password as input. – Patrick Roberts – 2016-07-06T04:03:42.903

@PatrickRoberts Totally understand but the line between self contained and not gets a little blurry with this sort of prompt considering all the moving parts you need. Mego's answer involves pulling the token out of Chrome's traffic analyzer which is where I'd personally draw the grey area line. Mine's most certainly not entirely self contained that's for sure' – Sirens – 2016-07-06T05:06:50.717

@PatrickRoberts Nowhere in the challenge does it say that taking the username and password as input is required. In fact, all it does say is (paraphrased) "post this comment to this question" and "you may leave username and password as U and P, respectively". This submission fulfills the requirements, and thus is valid. – Mego – 2016-07-06T05:12:57.807

1@Sirens Thanks for the answer. I just edited the question so can you not use URL Shorteners? – Meinkraft – 2016-07-06T17:03:22.190

1It doesn't appear to work... I mean, it does click the flag link, not the commenting link. – nicael – 2016-07-09T10:25:04.990

1The OP modified the challenge to disallow URL shorteners, which invalidates this answer until you swap ppcg.lol for codegolf.stackexchange.com. – Alex A. – 2016-07-09T19:21:39.847

I'm temporarily removing this post in compliance with our policy about answers not meeting the challenge specification for the reasons cited above. Please feel free to edit your answer and to flag it for moderator attention so it can be undeleted.

– Dennis – 2016-07-19T05:24:10.733

@nicael What OS version are you on? This still works for me on Sierra beta 6 – Sirens – 2016-08-28T02:40:01.660

7

Python 3.5 with Selenium Webdriver, 485 427 469 461 449 414 403 bytes:

from selenium.webdriver import*;import time;D=Chrome();I=lambda k:D.find_element_by_name(k);C='comment';D.get('http://www.codegolf.stackexchange.com/users/login');I('email').send_keys(U);Z=I('password');Z.send_keys(P);Z.submit();D.get('http://www.codegolf.stackexchange.com/questions/84546');D.find_element_by_link_text('add a '+C).click();E=I(C);E.send_keys('1234567890123456');time.sleep(1);E.submit()

A full program utilizing a simple Python selenium web driver solution. Works in Chrome, although it needs ChromeDriver installed to work. Works by renaming U and P to the user's Stack Exchange email and password, respectively. However, if there are any issues getting ChromeDriver installed, here is a FireFox solution that gets executed in the exact same manner as the above solution and does not need any drivers, although it is currently 1 byte longer at 414 404 bytes:

from selenium.webdriver import*;import time;D=Firefox();I=lambda k:D.find_element_by_name(k);C='comment';D.get('http://www.codegolf.stackexchange.com/users/login');I('email').send_keys(U);Z=I('password');Z.send_keys(P);Z.submit();D.get('http://www.codegolf.stackexchange.com/questions/84546');D.find_element_by_link_text('add a '+C).click();E=I(C);E.send_keys('1234567890123456');time.sleep(1);E.submit()

Also, if a function is wanted, here is a solution using an anonymous lambda function, currently standing at 513 455 497 489 477 449 431 bytes and using Chrome as the browser.

lambda U,P:exec("from selenium.webdriver import*;import time;D=Chrome();I=lambda k:D.find_element_by_name(k);C='comment';D.get('http://www.codegolf.stackexchange.com/users/login');I('email').send_keys(U);Z=I('password');Z.send_keys(P);Z.submit();D.get('http://www.codegolf.stackexchange.com/questions/84546');D.find_element_by_link_text('add a '+C).click();E=I(C);E.send_keys('1234567890123456');time.sleep(1);E.submit()",locals())

However, if there are, again, any issues regarding ChromeDriver, here is the same type of solution but this time using Firefox, currently standing at 442 432 bytes:

lambda U,P:exec("from selenium.webdriver import*;import time;D=Firefox();I=lambda k:D.find_element_by_name(k);C='comment';D.get('http://www.codegolf.stackexchange.com/users/login');I('email').send_keys(U);Z=I('password');Z.send_keys(P);Z.submit();D.get('http://www.codegolf.stackexchange.com/questions/84546');D.find_element_by_link_text('add a '+C).click();E=I(C);E.send_keys('1234567890123456');time.sleep(1);E.submit()",locals())

You call these lambda functions by simply renaming the function as anything valid and then calling with your email and password like a normal function. For instance, if the function were named H, you would call it like H(Email, Password).

R. Kap

Posted 2016-07-05T19:56:29.260

Reputation: 4 730

Thank you so much for introducing me to Selenium Webdriver. – Maltysen – 2016-07-06T05:32:43.043

@Maltysen No problem. :) – R. Kap – 2016-07-06T05:34:35.993

Chrome is shorter than Firefox? – Rɪᴋᴇʀ – 2016-07-06T20:50:29.253

@EᴀsᴛᴇʀʟʏIʀᴋ Well, I could not get it working with Webdriver before. I finally have though, and my solution is now 11 bytes shorter! :D – R. Kap – 2016-07-06T22:19:45.127

2

Swift 2.2 on iOS, 380 bytes

let r = NSMutableURLRequest(URL:NSURL(string:"http://codegolf.stackexchange.com/posts/84546/comments")!)
r.HTTPMethod = "POST"
r.HTTPBody = try!NSJSONSerialization.dataWithJSONObject(["comment":"1234567890123456","fkey":UIWebView().stringByEvaluatingJavaScriptFromString("localstorage.getItem('se:fkey')")!],options:[])
NSURLSession.sharedSession().dataTaskWithRequest(r).resume()

Assumes the user is logged in to Code Golf with Safari and has cookies enabled. Also assumes UIKit is implicitly imported and available. JSON serialization is so verbose in Cocoa... Might update this with a Swift 3 solution and optionally macOS/Linux testable answers.

JAL

Posted 2016-07-05T19:56:29.260

Reputation: 304

6stringByEvaluatingJavaScriptFromString brilliant – cat – 2016-07-06T16:32:42.070

1

@cat it gets better.

– JAL – 2016-07-06T16:37:46.297

2I'm angry now. I didn't think it was possible to hate Obejctive-C more than I did... – cat – 2016-07-06T16:40:28.417

0

Java 8 with Selenium Webdriver, 684 bytes:

import java.util.*;import org.openqa.selenium.*;import org.openqa.selenium.firefox.*;class Leave_a_Comment_PPCG_Challenge_Golfed_Version_1{static void Y(String U,String P){WebDriver D=new FirefoxDriver();D.get("http://www.codegolf.stackexchange.com/users/login");D.findElement(By.name("email")).sendKeys(U);WebElement Z=D.findElement(By.name("password"));Z.sendKeys(P);Z.submit();D.get("http://www.codegolf.stackexchange.com/questions/84546");D.findElement(By.linkText("add a comment")).click();WebElement V=D.findElement(By.name("comment"));V.sendKeys("1234567890123456");D.findElement(By.xpath("//input[@value='Add Comment']")).click();}public static void main(String[]a){Scanner I=new Scanner(System.in);Y(I.next(),I.next());}}

A direct adaptation in Java 8 of my second full program answer in Python. Works perfectly in Firefox and asks for space separated email and password input immediately when the program starts. In other words, the input is in the format Email Password where the space in between the two is needed.

R. Kap

Posted 2016-07-05T19:56:29.260

Reputation: 4 730

The code you have here is longer than 684, haha – Poke – 2019-05-21T06:33:30.757