q3fuba

Wowhead Link Grabber - fixed for 8.x

Jul 31st, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.28 KB | None | 0 0
  1. local tooltipInfo = {}
  2. local customframes;
  3. local wowheadLink = "wowhead.com/"
  4. local hash = ""
  5. local frame = CreateFrame("Frame")
  6. local gotoLink = ""
  7. local QuestMapFrame_IsQuestWorldQuest = QuestMapFrame_IsQuestWorldQuest or QuestUtils_IsQuestWorldQuest
  8.  
  9. local validfounds = {
  10.   npc = "",
  11.   spell = GetSpellInfo,
  12.   achievement = function(id)
  13.     return select(2,GetAchievementInfo(id))
  14.   end,
  15.   quest = function(id)
  16.     for i=1,GetNumQuestLogEntries() do
  17.       local name,_,_,_,_,_,_,qid = GetQuestLogTitle(i)
  18.       if id == qid then
  19.         return name
  20.       end
  21.     end
  22.     return "Quest";
  23.   end,
  24.   item = GetItemInfo
  25. }
  26.  
  27. local function clearTooltipInfo(tooltip)
  28.   wipe(tooltipInfo[tooltip])
  29. end
  30.  
  31. local function setTooltipHyperkink(tooltip, hyperlink)
  32.   local ttable = tooltipInfo[tooltip];
  33.   ttable.hl = hyperlink;
  34. end
  35.  
  36. local function setTooltipAura(tooltip, unit, index, filter)
  37.   local ttable = tooltipInfo[tooltip];
  38.   local name = UnitAura(unit, index, filter);
  39.   local id = select(10, UnitAura(unit, index, filter));
  40.   --print("Name: "..name.." = ID: "..id)
  41.   ttable.aura = id
  42.   ttable.name = name
  43. end
  44.  
  45. local function hookTooltip(tooltip)
  46.   tooltipInfo[tooltip] = {}
  47.   hooksecurefunc(tooltip, "SetHyperlink", setTooltipHyperkink)
  48.   hooksecurefunc(tooltip, "SetUnitAura", setTooltipAura)
  49.   tooltip:HookScript("OnTooltipCleared", clearTooltipInfo)
  50. end
  51.  
  52. local function onEvent(frame, event)
  53.   if event == "PLAYER_ENTERING_WORLD" then
  54.     hookTooltip(GameTooltip)
  55.     hookTooltip(ItemRefTooltip)
  56.   end
  57. end
  58.  
  59. local function onUpdate()
  60.   StaticPopup_Show("WOWHEAD_LINK_GRABBER")
  61.   frame:Hide();
  62. end
  63.  
  64. local function found(ftype, id, name)
  65.   local foundAccept = validfounds[ftype];
  66.   if foundAccept then
  67.     name = name or foundAccept;
  68.     if type(name) == 'function' then
  69.       name = name(id);
  70.     end
  71.     name = name or ftype;
  72.     --print("Found "..type.." "..id)
  73.     -- Show frame to recieve OnUpdate next frame
  74.     -- So pressed hotkey doesnt erase text field
  75.     gotoLink = "http://" .. wowheadLink .. ftype .. "=" .. id .. hash;
  76.     local upper_name = firstToUpper(ftype)
  77.     if QuestMapFrame_IsQuestWorldQuest(id) then
  78.       upper_name = firstToUpper("World Quest");
  79.     end
  80.     StaticPopupDialogs["WOWHEAD_LINK_GRABBER"].text = "|cffffff00"..upper_name .. ":\n|r" .. name .. "\n\n|cff00ff00CTRL+C to copy!|r";
  81.     frame:Show();
  82.     return true;
  83.   end
  84. end
  85.  
  86. local function foundplayer(name)
  87.   return true;
  88. end
  89.  
  90. local function getUnitInfo(unit, name)
  91.   if UnitIsPlayer(unit) then
  92.     return foundplayer(name)
  93.   else
  94.     local GUID=UnitGUID(unit)
  95.     local type,_,_,_,_,id = strsplit("-",GUID);
  96.     if type == "Creature" then return found("npc",id,name) end
  97.   end
  98. end
  99.  
  100. local function getFocusInfo()
  101.   local focus = GetMouseFocus()
  102.   local current = focus;
  103.   local focusname;
  104.   --__LASTFRAME = focus;
  105.   while current and not focusname do
  106.  
  107.     -- added by fuba82 for World Quest support
  108.     if WorldMapFrame and WorldMapFrame:IsVisible() and current and current.questID and QuestMapFrame_IsQuestWorldQuest(current.questID) then
  109.       return found("quest", current.questID, select(4, GetTaskInfo(current.questID)))
  110.     end
  111.  
  112.     focusname = current:GetName()
  113.     current = current:GetParent()
  114.   end
  115.   if not focusname then return end
  116.   local focuslen = string.len(focusname);
  117.   --print(focusname);
  118.   for name,func in pairs(customframes) do
  119.     local customlen = string.len(name)
  120.     if customlen <= focuslen and name == string.sub(focusname,1,customlen) then
  121.       if func(focus, focusname) then return true end
  122.     end
  123.   end
  124. end
  125.  
  126. local function parseLink(link)
  127.   local linkstart = string.find(link,"|H")
  128.   local _,lastfound,type,id = string.find(link,"(%a+):(%d+):",linkstart and linkstart + 2)
  129.   local _,_,name = string.find(link,"%[([^%[%]]*)%]",lastfound)
  130.   return found(type,id,name)
  131. end
  132.  
  133. local function parseTooltip(tooltip)
  134.   local name, link = tooltip:GetItem()
  135.   if name then return parseLink(link) end
  136.   --local name, _, id = tooltip:GetSpell();
  137.   local name, id = tooltip:GetSpell();
  138.   if name then return found("spell",id,name) end
  139.   local name, unit = tooltip:GetUnit()
  140.   if unit then return getUnitInfo(unit, name) end
  141.   local ttdata = tooltipInfo[tooltip];
  142.   if ttdata.hl then return parseLink(ttdata.hl) end
  143.   if ttdata.aura then return found("spell",ttdata.aura,ttdata.name) end
  144. end
  145.  
  146. local function linkGrabberRunInternal()
  147.   return parseTooltip(ItemRefTooltip)
  148.     or parseTooltip(GameTooltip)
  149.     or getFocusInfo()
  150. end
  151.  
  152. linkGrabberRun = function()
  153.   linkGrabberRunInternal()
  154.   --pcall(linkGrabberRunInternal);
  155. end
  156.  
  157. -- Formatting
  158. function firstToUpper(str)
  159.   return (str:gsub("^%l", string.upper))
  160. end
  161.  
  162. -- Custom frames mouseover
  163.  
  164. local function AchievmentWidget(widget)
  165.   return found("achievement",widget.id);
  166. end
  167.  
  168. local function AchievmentWidgetParent(widget)
  169.   return AchievmentWidget(widget:GetParent())
  170. end
  171.  
  172. local function QuestWidget(widget)
  173.   if widget.questID then return found("quest",widget.questID) end
  174. end
  175.  
  176. local function TrackWidget(widget)
  177.   local parent = widget:GetParent()
  178.   local module = parent.module;
  179.   if module == QUEST_TRACKER_MODULE then
  180.     return found("quest", parent.id)
  181.   elseif module == ACHIEVEMENT_TRACKER_MODULE then
  182.     return found("achievement", parent.id)
  183.   end
  184. end
  185.  
  186. -- added by fuba82 for World Quest support
  187. local function TrackWorldQuestWidget(widget)
  188.   local module = widget.module;
  189.   if module == WORLD_QUEST_TRACKER_MODULE then
  190.     if widget.id then
  191.       return found("quest", widget.id, select(4, GetTaskInfo(widget.id)))
  192.     end
  193.   elseif widget.questID then -- World Quest Tracker support
  194.     if QuestMapFrame_IsQuestWorldQuest(widget.questID) then
  195.       return found("quest", widget.questID, select(4, GetTaskInfo(widget.questID)))
  196.   end
  197.   end
  198. end
  199.  
  200. customframes = {
  201.   ["AchievementFrameCriteria"] = AchievmentWidgetParent,
  202.   ["AchievementFrameSummaryAchievement"] = AchievmentWidget,
  203.   ["AchievementFrameAchievementsContainerButton"] = AchievmentWidget,
  204.   ["QuestScrollFrame"] = QuestWidget,
  205.   ["ObjectiveTrackerBlocksFrameHeader"] = TrackWidget,
  206.   -- added by fuba82 for World Quest support
  207.   ["ObjectiveTrackerBlocksFrame"] = TrackWorldQuestWidget,
  208.   ["WorldMapFrameTaskPOI"] = WolrdQuestWidget,
  209.   ["WorldQuestTrackerZonePOIWidget"] = WolrdQuestWidget, -- World Quest Tracker support
  210.   ["WorldQuestTracker_Tracker"] = TrackWorldQuestWidget -- World Quest Tracker support
  211. }
  212.  
  213. frame:Hide()
  214. frame:RegisterEvent("PLAYER_ENTERING_WORLD")
  215. frame:SetScript("OnEvent", onEvent)
  216. frame:SetScript("OnUpdate", onUpdate)
  217.  
  218. local locale = string.sub(GetLocale(),1,2)
  219. if locale ~= "en" then
  220.   wowheadLink = locale.."."..wowheadLink
  221.   hash = "#english-comments"
  222. end
  223.  
  224. BINDING_HEADER_LINK_GRABBER_HEAD = "Wowhead Link Grabber"
  225. BINDING_DESCRIPTION_LINK_GRABBER_DESC = "Hotkey for displaying wowhead link"
  226. BINDING_NAME_LINK_GRABBER_NAME = "Generate Link"
  227.  
  228. StaticPopupDialogs["WOWHEAD_LINK_GRABBER"] = {
  229.   OnShow = function (self, data)
  230.     self.editBox:SetText(gotoLink)
  231.     self.editBox:HighlightText()
  232.   end,
  233.   EditBoxOnEscapePressed = function(self)
  234.     self:GetParent():Hide();
  235.   end,
  236.   EditBoxOnTextChanged = function(self)
  237.     self:SetText(gotoLink)
  238.     self:HighlightText()
  239.   end,
  240.   button1 = OKAY,
  241.   editBoxWidth = 350,
  242.   hasEditBox=true,
  243.   preferredIndex = 3
  244. }
Advertisement
Add Comment
Please, Sign In to add comment