You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.1 KiB
PowerShell

#!/usr/bin/env pwsh
<#
.SYNOPSIS
Takes a screenshot and puts the URl into the clipboard
.DESCRIPTION
Uses grim and slurp to create a screenshot and uploads it into a matrix content repository.
A GETable URL is placed into the clipboard via wl-copy and optionally sent to the specified room.
.NOTES
Example config at $HOME/.config/matrix-screenshot.ps1:
```
$roomId = '!XpxHNLnPsnNEbScyeh:imninja.net'
$accessToken = 'NotaReALaCceSSTOk3n'
$homeserverUrl = 'https://imninja.net'
```
#>
[CmdletBinding()]
param (
# Takes a fullscreen screenshot instead of selecting an area.
[switch]
$Fullscreen
)
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version 3.0
# import config
. (Join-Path $env:HOME '.config/matrix-screenshot.ps1')
$tmp = New-TemporaryFile
if ($Fullscreen) {
grim $tmp.FullName
} else {
grim -g $(slurp) $tmp.FullName
}
if (($tmp | Get-Item).Length -eq 0) {
Write-Error ('Screenshot file "{0}" is empty' -f $tmp.FullName) -ErrorAction Continue
$tmp | Remove-Item
exit
}
$mediaSplat = @{
Authentication = 'Bearer'
Token = $accessToken | ConvertTo-SecureString -AsPlainText
Headers = @{
'Content-Type' = 'image/jpeg'
}
Method = 'Post'
Uri = ($homeserverUrl+'/_matrix/media/r0/upload')
InFile = $tmp.FullName
}
$media = Invoke-RestMethod @mediaSplat
$tmp | Remove-Item
$serverName = $media.content_uri.Split('/')[2]
$mediaId = $media.content_uri.Split('/')[3]
$mediaUrl = '{0}/_matrix/media/r0/download/{1}/{2}' -f $homeserverUrl,$serverName,$mediaId
$mediaUrl | wl-copy --trim-newline
if ($roomId) {
$eventSplat = @{
Authentication = 'Bearer'
Token = $accessToken | ConvertTo-SecureString -AsPlainText
Method = 'Put'
Uri = '{0}/_matrix/client/r0/rooms/{1}/send/{2}/{3}' -f $homeserverUrl,$roomId,'m.room.message',($tmp.Name)
Body = @{
body = $tmp.Name
msgtype = 'm.image'
url = $media.content_uri
} | ConvertTo-Json
}
$event = Invoke-RestMethod @eventSplat
Write-Output ('{0}' -f $event.event_id)
}