-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
90 lines (76 loc) · 1.39 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"net/url"
"os"
"strings"
"github.com/atotto/clipboard"
)
func main() {
var s, err = clipboard.ReadAll()
if err != nil {
os.Exit(1)
return
}
for {
var temp string
temp, err = clipboard.ReadAll()
switch {
case err != nil:
os.Exit(1)
return
case temp != s:
var new string
new, err = stripQuery(temp)
if err != nil || new == "" {
// Silently fail on non-URL.
break
}
err = clipboard.WriteAll(new)
if err != nil {
os.Exit(1)
return
}
s = temp
}
}
}
// stripQuery removes any known tracking parameters from |s|.
func stripQuery(s string) (string, error) {
var u, err = url.Parse(s)
if err != nil {
return "", err
}
// Seems virtually impossible that if the scheme is not http(s) that
// we need to strip any query parameters.
if !strings.HasPrefix(u.Scheme, "http") {
return s, nil
}
var q = u.Query()
switch u.Host {
case "open.spotify.com":
q.Del("si")
case "www.amazon.com":
if u.Path == "/s" {
q = url.Values{
"k": []string{q.Get("k")},
}
break
}
q = url.Values{}
case "twitter.com", "x.com":
q.Del("t")
case "www.threads.net":
q.Del("xmt")
}
for k, _ := range q {
if strings.HasPrefix(k, "utm_") {
q.Del(k)
}
// Yahoo tracking parameters.
if strings.HasPrefix(k, "guce_") || k == "guccounter" {
q.Del(k)
}
}
u.RawQuery = q.Encode()
return u.String(), nil
}