15
\$\begingroup\$

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.

\$\endgroup\$
  • 26
    \$\begingroup\$ Please post a screenshot of your inbox at the end of this challenge... \$\endgroup\$ – Martin Ender Jul 5 '16 at 20:03
  • 1
    \$\begingroup\$ To 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. \$\endgroup\$ – Mego Jul 5 '16 at 23:15
  • 1
    \$\begingroup\$ Does 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? \$\endgroup\$ – cat Jul 6 '16 at 0:22
  • 4
    \$\begingroup\$ @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. \$\endgroup\$ – Alex A. Jul 6 '16 at 1:03
  • 3
    \$\begingroup\$ Voting 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). \$\endgroup\$ – Peter Taylor Jul 6 '16 at 8:21
11
\$\begingroup\$

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}))}
\$\endgroup\$
  • \$\begingroup\$ I was just about to write this. Please add a Stack Snippet. \$\endgroup\$ – NoOneIsHere Jul 6 '16 at 2:28
  • \$\begingroup\$ I like how you use the request to access the site instead of using selenium! \$\endgroup\$ – Meinkraft Jul 6 '16 at 2:31
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – Mego Jul 6 '16 at 5:04
  • 3
    \$\begingroup\$ @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. \$\endgroup\$ – Mego Jul 8 '16 at 0:40
  • 2
    \$\begingroup\$ @IsmaelMiguel Downvoting because of a slight typo is. \$\endgroup\$ – Mego Jul 8 '16 at 0:41
14
\$\begingroup\$

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

\$\endgroup\$
  • 2
    \$\begingroup\$ Hello, and welcome to PPCG! This is great! That's not cheating. You also need to enable JS for all JavaScript answers. \$\endgroup\$ – NoOneIsHere Jul 6 '16 at 3:32
  • \$\begingroup\$ 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. \$\endgroup\$ – Patrick Roberts Jul 6 '16 at 4:03
  • 1
    \$\begingroup\$ @Sirens Thanks for the answer. I just edited the question so can you not use URL Shorteners? \$\endgroup\$ – Meinkraft Jul 6 '16 at 17:03
  • 1
    \$\begingroup\$ It doesn't appear to work... I mean, it does click the flag link, not the commenting link. \$\endgroup\$ – nicael Jul 9 '16 at 10:25
  • 1
    \$\begingroup\$ The OP modified the challenge to disallow URL shorteners, which invalidates this answer until you swap ppcg.lol for codegolf.stackexchange.com. \$\endgroup\$ – Alex A. Jul 9 '16 at 19:21
7
\$\begingroup\$

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).

\$\endgroup\$
  • \$\begingroup\$ Thank you so much for introducing me to Selenium Webdriver. \$\endgroup\$ – Maltysen Jul 6 '16 at 5:32
  • \$\begingroup\$ @Maltysen No problem. :) \$\endgroup\$ – R. Kap Jul 6 '16 at 5:34
  • \$\begingroup\$ Chrome is shorter than Firefox? \$\endgroup\$ – Rɪᴋᴇʀ Jul 6 '16 at 20:50
  • \$\begingroup\$ @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 \$\endgroup\$ – R. Kap Jul 6 '16 at 22:19
2
\$\begingroup\$

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.

\$\endgroup\$
  • 6
    \$\begingroup\$ stringByEvaluatingJavaScriptFromString brilliant \$\endgroup\$ – cat Jul 6 '16 at 16:32
  • 1
    \$\begingroup\$ @cat it gets better. \$\endgroup\$ – JAL Jul 6 '16 at 16:37
  • 2
    \$\begingroup\$ I'm angry now. I didn't think it was possible to hate Obejctive-C more than I did... \$\endgroup\$ – cat Jul 6 '16 at 16:40
0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ The code you have here is longer than 684, haha \$\endgroup\$ – Poke May 21 at 6:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.