-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathClickLinks.lua
More file actions
1095 lines (934 loc) · 39.5 KB
/
ClickLinks.lua
File metadata and controls
1095 lines (934 loc) · 39.5 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- File: ClickLinks.lua
-- Name: Click Links
-- Original Author: tannerng
-- Continued by : Milestorme
-- Description: Makes URLs clickable + automatic version checking
-- notes: Adds clickable URL links to chat, plus in-game version announcement + one-time update warning.
local ADDON_NAME = ...
local L = LibStub("AceLocale-3.0"):GetLocale("ClickLinks")
-------------------------------------------------
-- FUNCTION INDEX
-------------------------------------------------
-- URL / Formatting
-- formatURL(url) -> Wraps a URL string into a clickable |Hurl:| hyperlink
-- Chat Filtering
-- makeClickable(self, event, msg, ...)
-- -> Chat message event filter; replaces URL-like patterns with clickable links
-- URL Copy Popup / Hyperlink Hook
-- StaticPopupDialogs["CLICK_LINK_CLICKURL"].OnShow(self)
-- -> Populates the edit box with the clicked URL and highlights for copy
-- ItemRefTooltip:SetHyperlink(link)
-- -> Intercepts "url:" links and opens copy popup; otherwise calls original handler
-- Version Checking
-- VersionToNumber(ver) -> Converts "X.Y.Z" to comparable integer
-- SendVersionToGroup() -> Broadcasts local version to guild and (once per group) party/raid
-- f:SetScript("OnEvent", ...) -> Handles login/group roster changes/addon messages for update detection
-- Slash Commands
-- /clicklinks version|ver -> Prints addon version
-- /cl version|ver -> Short alias for version command
-------------------------------------------------
-------------------------------------------------
-- URL Patterns
-------------------------------------------------
-- notes: Patterns used to detect URLs/emails/IPs in chat text. Each match is replaced with a clickable hyperlink.
local URL_PATTERNS = {
-- X://Y most urls
"^(%a[%w+.-]+://[^%s|]+)",
"%f[%S](%a[%w+.-]+://[^%s|]+)",
-- www.X.Y domain and path
"^(www%.[-%w_%%]+%.(%a%a+)/[^%s|]+)",
"%f[%S](www%.[-%w_%%]+%.(%a%a+)/[^%s|]+)",
-- www.X.Y domain
"^(www%.[-%w_%%]+%.(%a%a+))",
"%f[%S](www%.[-%w_%%]+%.(%a%a+))",
-- domain.tld/path (no scheme/www)
"^(%w[%w%._-]+%.(%a%a+)/[^%s|]+)",
"%f[%S](%w[%w%._-]+%.(%a%a+)/[^%s|]+)",
-- domain.tld (no scheme/www)
"^(%w[%w%._-]+%.(%a%a+))",
"%f[%S](%w[%w%._-]+%.(%a%a+))",
-- email
"(%S+@[%w_.-%%]+%.(%a%a+))",
-- ip address with port and path
"^([0-2]?%d?%d%.[0-2]?%d?%d%.[0-2]?%d?%d%.[0-2]?%d?%d:[0-6]?%d?%d?%d?%d/[^%s|]+)",
"%f[%S]([0-2]?%d?%d%.[0-2]?%d?%d%.[0-2]?%d?%d%.[0-2]?%d?%d:[0-6]?%d?%d?%d?%d/[^%s|]+)",
-- ip address with port
"^([0-2]?%d?%d%.[0-2]?%d?%d%.[0-2]?%d?%d%.[0-2]?%d?%d:[0-6]?%d?%d?%d?%d)%f[%D]",
"%f[%S]([0-2]?%d?%d%.[0-2]?%d?%d%.[0-2]?%d?%d%.[0-2]?%d?%d:[0-6]?%d?%d?%d?%d)%f[%D]",
-- ip address with path
"^([0-2]?%d?%d%.[0-2]?%d?%d%.[0-2]?%d?%d%.[0-2]?%d?%d%/[^%s|]+)",
"%f[%S]([0-2]?%d?%d%.[0-2]?%d?%d%.[0-2]?%d?%d%.[0-2]?%d?%d%/[^%s|]+)",
-- ip address
"^([0-2]?%d?%d%.[0-2]?%d?%d%.[0-2]?%d?%d%.[0-2]?%d?%d)%f[%D]",
"%f[%S]([0-2]?%d?%d%.[0-2]?%d?%d%.[0-2]?%d?%d%.[0-2]?%d?%d)%f[%D]"
}
-- Characters commonly appended after URLs that should not be clickable
local URL_TRAILING = {
["."] = true, [","] = true, [";"] = true, [":"] = true,
["!"] = true, ["?"] = true, [")"] = true,
["\""] = true, ["'"] = true,
["]"] = true, ["}"] = true,
}
local function _SplitTrailingURLPunctuation(url)
url = tostring(url or "")
local trailing = ""
while #url > 0 do
local c = url:sub(-1)
if URL_TRAILING[c] then
trailing = c .. trailing
url = url:sub(1, -2)
else
break
end
end
return url, trailing
end
-------------------------------------------------
-- Bare domain TLD whitelist (prevents false positives)
-------------------------------------------------
local ALLOWED_TLDS = {
com = true, net = true, org = true,
io = true, gg = true, co = true,
au = true, uk = true, us = true, nz = true, ca = true,
de = true, fr = true, jp = true, kr = true,
}
local function _CL_IsAllowedTLD(domain)
local tld = domain:match("%.([%a][%a]+)$")
if not tld then return false end
return ALLOWED_TLDS[string.lower(tld)] == true
end
local function formatURL(url)
local trailing
url, trailing = _SplitTrailingURLPunctuation(url)
-- If this is a bare domain (no scheme, no www), enforce TLD whitelist
if not string.find(url, "://", 1, true)
and not string.find(url, "www.", 1, true)
then
if not _CL_IsAllowedTLD(url) then
return url .. trailing
end
end
url = url:gsub("%|", "||")
return "|cff149bfd|Hurl:" .. url .. "|h[" .. url .. "]|h|r" .. trailing
end
-- ------------------------------------------------
-- Safety helpers (Retail 12.x "secret" chat values)
-- ------------------------------------------------
-- notes:
-- Retail 12.x may pass "secret" values into chat frames (e.g. in PvP instances).
-- These values behave like strings in some contexts but error on indexing/string methods.
-- We defensively wrap string operations with pcall and/or skip processing.
local function _CL_SafeCall(fn, ...)
local ok, a, b, c, d = pcall(fn, ...)
if ok then return true, a, b, c, d end
return false
end
local function _CL_CanTreatAsString(v)
if type(v) ~= "string" then return false end
-- Some secret values report as "string" but throw on operations; # is a cheap probe.
return _CL_SafeCall(function(s) return #s end, v)
end
local function _CL_SafeFind(s, pattern, plain)
local ok, idx = _CL_SafeCall(function(str, pat, isPlain)
return string.find(str, pat, 1, isPlain)
end, s, pattern, plain == true)
if ok then return idx end
return nil
end
local function _CL_SafeMatch(s, pattern)
local ok, a, b, c, d = _CL_SafeCall(function(str, pat)
return string.match(str, pat)
end, s, pattern)
if ok then return a, b, c, d end
return nil
end
-------------------------------------------------
-- Chat message filter
-------------------------------------------------
local function makeClickable(self, event, msg, ...)
-- notes: ChatFrame_AddMessageEventFilter callback.
-- notes: Performs a cheap pre-check, then gsubs all URL patterns into clickable links.
-- notes: Returns (false, msg, ...) so the message continues through normal rendering.
-- ElvUI has its own URL filter (CH:FindURL) that also produces |Hurl: links.
-- If both run on the same message, ElvUI re-processes ClickLinks' already-formatted
-- |Hurl: text, matching www. patterns inside it twice and producing garbage like
-- |Hurl:[[www.test.com]www.test.com[www.test.com]].
-- When ElvUI is active and its URL feature is enabled (the default), let ElvUI handle
-- formatting entirely. ClickLinks' SetItemRef copy-box hook still fires on the
-- resulting |Hurl: links, so copy-on-click continues to work.
if _G.ElvUI then
local E = _G.ElvUI[1]
local CH = E and E.GetModule and E:GetModule("Chat", true)
-- CH.db.url defaults to true; skip unless the user has explicitly disabled it in ElvUI
if not (CH and CH.db and CH.db.url == false) then
-- Return nil to signal "no change" so Blizzard keeps the original
-- args. Returning (false, msg, ...) would propagate addon taint to
-- all varargs (including the sender name), which causes
-- "secret string" errors in ChatFrameUtil.SetLastTellTarget on 12.x.
return
end
end
-- Retail 12.x can pass "secret" chat values that error on string methods.
-- Guard against non-string types before any string operations.
if not _CL_CanTreatAsString(msg) then
return
end
-- If the line already contains hyperlinks (items/spells/etc), don't touch it.
if _CL_SafeFind(msg, "|H", true) then
return
end
-- Quick pre-check to avoid unnecessary gsub.
-- Bare domains (e.g. google.com) are caught by the dot-containing patterns below;
-- false positives are filtered by the TLD whitelist inside formatURL().
if not (_CL_SafeFind(msg, "://", true)
or _CL_SafeFind(msg, "www.", true)
or _CL_SafeFind(msg, "@", true)
or _CL_SafeFind(msg, "%d+%.%d+%.%d+%.%d+")
or _CL_SafeFind(msg, "[%w_%-]+%.[%a][%a]")) -- bare domains like google.com
then
return
end
local ok, newMsg = _CL_SafeCall(function(m)
local gsub = string.gsub
for i = 1, #URL_PATTERNS do
m = gsub(m, URL_PATTERNS[i], formatURL)
end
return m
end, msg)
if ok and type(newMsg) == "string" and newMsg ~= msg then
return false, newMsg, ...
end
-- Message wasn't actually modified (e.g. bare "word.word" matched the
-- pre-check but no real URL was found). Return nil so Blizzard keeps the
-- original args untainted — returning (false, msg, ...) would taint the
-- secret BNet sender name on 12.x.
return
end
-------------------------------------------------
-- Chat Filtering: Register on all known chat events (cross-client safe)
-------------------------------------------------
local CHAT_EVENT_SUFFIXES = {
"SAY","YELL","EMOTE",
"WHISPER","WHISPER_INFORM",
"GUILD","OFFICER",
"PARTY","PARTY_LEADER",
"RAID","RAID_LEADER","RAID_WARNING",
"INSTANCE_CHAT","INSTANCE_CHAT_LEADER",
"CHANNEL",
"AFK","DND",
"BATTLEGROUND","BATTLEGROUND_LEADER",
"SYSTEM",
"LOOT","MONEY","CURRENCY",
"SKILL","TRADESKILLS",
"COMBAT_XP_GAIN","COMBAT_HONOR_GAIN","COMBAT_FACTION_CHANGE",
"ACHIEVEMENT","GUILD_ACHIEVEMENT",
"BN_WHISPER","BN_WHISPER_INFORM",
"BN_CONVERSATION","BN_CONVERSATION_NOTICE","BN_CONVERSATION_LIST",
"BN_INLINE_TOAST_ALERT",
"BN_INLINE_TOAST_BROADCAST","BN_INLINE_TOAST_BROADCAST_INFORM",
"BN_INLINE_TOAST_CONVERSATION","BN_INLINE_TOAST_CONVERSATION_INFORM",
"COMMUNITIES_CHANNEL",
"CLUB",
"CLUB_MEMBER_UPDATED","CLUB_MEMBER_ADDED","CLUB_MEMBER_REMOVED",
"CLUB_STREAMS_LOADED","CLUB_STREAM_MESSAGE",
"PET_BATTLE_INFO",
}
local function _CL_RegisterAllChatFilters()
for _, suffix in ipairs(CHAT_EVENT_SUFFIXES) do
local ev = "CHAT_MSG_" .. suffix
pcall(ChatFrame_AddMessageEventFilter, ev, makeClickable)
end
end
_CL_RegisterAllChatFilters()
-------------------------------------------------
-- Ensure system-style messages (e.g. GUILD_MOTD) also get clickable URLs
-------------------------------------------------
-- notes: Some messages (guild MOTD, addon prints, certain system lines) are written directly via ChatFrame:AddMessage
-- notes: and do NOT go through CHAT_MSG_* filters. We wrap AddMessage to catch those too.
-- notes: ElvUI replaces the ChatFrame system but correctly passes messages through CHAT_MSG_* filters,
-- notes: so the AddMessage hook is not needed (and causes triple-message display) when ElvUI is active.
local function _CL_IsElvUIActive()
return _G.ElvUI ~= nil
end
local HookCommunitiesFramesForClickableURLs -- forward declare (used before definition)
local function _CL_TryHookCommunitiesFrames()
if not HookCommunitiesFramesForClickableURLs then return end
pcall(HookCommunitiesFramesForClickableURLs)
end
local function HookChatFramesForClickableURLs()
if _CL_IsElvUIActive() then return end
_CL_TryHookCommunitiesFrames()
if not _G.ChatFrame1 then return end
for i = 1, (NUM_CHAT_WINDOWS or 0) do
local cf = _G["ChatFrame" .. i]
if cf and type(cf.AddMessage) == "function" then
-- If another addon replaced AddMessage after our hook, re-hook safely.
if cf.AddMessage ~= cf.__ClickLinks_WrappedAddMessage then
cf.__ClickLinks_OrigAddMessage = cf.AddMessage
cf.__ClickLinks_WrappedAddMessage = function(self, text, ...)
if _CL_CanTreatAsString(text) then
-- Reuse the same safety rules as makeClickable:
-- 1) Do not touch existing hyperlinks
-- 2) Only process if it looks like it contains a URL/email/IP
if not _CL_SafeFind(text, "|H", true) then
-- makeClickable returns (false, msg, ...) because it's a filter; we only need the transformed msg.
local ok, _, newText = _CL_SafeCall(makeClickable, self, "ADD_MESSAGE", text, ...)
if ok and newText then
text = newText
end
end
end
local orig = self.__ClickLinks_OrigAddMessage
if orig then
return orig(self, text, ...)
end
end
cf.AddMessage = cf.__ClickLinks_WrappedAddMessage
end
end
end
end
-------------------------------------------------
-- Communities / Guild UI message frames (Retail)
-------------------------------------------------
-- notes: The Communities/Guild UI uses its own scrolling message frame and does not always
-- go through CHAT_MSG_* filters or ChatFrame:AddMessage. We hook its AddMessage too.
local function _TryHookMessageFrame(frame)
if _CL_IsElvUIActive() then return end
if not frame then return end
if type(frame.AddMessage) ~= "function" then return end
-- If another addon/UI code replaces AddMessage later, re-hook safely.
if frame.AddMessage == frame.__ClickLinks_WrappedAddMessage then return end
frame.__ClickLinks_OrigAddMessage = frame.AddMessage
frame.__ClickLinks_WrappedAddMessage = function(self, msg, ...)
if _CL_CanTreatAsString(msg) and not _CL_SafeFind(msg, "|H", true) then
local ok, _, newMsg = _CL_SafeCall(makeClickable, self, "MESSAGE_FRAME_ADD_MESSAGE", msg, ...)
if ok and newMsg then
msg = newMsg
end
end
local orig = self.__ClickLinks_OrigAddMessage
if orig then
return orig(self, msg, ...)
end
end
frame.AddMessage = frame.__ClickLinks_WrappedAddMessage
end
HookCommunitiesFramesForClickableURLs = function()
local cf = _G.CommunitiesFrame
if not cf then return end
local candidates = {}
local function addCandidate(frame)
if frame then
candidates[#candidates + 1] = frame
end
end
addCandidate(cf.Chat and cf.Chat.MessageFrame)
addCandidate(cf.Chat and cf.Chat.InsetFrame and cf.Chat.InsetFrame.Chat and cf.Chat.InsetFrame.Chat.MessageFrame)
addCandidate(cf.Chat and cf.Chat.InsetFrame and cf.Chat.InsetFrame.ChatFrame and cf.Chat.InsetFrame.ChatFrame.MessageFrame)
addCandidate(cf.ChatFrame and cf.ChatFrame.MessageFrame)
addCandidate(cf.ChatFrame)
for _, f in ipairs(candidates) do
_TryHookMessageFrame(f)
end
end
-- Try immediately (addon load), and also after login in case chat frames are not fully built yet.
HookChatFramesForClickableURLs()
-- Re-hook safety: some UI mods replace ChatFrame:AddMessage after login.
-- We do a short-lived ticker after login to re-apply our hooks if needed.
local __clRehookTicker
local function _CL_StartRehookTicker()
if __clRehookTicker then return end
if not (C_Timer and C_Timer.NewTicker) then return end
local ticks = 0
__clRehookTicker = C_Timer.NewTicker(5, function()
ticks = ticks + 1
HookChatFramesForClickableURLs()
_CL_TryHookCommunitiesFrames()
if _G.ClickLinks_EnsureSetItemRefHook then _G.ClickLinks_EnsureSetItemRefHook() end
if ticks >= 12 then -- ~60 seconds
__clRehookTicker:Cancel()
__clRehookTicker = nil
end
end)
end
local __clHookFrame = CreateFrame("Frame")
__clHookFrame:RegisterEvent("PLAYER_LOGIN")
__clHookFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
__clHookFrame:RegisterEvent("ADDON_LOADED")
__clHookFrame:SetScript("OnEvent", function(_, event, addonName)
HookChatFramesForClickableURLs()
if event == "PLAYER_LOGIN" then _CL_StartRehookTicker() end
if _G.ClickLinks_EnsureSetItemRefHook then _G.ClickLinks_EnsureSetItemRefHook() end
-- Communities/Guild UI is loaded on-demand on Retail
if event == "ADDON_LOADED" then
if addonName == "Blizzard_Communities" or addonName == "Blizzard_GuildUI" then
_CL_TryHookCommunitiesFrames()
end
else
_CL_TryHookCommunitiesFrames()
end
end)
-------------------------------------------------
-- Copy popup
-------------------------------------------------
-- notes:
-- WoW addons cannot write to the OS clipboard. We show a small edit box with the URL
-- selected and focused so the user can press Ctrl+C.
local function _CL_EnsureCopyBox()
ClickLinksDB = ClickLinksDB or {}
if _G.ClickLinks_CopyBox and _G.ClickLinks_CopyBox.editBox then return _G.ClickLinks_CopyBox end
local template = (BackdropTemplateMixin and "BackdropTemplate") or nil
local f = CreateFrame("Frame", "ClickLinks_CopyBox", UIParent, template)
f:SetFrameStrata("DIALOG")
f:SetSize(420, 70)
local cbp = ClickLinksDB.copyBoxPos
if cbp
and type(cbp.point) == "string"
and type(cbp.relativePoint) == "string"
and type(cbp.x) == "number"
and type(cbp.y) == "number"
then
f:SetPoint(cbp.point, UIParent, cbp.relativePoint, cbp.x, cbp.y)
else
f:SetPoint("CENTER")
end
f:Hide()
f:SetClampedToScreen(true)
f:SetMovable(true)
f:EnableMouse(true)
f:RegisterForDrag("LeftButton")
f:SetScript("OnDragStart", function() f:StartMoving() end)
f:SetScript("OnDragStop", function(self)
self:StopMovingOrSizing()
local point, _, relativePoint, x, y = self:GetPoint()
ClickLinksDB.copyBoxPos = {
point = point,
relativePoint = relativePoint,
x = x,
y = y,
}
end)
if f.SetBackdrop then
f:SetBackdrop({
bgFile = "Interface/Tooltips/UI-Tooltip-Background",
edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
tile = true,
tileSize = 16,
edgeSize = 16,
insets = { left = 4, right = 4, top = 4, bottom = 4 },
})
f:SetBackdropColor(0, 0, 0, 0.95) -- solid dark background
f:SetBackdropBorderColor(0.2, 0.2, 0.2, 1)
end
local title = f:CreateFontString(nil, "ARTWORK", "GameFontNormal")
title:SetPoint("TOP", 0, -10)
title:SetText(L["COPYBOX_TITLE"] or "Copy Link")
local hint = f:CreateFontString(nil, "ARTWORK", "GameFontDisableSmall")
hint:SetPoint("BOTTOM", 0, 10)
hint:SetText(L["COPYBOX_HINT"] or "Press Ctrl+C to copy link")
-- Don't auto-hide on focus loss (dragging/clicking can steal focus)
-- Don't re-highlight on every change (prevents cursor placement)
local close = CreateFrame("Button", nil, f, "UIPanelCloseButton")
close:SetPoint("TOPRIGHT", -2, -2)
local eb = CreateFrame("EditBox", nil, f, "InputBoxTemplate")
eb:SetAutoFocus(true)
eb:SetSize(380, 24)
eb:SetPoint("CENTER", 0, -2)
eb:SetScript("OnEscapePressed", function() f:Hide() end)
eb:SetScript("OnEnterPressed", function() f:Hide() end)
f.editBox = eb
return f
end
local function _CL_ShowCopyBox(text)
local f = _CL_EnsureCopyBox()
if not f or not f.editBox then return end
f:Show()
f.editBox:SetText(text or "")
-- Slight delay ensures focus isn't stolen by other UI hooks
C_Timer.After(0, function()
if f:IsShown() and f.editBox then
f.editBox:SetFocus()
f.editBox:HighlightText()
end
end)
end
-- Expose for sub-modules (e.g. WowheadLinks)
_G.ClickLinks_ShowCopyBox = _CL_ShowCopyBox
local _AddToJournal -- forward declared (used by URL hooks)
--[[-------------------------------------------------------------------------
ElvUI (and some other UI mods) may replace ItemRefTooltip:SetHyperlink after we
hook it, which can prevent our URL popup from firing. Hooking SetItemRef is
more reliable because it runs when a hyperlink is clicked.
---------------------------------------------------------------------------]]
-- Keep original for non-url links; we will re-capture if another addon replaces SetItemRef.
local _OriginalSetItemRef
-- Capture a base fallback the first time we ever run, before other addons start
-- swapping SetItemRef around.
_G.ClickLinks_OriginalSetItemRef_Base = _G.ClickLinks_OriginalSetItemRef_Base or _G.SetItemRef
-- Wrapper is defined once. We only swap _OriginalSetItemRef as other addons replace SetItemRef.
_G.ClickLinks_SetItemRef_Wrapper = _G.ClickLinks_SetItemRef_Wrapper or function(link, text, button, chatFrame)
-- Prevent infinite recursion if load-order causes _OriginalSetItemRef to point back at us.
if _G.__ClickLinks_InSetItemRef then
local fallback = _G.ClickLinks_OriginalSetItemRef_Base
if fallback and fallback ~= _G.ClickLinks_SetItemRef_Wrapper then
return fallback(link, text, button, chatFrame)
end
return
end
if type(link) == "string" and link:match("^url:") then
local u = link:sub(5):gsub("||", "|")
_AddToJournal(u)
_CL_ShowCopyBox(u)
return
end
local orig = _OriginalSetItemRef or _G.ClickLinks_OriginalSetItemRef_Base
if not orig or orig == _G.ClickLinks_SetItemRef_Wrapper then
orig = _G.ClickLinks_OriginalSetItemRef_Base
end
if not orig then
return
end
_G.__ClickLinks_InSetItemRef = true
local ok, r1, r2, r3, r4 = pcall(orig, link, text, button, chatFrame)
_G.__ClickLinks_InSetItemRef = false
if ok then
return r1, r2, r3, r4
end
end
_G.ClickLinks_EnsureSetItemRefHook = function()
-- On modern clients, avoid overriding the global SetItemRef entirely.
-- Overriding can taint the chat popup menu flow and break protected clipboard actions (Copy Character Name).
if _G.hooksecurefunc then
if _G.__ClickLinks_SetItemRefHooked then
return
end
_G.__ClickLinks_SetItemRefHooked = true
hooksecurefunc("SetItemRef", function(link, text, button, chatFrame)
if type(link) == "string" and link:match("^url:") then
local u = link:sub(5):gsub("||", "|")
_AddToJournal(u)
_CL_ShowCopyBox(u)
end
end)
return
end
-- Legacy fallback (no hooksecurefunc): keep the previous wrapper swap logic.
local current = _G.SetItemRef
if current == _G.ClickLinks_SetItemRef_Wrapper then
return
end
-- Don't allow "original" to ever be our wrapper.
_OriginalSetItemRef = current
if not _OriginalSetItemRef or _OriginalSetItemRef == _G.ClickLinks_SetItemRef_Wrapper then
_OriginalSetItemRef = _G.ClickLinks_OriginalSetItemRef_Base
end
_G.SetItemRef = _G.ClickLinks_SetItemRef_Wrapper
end
-- Call once now, and again after other addons load (eg. ElvUI) to keep our hook active.
_G.ClickLinks_EnsureSetItemRefHook()
-- (Optional) Keep the ItemRefTooltip hook as a secondary path for clients that call it directly.
if ItemRefTooltip then
local OriginalSetHyperlink = ItemRefTooltip.SetHyperlink
function ItemRefTooltip:SetHyperlink(link)
if type(link) == "string" and link:match("^url:") then
local u = link:sub(5):gsub("||", "|")
_AddToJournal(u)
_CL_ShowCopyBox(u)
else
if type(OriginalSetHyperlink) == "function" then
pcall(OriginalSetHyperlink, self, link)
end
return
end
end
end
-------------------------------------------------
-- Automatic Version Check
-------------------------------------------------
local PREFIX = "CLICKLINKS_VER"
-- notes: SavedVariables: stores a one-time warning flag so users don't get spammed repeatedly.
ClickLinksDB = ClickLinksDB or { warned = false }
-- ------------------------------------------------
-- Journal + Minimap Button (Saved clicked links)
-- ------------------------------------------------
-- notes:
-- ClickLinksDB.journal: array of { url = "...", t = time() }
-- ClickLinksDB.minimap: { hide = false, angle = 220 }
-- Use /cl journal (or minimap button) to open the journal.
ClickLinksDB.journal = ClickLinksDB.journal or {}
ClickLinksDB.journalMax = ClickLinksDB.journalMax or 200
ClickLinksDB.minimap = ClickLinksDB.minimap or { hide = false, angle = 220 }
ClickLinksDB.copyBoxPos = ClickLinksDB.copyBoxPos or nil
-- ---- Journal data normalization ----
-- notes:
-- Older test builds (or manual edits) may leave the journal as a sparse table or with non-entry values.
-- Normalize it into a clean array so #journal, table.remove, and UI iteration are safe.
local function _NormalizeJournal()
local j = ClickLinksDB.journal
if type(j) ~= "table" then
ClickLinksDB.journal = {}
return
end
local arr = {}
for _, v in pairs(j) do
if type(v) == "table" then
local url = v.url or v[1]
local t = v.t or v.time or v[2]
if type(url) == "string" and url ~= "" then
table.insert(arr, { url = url, t = tonumber(t) or time() })
end
elseif type(v) == "string" and v ~= "" then
table.insert(arr, { url = v, t = time() })
end
end
table.sort(arr, function(a, b)
return (a.t or 0) < (b.t or 0) -- oldest -> newest
end)
ClickLinksDB.journal = arr
end
_NormalizeJournal()
local function _TrimJournal()
local maxKeep = tonumber(ClickLinksDB.journalMax) or 200
if maxKeep < 10 then maxKeep = 10 end
local j = ClickLinksDB.journal
local extra = #j - maxKeep
if extra > 0 then
for i = 1, extra do
table.remove(j, 1)
end
end
end
local function _ClearJournal()
local j = ClickLinksDB.journal
for i = #j, 1, -1 do
j[i] = nil
end
end
-- Journal UI forward declarations (so journal updates live while the window is open)
local JournalFrame, JournalScrollChild, JournalButtons = nil, nil, nil
local _UpdateJournalUI -- forward declaration (used by _AddToJournal and journal actions)
_AddToJournal = function(url)
url = tostring(url or "")
url = url:gsub("||", "|")
url = url:gsub("%s+$", ""):gsub("^%s+", "")
if url == "" then return end
local j = ClickLinksDB.journal
-- De-duplicate: if URL already exists anywhere, remove old entry(ies) and re-add as newest.
for i = #j, 1, -1 do
local e = j[i]
if e and e.url == url then
table.remove(j, i)
end
end
j[#j + 1] = { url = url, t = time() }
_TrimJournal()
if JournalFrame and JournalFrame:IsShown() then
_UpdateJournalUI()
end
end
-- Expose for sub-modules (e.g. WowheadLinks)
_G.ClickLinks_AddToJournal = _AddToJournal
-- ---- Journal UI ----
-- (JournalFrame locals were forward-declared above so _AddToJournal can refresh the list live)
local function _FormatTime(ts)
if type(date) == "function" and ts then
return date("%Y-%m-%d %H:%M", ts)
end
return ""
end
local function _EnsureJournalUI()
if JournalFrame then return end
local template = (BackdropTemplateMixin and "BackdropTemplate") or nil
JournalFrame = CreateFrame("Frame", "ClickLinks_JournalFrame", UIParent, template)
JournalFrame:SetSize(520, 420)
JournalFrame:SetPoint("CENTER")
JournalFrame:SetFrameStrata("DIALOG")
JournalFrame:EnableMouse(true)
JournalFrame:SetMovable(true)
JournalFrame:RegisterForDrag("LeftButton")
JournalFrame:SetScript("OnDragStart", function(self) self:StartMoving() end)
JournalFrame:SetScript("OnDragStop", function(self) self:StopMovingOrSizing() end)
JournalFrame:Hide()
if JournalFrame.SetBackdrop then
JournalFrame:SetBackdrop({
bgFile = "Interface/Tooltips/UI-Tooltip-Background",
edgeFile = "Interface/Tooltips/UI-Tooltip-Border",
tile = true, tileSize = 16, edgeSize = 16,
insets = { left = 4, right = 4, top = 4, bottom = 4 }
})
JournalFrame:SetBackdropColor(0, 0, 0, 0.95)
end
local title = JournalFrame:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
title:SetPoint("TOP", 0, -10)
title:SetText(L["JOURNAL_TITLE"])
local hint = JournalFrame:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
hint:SetPoint("TOP", title, "BOTTOM", 0, -6)
hint:SetText(L["JOURNAL_HINT"])
local close = CreateFrame("Button", nil, JournalFrame, "UIPanelCloseButton")
close:SetPoint("TOPRIGHT", -2, -2)
local clear = CreateFrame("Button", nil, JournalFrame, "UIPanelButtonTemplate")
clear:SetSize(90, 22)
clear:SetPoint("TOPRIGHT", -38, -28)
clear:SetText(L["CLEAR_ALL"])
clear:SetScript("OnClick", function()
-- Confirm before wiping the entire journal (prevents misclicks)
StaticPopupDialogs["CLICKLINKS_CLEAR_JOURNAL_CONFIRM"] = StaticPopupDialogs["CLICKLINKS_CLEAR_JOURNAL_CONFIRM"] or {
text = L["CLEAR_ALL_CONFIRM"],
button1 = L["OK"],
button2 = L["CANCEL"],
OnAccept = function()
_ClearJournal()
if JournalFrame and JournalFrame:IsShown() and _UpdateJournalUI then
_UpdateJournalUI()
end
end,
timeout = 0,
whileDead = true,
hideOnEscape = true,
preferredIndex = 3,
}
StaticPopup_Show("CLICKLINKS_CLEAR_JOURNAL_CONFIRM")
end)
local scroll = CreateFrame("ScrollFrame", "ClickLinks_JournalScrollFrame", JournalFrame, "UIPanelScrollFrameTemplate")
scroll:SetPoint("TOPLEFT", 16, -58)
scroll:SetPoint("BOTTOMRIGHT", -34, 16)
JournalScrollChild = CreateFrame("Frame", nil, scroll)
JournalScrollChild:SetSize(1, 1)
scroll:SetScrollChild(JournalScrollChild)
JournalButtons = {}
end
_UpdateJournalUI = function()
if not JournalFrame or not JournalFrame:IsShown() then return end
_EnsureJournalUI()
-- Ensure journal is a clean array (protects against old saved data / sparse tables)
_NormalizeJournal()
local j = ClickLinksDB.journal
local rowH = 20
local width = 460
for i = 1, #JournalButtons do
JournalButtons[i]:Hide()
end
local shown = 0
-- Newest-first (journal is oldest->newest, so iterate backwards)
for i = #j, 1, -1 do
local entry = j[i]
if entry and type(entry.url) == "string" and entry.url ~= "" then
shown = shown + 1
local idx = shown -- 1..shown newest first
local btn = JournalButtons[idx]
if not btn then
btn = CreateFrame("Button", nil, JournalScrollChild)
btn:SetHeight(rowH)
btn:SetWidth(width)
btn.text = btn:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
btn.text:SetPoint("LEFT", 2, 0)
btn.text:SetJustifyH("LEFT")
btn.text:SetWidth(width - 4)
btn.text:SetWordWrap(false)
btn:SetHighlightTexture("Interface/QuestFrame/UI-QuestTitleHighlight")
btn:RegisterForClicks("LeftButtonUp", "RightButtonUp")
btn:SetScript("OnClick", function(self, button)
local url = self._url
if not url then return end
if button == "RightButton" then
local idxToRemove = self._journalIndex
local entry = idxToRemove and ClickLinksDB.journal[idxToRemove]
if entry and entry.url == self._url and entry.t == self._ts then
table.remove(ClickLinksDB.journal, idxToRemove)
else
-- fallback for legacy rows/state drift
for k = #ClickLinksDB.journal, 1, -1 do
local e = ClickLinksDB.journal[k]
if e and e.url == self._url and e.t == self._ts then
table.remove(ClickLinksDB.journal, k)
break
end
end
end
_UpdateJournalUI()
else
_CL_ShowCopyBox(url)
end
end)
JournalButtons[idx] = btn
end
local ts = _FormatTime(entry.t)
local display = entry.url or ""
if #display > 300 then display = display:sub(1, 297) .. "..." end
btn._url = entry.url
btn._ts = entry.t
btn._journalIndex = i
btn.text:SetText((ts ~= "" and ("|cffaaaaaa" .. ts .. "|r ") or "") .. "|cff00ccff" .. display .. "|r")
btn:ClearAllPoints()
btn:SetPoint("TOPLEFT", 0, -((idx - 1) * rowH))
btn:Show()
end
end
JournalScrollChild:SetHeight(math.max(1, shown * rowH))
end
local function ToggleJournal()
_EnsureJournalUI()
if JournalFrame:IsShown() then
JournalFrame:Hide()
else
JournalFrame:Show()
_UpdateJournalUI()
end
end
-- ---- Minimap button (LibDBIcon-1.0) ----
-- notes:
-- Uses LibDataBroker + LibDBIcon for a standard, drag-to-move minimap icon.
-- SavedVariables:
-- ClickLinksDB.minimap = { hide = false, minimapPos = 220 }
-- (Older builds used `angle`; we migrate it to `minimapPos` automatically.)
-- Forward declaration:
-- The minimap icon's OnClick closure is created before the localization table is assigned.
local DBIcon = nil
local LDB = nil
local LDBObj = nil
local function _InitMinimap()
if DBIcon and LDBObj then return end
if type(LibStub) ~= "table" or type(LibStub.GetLibrary) ~= "function" then return end
DBIcon = LibStub:GetLibrary("LibDBIcon-1.0", true)
LDB = LibStub:GetLibrary("LibDataBroker-1.1", true)
if not (DBIcon and LDB) then return end
ClickLinksDB.minimap = ClickLinksDB.minimap or { hide = false, minimapPos = 220 }
-- migrate legacy key
if ClickLinksDB.minimap.angle and not ClickLinksDB.minimap.minimapPos then
ClickLinksDB.minimap.minimapPos = ClickLinksDB.minimap.angle
end
ClickLinksDB.minimap.angle = nil
LDBObj = LDB:NewDataObject("ClickLinks", {
label = L["ADDON_TITLE"],
text = L["ADDON_TITLE"],
type = "launcher",
icon = "Interface/ICONS/INV_Misc_Note_04",
OnClick = function(_, button)
if button == "LeftButton" then
ToggleJournal()
end
end,
OnTooltipShow = function(tt)
tt:AddLine(L["ADDON_TITLE"], 0.08, 0.63, 0.85)
tt:AddLine(L["TOOLTIP_LEFTCLICK_JOURNAL"], 1, 1, 1)
tt:AddLine(L["TOOLTIP_DRAG_MOVE"], 1, 1, 1)
end,
})
DBIcon:Register("ClickLinks", LDBObj, ClickLinksDB.minimap)
if ClickLinksDB.minimap.hide then
DBIcon:Hide("ClickLinks")
end
end
local function ToggleMinimapButton()
ClickLinksDB.minimap = ClickLinksDB.minimap or { hide = false, minimapPos = 220 }
ClickLinksDB.minimap.hide = not ClickLinksDB.minimap.hide
_InitMinimap()
if DBIcon then
if ClickLinksDB.minimap.hide then
DBIcon:Hide("ClickLinks")
else
DBIcon:Show("ClickLinks")
end
end
end
-- notes: Localization-ready strings (single table).
-- notes: Reads the addon Version field from the TOC metadata.
local localVersion = ( (C_AddOns and C_AddOns.GetAddOnMetadata) or GetAddOnMetadata )(ADDON_NAME, "Version")
or ( (C_AddOns and C_AddOns.GetAddOnMetadata) or GetAddOnMetadata )("ClickLinks", "Version")
or "0"
localVersion = tostring(localVersion)
local function VersionToNumber(ver)
-- notes: Converts semantic X.Y.Z into an integer so versions can be compared numerically.
-- notes: Example: 1.2.3 -> 10203 (via a*10000 + b*100 + c)
ver = tostring(ver or "")
local a, b, c = ver:match("(%d+)%.(%d+)%.(%d+)")
if not a then
a, b = ver:match("(%d+)%.(%d+)")
c = 0
end
if not a then return 0 end
return tonumber(a) * 10000 + tonumber(b) * 100 + tonumber(c)
end
local localVerNum = VersionToNumber(localVersion)
local _CL_RegisterAddonMessagePrefix = (C_ChatInfo and C_ChatInfo.RegisterAddonMessagePrefix) or RegisterAddonMessagePrefix