Skip to content

Instantly share code, notes, and snippets.

@LeoDJ
Last active July 11, 2017 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LeoDJ/4514267d6cf3867ad95852e12c7fe29a to your computer and use it in GitHub Desktop.
Save LeoDJ/4514267d6cf3867ad95852e12c7fe29a to your computer and use it in GitHub Desktop.
Greenshot Imgur Upload Script via External Command

Problem

I don't have admin rights on a PC, but do have Greenshot installed. Having said that, greenshot was installed without any online publishing plugins, but with the Office and External Command plugins.
So I set out and created a hacky solution with a more or less simple vbs script that uploads images to imgur using the external command plugin.

Installation Instructions

  • download the .vbs and the .bat file and place them in some folder (has to be in the same folder)
  • open Greenshot Settings
    • go to the "Plugins" tab
    • select "External command Plugin"
    • click "Configure"
    • in the new window, click "New"
    • enter any name you want
    • click on the three ... behind "Command" and locate the .bat file
    • OK -> OK -> OK
  • You're done
  • now you can upload images to imgur the same way you would with the official plugin

Delete Picture

If you accidentally uploaded a picture and want to remove it again, a log of all uploaded pictures will be kept in the same folder, in the uploadLog.csv.
To delete a picture, simply locate the picture you want to delete, copy the "deleteHash" and then go to http://imgur.com/delete/[yourDeleteHash] and click "yes"

Additional Information

If you are unable to upload a picture and the blank CMD window stays open for about 10s, you are most likely behind a proxy and need to configure it in the script.
If you don't have access to the Windows proxy settings and it is set to autmatic configuration, you need to manually find out your proxy using the following method:

  1. enter ping wpad in CMD
  2. copy the domain (most likely looks like wpad.domain.tld)
  3. get the wpad.dat by typing http://wpad.<yourDomain.yourTld>/wpad.dat into a browser and downloading the file
  4. now you have to find your proxy server by looking up the front part of your IP address in the list
  5. enter your proxy in line 25 of the vbs script
REM this file is only needed, because Greenshot can't execute .vbs files directly
@echo off
pushd %~dp0
cscript imgurUpload.vbs %*
Set args = Wscript.Arguments
Set WshShell = CreateObject("WScript.Shell")
Set oExec = WshShell.Exec("clip")
Set oIn = oExec.stdIn
Dim xHttp , URL
URL = "https://api.imgur.com/3/image"
uploadLogFile = "uploadLog.csv"
fileName = args(0)
img = encodeBase64(readBytes(fileName)) 'convert image to base64
jsonToSend = "{""image"": """ & img & """, ""type"": ""base64""}"
Set xHttp = CreateObject("Msxml2.ServerXMLHTTP.6.0")
On Error Resume Next
xHttp.Open "POST",URL, False
If Err Then 'handle errors
WScript.Echo Err.Description & " [0x" & Hex(Err.Number) & "]"
WScript.Quit 1
End If
On Error Goto 0 'disable error handling again
xHttp.setRequestHeader "Content-Type", "application/json"
xHttp.setRequestHeader "Authorization", "Client-ID ef782ecfe507f1f" 'I'm letting you guys have my Client key, be gentle with it
'xHttp.setProxy 2, "https=yourProxy:yourPort", "" 'internal proxy needs to be set if you are behind a proxy
xHttp.send jsonToSend
res = xHttp.responseText
set objJSONDoc = nothing
set objResult = nothing
success = getJSONSuccess(res)
If success = "true" then
link = getJSONVal(res, "link")
deleteHash = getJSONVal(res, "deletehash")
'log uploaded file to csv (for optional deletion if necessary)
logUpload link, deleteHash, fileName
'put link in clipboard
oIn.Write link
oIn.Close
Wscript.Echo "Upload successful. " & link & " copied to clipboard."
Else
Wscript.Echo "Upload failed, log written to failedUpload.txt"
writeString "failedUpload.txt", res
End If
Function logUpload(link, deleteHash, file)
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FileExists(uploadLogFile) Then
writeString uploadLogFile, "time;link;deleteHash;fileName"
End If
Set f = fso.OpenTextFile(uploadLogFile, 8)
f.WriteLine Now() & ";" & link & ";" & deleteHash & ";" & file
f.Close
End Function
Function getJSONSuccess(json)
keyString = ",""success"":"
startPos = InStr(res, keyString) + Len(keyString)
endPos = InStr(startPos, res, ",")
getJSONSuccess = Mid(json, startPos, endPos-startPos)
End Function
Function getJSONVal(json, key) 'crude implementation, probably breaks for other data formats than current imgur API
keyString = ",""" & key & """:"""
startPos = InStr(res, keyString) + Len(keyString)
endPos = InStr(startPos, res, """")
getJSONVal = Mid(json, startPos, endPos-startPos)
getJSONVal = Replace(getJSONVal, "\", "")
End Function
private function encodeBase64(bytes)
dim DM, EL
Set DM = CreateObject("Microsoft.XMLDOM")
' Create temporary node with Base64 data type
Set EL = DM.createElement("tmp")
EL.DataType = "bin.base64"
' Set bytes, get encoded String
EL.NodeTypedValue = bytes
encodeBase64 = Replace(EL.Text, Chr(10), "") 'remove new lines
end function
private function readBytes(file)
dim inStream
' ADODB stream object used
set inStream = WScript.CreateObject("ADODB.Stream")
' open with no arguments makes the stream an empty container
inStream.type = 1 'binary
inStream.Open
inStream.LoadFromFile(file)
readBytes = inStream.Read()
end function
private Sub writeString(outFile, str)
Set objFSO=CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile(outFile,True)
objFile.WriteLine str
objFile.Close
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment