Sign in to follow this  
Followers 0
Jdurant

Getting Rid Of Pop-up Windows

3 posts in this topic

I was wondering on another problem I've seen.

While running my script on a certain webpage I saw that with a certain number of hits the page will activate a pop-up webpage. (it's hard for me to count the pages because of the other hits that might happen)

What would be a good code to make it an "if" command to see the pop-up and close it but otherwise continue if it doesn't pop-up.

Thanks again

-Jeremy

Share this post


Link to post
Share on other sites



I use the Mozilla web browser, and it has built-in pop-up blockers. ...But if you're like me, you'll want to know how to do it via AutoIt anyway. :whistle:

Back around 2002, Larry created an excellent example of a pop-up blocking script using v2. He probably has a more current version of the script, but here's the copy that I snagged from one of his original posts:

SetBatchLines,150
SetWinDelay,1
SetTitleMatchMode,2

IfEqual,1,/secret1,Goto,Watch
Run,%A_SCRIPTFULLPATH% /secret1
SetEnv,POST,- Microsoft Internet Explorer
SetEnv,NEWPOST,- PUPKill
SetEnv,MAIN,JustStartingThisThing
IfEqual,1,/all,Goto,JustKill

SetEnv,ARRAY,0
IfExist,%A_SCRIPTDIR%killfile.pup,SetEnv,ARRAY,1
IfExist,%A_SCRIPTDIR%killfile.pup,Gosub,LoadKillFile
Gosub,LoadKillFile

Start:
   WinWaitActive,%POST%
   WinGetActiveTitle,result
   StringReplace,MAIN,result,%POST%,%NEWPOST%
   WinSetTitle,%result%,,%MAIN%

Main:
   IfWinNotExist,%NEWPOST%,,Goto,Start
   Sleep,100
   WinGetActiveTitle,result
   IfEqual,result,%MAIN%,Goto,Main
   TitleLoop:
   WinGetActiveTitle,result      
      IfInString,result,%NEWPOST%,SetEnv,MAIN,%result%
      IfInString,result,%NEWPOST%,Goto,Main
      IfInString,result,%POST%,IfWinNotExist,%MAIN%,,Goto,MakeMeMain
      IfInString,result,%POST%,IfWinExist,%MAIN%,,Goto,POPcheck
      Wait4POST:
      WinWait,%POST%
      IfWinNotActive,%POST%,,WinActivate,%POST%
      WinWaitActive,%POST%,,1
      Goto,TitleLoop

POPcheck:
   SetEnv,Kill,0
   StringTrimRight,check,result,30
   IfEqual,ARRAY,0,Goto,NEWpop
   SetEnv,count,1
   POPloop:
      IfGreater,count,%ARRAY%,Goto,NEWpop
      StringTrimRight,result,KILLME%count%,0
      IfEqual,check,%result%,SetEnv,Kill,1
      IfEqual,Kill,1,WinClose,%result%
      IfEqual,Kill,1,Goto,Wait4Post
      EnvAdd,count,1
   Goto,POPloop

NEWpop:
   MsgBox,4100,PopUP Killer,A NEW WINDOW POPPED UP\n\n%check%\n\nAdd it to the PopUp Window Kill List?
   IfMsgBox,YES,Goto,KillTheBastard
   WinSetTitle,%check% %POST%,,%check% %NEWPOST%
   Goto,TitleLoop
KillTheBastard:
   EnvAdd,ARRAY,1
   SetEnv,KILLME%ARRAY%,%check%
   FileAppend,%check%\n,%A_SCRIPTDIR%killfile.pup
   WinClose,%check%
   Goto,TitleLoop

MakeMeMain:
   StringReplace,MAIN,result,%POST%,%NEWPOST%
   WinSetTitle,%result%,,%MAIN%
   Goto,Main

LoadKillFile:
   FileReadLine,KILLME%ARRAY%,%A_SCRIPTDIR%killfile.pup,%ARRAY%
   IfEqual,errorlevel,1,Return
   EnvAdd,ARRAY,1
   Goto,LoadKillFile

NewKillFile:
   MsgBox,4096,PopUp Killer,PopUp Window Kill List does not exist!\n\nCREATE IT?  (NO will EXIT)
   IfMsgBox,NO,Exit
   RunWait,%comspec% /c type nul>killfile.pup,%A_SCRIPTDIR%,hide
   Return

JustKill:
   SetTitleMatchMode,2
   IfEqual,MAIN,JustStartingThisThing,Gosub,GetMain
   SetEnv,POP,0
   WinWait,%POST%
   IfWinNotActive,%POST%,,WinActivate,%POST%
   WinWaitActive,%POST%,,1
   IfEqual,errorlevel,1,Goto,JustKill
   WinGetActiveTitle,result
   IfNotInString,result,%POST%,Goto,JustKill
   SetTitleMatchMode,1
   IfWinExist,%MAIN%,,SetEnv,POP,1
   IfEqual,POP,1,WinClose,%result%
   IfEqual,POP,1,Goto,JustKill
   StringReplace,MAIN,result,%POST%,- PUPKill STRICT
   WinSetTitle,%result%,,%MAIN%
   Goto,JustKill

GetMain:
   WinWaitActive,%POST%
   WinGetActiveTitle,MAIN
   StringReplace,MAIN,MAIN,%POST%,- PUPKill STRICT
   WinSetTitle,%POST%,,%MAIN%
   Return

Watch:
   HideAutoItWin,on
   DetectHiddenText,on
   WinSetTitle,Jonathan Bennett,,PUPKill v1.1
   WinSetTitle,Compiled AutoIt Script,,PUPKill v1.1
   WinWait,PUPKill v1.1
   WinWaitClose,PUPKill v1.1
   IfWinNotExist,- PUPKill,,Exit
   TitleBack:
   WinActivate,- PUPKill
   WinWaitActive,- PUPKill,,3
   WinGetActiveTitle,result
   SetEnv,new,
   IfInString,result,- PUPKill STRICT,StringReplace,new,result,- PUPKill STRICT,- Microsoft Internet Explorer
   IfEqual,new,,IfInString,result,- PUPKill,StringReplace,new,result,- PUPKill,- Microsoft Internet Explorer
   WinSetTitle,%result%,,%new%
   IfWinExist,- PUPKill,,Goto,TitleBack
   Exit

Again, this is NOT my code. Larry was the original author, and deserves full credit.

Also, I seem to remember him stating that it would be best to make sure all browser windows were closed before starting this script.

Share this post


Link to post
Share on other sites

Thank you very much,

I'll put this in my code and see how it will act with the other parts of my code.

thanks

-Jer

Share this post


Link to post
Share on other sites
Sign in to follow this  
Followers 0