Skip to content

Commit

Permalink
support v4 (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
blurfx authored Apr 19, 2024
1 parent 8fe8c3c commit 8efe02d
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 109 deletions.
6 changes: 6 additions & 0 deletions internal/ad.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,16 @@ func HidePopupAd() {
}

func HideMainWindowAd(windowClass string, handle windows.HWND) {
// @deprecated
if windowClass == "BannerAdWnd" {
winapi.ShowWindow(handle, 0)
winapi.SetWindowPos(handle, 0, 0, 0, 0, 0, winapi.SwpNomove)
}
if windowClass == "BannerAdContainer" {
parentHandle := winapi.GetParent(handle)
winapi.ShowWindow(parentHandle, 0)
winapi.SetWindowPos(parentHandle, 0, 0, 0, 0, 0, winapi.SwpNomove)
}
}

func HideLockScreenAdArea(windowText string, rect *winapi.Rect, handle windows.HWND) {
Expand Down
19 changes: 19 additions & 0 deletions internal/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,32 @@ func removeAd() {

rect := new(winapi.Rect)
winapi.GetWindowRect(wnd, rect)
var mainWindowParentHandle windows.HWND
var candidates [][]windows.HWND
for _, childHandle := range childHandles {
className := winapi.GetClassName(childHandle)
windowText := winapi.GetWindowText(childHandle)
parentHandle := winapi.GetParent(childHandle)
if className == "EVA_ChildWindow" {
if windowText == "" {
candidates = append(candidates, []windows.HWND{childHandle, parentHandle})
} else if strings.HasPrefix(windowText, "OnlineMainView") {
mainWindowParentHandle = parentHandle
}
}
HideMainWindowAd(className, childHandle)
HideMainViewAdArea(windowText, rect, childHandle)
HideLockScreenAdArea(windowText, rect, childHandle)
}
if mainWindowParentHandle != 0 && len(candidates) > 0 {
for _, candidate := range candidates {
if candidate[1] == mainWindowParentHandle {
winapi.ShowWindow(candidate[0], 0)
winapi.SetWindowPos(candidate[0], 0, 0, 0, 0, 0, winapi.SwpNomove)
break
}
}
}
}
HidePopupAd()
mutex.Unlock()
Expand Down
218 changes: 109 additions & 109 deletions internal/win/tray_icon.go
Original file line number Diff line number Diff line change
@@ -1,109 +1,109 @@
package win

import (
"unsafe"

"golang.org/x/sys/windows"

"kakaotalkadblock/internal/win/winapi"
)

var quit *chan struct{}

func wndProc(hWnd uintptr, msg uint32, wParam, lParam uintptr) uintptr {
switch msg {
case winapi.WmTrayicon:
switch uint16(lParam) {
case winapi.WmLbuttondblclk:
close(*quit)
}
case winapi.WmDestroy:
winapi.PostQuitMessage(0)
default:
return winapi.DefWindowProc(hWnd, msg, wParam, lParam)
}
return 0
}

func createMainWindow() (uintptr, error) {
hInstance, err := winapi.GetModuleHandle(nil)
if err != nil {
return 0, err
}

wndClass, _ := windows.UTF16PtrFromString("KakaoTalkAdBlock")

var windowClass winapi.WindowClassEx

windowClass.CbSize = uint32(unsafe.Sizeof(windowClass))
windowClass.LpfnWndProc = windows.NewCallback(wndProc)
windowClass.HInstance = hInstance
windowClass.LpszClassName = wndClass
if _, err := winapi.RegisterClassEx(&windowClass); err != nil {
return 0, err
}

handle, err := winapi.CreateWindowEx(
0,
wndClass,
windows.StringToUTF16Ptr("KakaoTalkAdBlock"),
winapi.WsOverlappedwindow,
winapi.CwUsedefault,
winapi.CwUsedefault,
1,
1,
0,
0,
hInstance,
nil)
if err != nil {
return 0, err
}

return handle, nil
}

type TrayIcon struct {
notifyIconData winapi.NotifyIconData
}

func NewTrayIcon(quitChan *chan struct{}) *TrayIcon {
var data winapi.NotifyIconData
data.CbSize = uint32(unsafe.Sizeof(data))
data.UFlags = winapi.NifIcon | winapi.NifMessage | winapi.NifInfo
data.UCallbackMessage = winapi.WmTrayicon

hInst, err := winapi.GetModuleHandle(nil)
if err != nil {
panic(err)
}
icon, err := winapi.LoadIcon(hInst, winapi.MakeIntResource(1))
if err != nil {
panic(err)
}
data.HIcon = icon

quit = quitChan
return &TrayIcon{
notifyIconData: data,
}
}

func (t *TrayIcon) Show() {
if t.notifyIconData.HWnd == 0 {
handle, err := createMainWindow()
if err != nil {
panic(err)
}
t.notifyIconData.HWnd = handle
}
if err := winapi.ShellNotifyIcon(winapi.NimAdd, &t.notifyIconData); err != nil {
panic(err)
}
}

func (t *TrayIcon) Hide() {
if err := winapi.ShellNotifyIcon(winapi.NimDelete, &t.notifyIconData); err != nil {
panic(err)
}
}
package win

import (
"unsafe"

"golang.org/x/sys/windows"

"kakaotalkadblock/internal/win/winapi"
)

var quit *chan struct{}

func wndProc(hWnd uintptr, msg uint32, wParam, lParam uintptr) uintptr {
switch msg {
case winapi.WmTrayicon:
switch uint16(lParam) {
case winapi.WmLbuttondblclk:
*quit <- struct{}{}
}
case winapi.WmDestroy:
winapi.PostQuitMessage(0)
default:
return winapi.DefWindowProc(hWnd, msg, wParam, lParam)
}
return 0
}

func createMainWindow() (uintptr, error) {
hInstance, err := winapi.GetModuleHandle(nil)
if err != nil {
return 0, err
}

wndClass, _ := windows.UTF16PtrFromString("KakaoTalkAdBlock")

var windowClass winapi.WindowClassEx

windowClass.CbSize = uint32(unsafe.Sizeof(windowClass))
windowClass.LpfnWndProc = windows.NewCallback(wndProc)
windowClass.HInstance = hInstance
windowClass.LpszClassName = wndClass
if _, err := winapi.RegisterClassEx(&windowClass); err != nil {
return 0, err
}

handle, err := winapi.CreateWindowEx(
0,
wndClass,
windows.StringToUTF16Ptr("KakaoTalkAdBlock"),
winapi.WsOverlappedwindow,
winapi.CwUsedefault,
winapi.CwUsedefault,
1,
1,
0,
0,
hInstance,
nil)
if err != nil {
return 0, err
}

return handle, nil
}

type TrayIcon struct {
notifyIconData winapi.NotifyIconData
}

func NewTrayIcon(quitChan *chan struct{}) *TrayIcon {
var data winapi.NotifyIconData
data.CbSize = uint32(unsafe.Sizeof(data))
data.UFlags = winapi.NifIcon | winapi.NifMessage | winapi.NifInfo
data.UCallbackMessage = winapi.WmTrayicon

hInst, err := winapi.GetModuleHandle(nil)
if err != nil {
panic(err)
}
icon, err := winapi.LoadIcon(hInst, winapi.MakeIntResource(1))
if err != nil {
panic(err)
}
data.HIcon = icon

quit = quitChan
return &TrayIcon{
notifyIconData: data,
}
}

func (t *TrayIcon) Show() {
if t.notifyIconData.HWnd == 0 {
handle, err := createMainWindow()
if err != nil {
panic(err)
}
t.notifyIconData.HWnd = handle
}
if err := winapi.ShellNotifyIcon(winapi.NimAdd, &t.notifyIconData); err != nil {
panic(err)
}
}

func (t *TrayIcon) Hide() {
if err := winapi.ShellNotifyIcon(winapi.NimDelete, &t.notifyIconData); err != nil {
panic(err)
}
}

0 comments on commit 8efe02d

Please sign in to comment.