Jump to content
Sign in to follow this  
BitByteBit

Check if Facebook is open.

Recommended Posts

BitByteBit

I'm writing an application to monitor the amount of time spent on Facebook.

When you are viewing someones profile the string "Facebook" is not found in the window title, because of this I decided to use PixelSearch in addition to checking the active window's title.

The following function checks if Facebook is open, it works okay; however if the pixel I'm searching for on the screen is present in another window then it will stop searching and return false. In theory it should continue searching the rest of the screen from the last location, aaannd this is where I'm stuck.

Func _OnFacebook()
If WinActive('Facebook') Then Return True
Local $iX = 0, $iY = 0
While 1
  $aLoc1 = PixelSearch($iX, $iY, @DesktopWidth, @DesktopHeight, 0x133783)
  If Not @error Then
   If PixelGetColor($aLoc1[0] + 20, $aLoc1[1]) = 0x133783 Then
    Return True
   Else
    ConsoleWrite('Found pixel, but not Facebook!' & @CRLF)
    $iX = $aLoc1[0] + 1
    $iY = $aLoc1[1]
   EndIf
  Else
   Return False
  EndIf
WEnd
EndFunc   ;==>_OnFacebook

Here is the full code.

Opt('GuiOnEventMode', 1)
Opt('TrayAutoPause', 0)
Opt('TrayOnEventMode', 1)
Opt('TrayMenuMode', 1)
Global $iTotal = 0, $iLimit = 3600000, $aLoc1, $bHidden = False
Global $iHours, $iMins, $iSecs
$hGUI = GUICreate('Anti Facebook', 70, 90, -1, -1, 0x00400000)
GUISetOnEvent(-3, '_Exit')
GUISetBkColor(0x1F1F1F)
$hLabel = GUICtrlCreateLabel('Total time:', 25, 10)
GUICtrlSetColor(-1, 0xFFFFFF)
$hLabel2 = GUICtrlCreateLabel('0:00', 80, 10)
GUICtrlSetColor(-1, 0xFFFFFF)
GUICtrlCreateButton('Reset', 24, 32, 78)
GUICtrlSetOnEvent(-1, '_Reset')
GUISetState()
$iCurrent = @MDAY
TraySetClick(8)
TraySetOnEvent(-7, '_ToggleGUI')
TrayCreateItem('Exit')
TrayItemSetOnEvent(-1, '_Exit')
While 1
If $iCurrent <> @MDAY Then $iTotal = 0
If _OnFacebook() Then
  If $iTotal >= $iLimit Then _CloseFacebook()
  $T1 = TimerInit()
  While _OnFacebook()
   $iTicks = Mod(Int((TimerDiff($T1) + $iTotal) / 1000), 3600)
   $iSecs = Mod($iTicks, 60)
   If $iSecs < 10 Then $iSecs = '0' & $iSecs
   GUICtrlSetData($hLabel2, Int($iTicks / 60) & ':' & $iSecs)
   If $iTotal + TimerDiff($T1) >= $iLimit Then _CloseFacebook()
   Sleep(990)
  WEnd
  $iTotal += TimerDiff($T1)
EndIf
Sleep(1000)
WEnd
Func _Exit()
Exit
EndFunc   ;==>_Exit
Func _OnFacebook()
If WinActive('Facebook') Then Return True
Local $iX = 0, $iY = 0
While 1
  $aLoc1 = PixelSearch($iX, $iY, @DesktopWidth, @DesktopHeight, 0x133783)
  If Not @error Then
   If PixelGetColor($aLoc1[0] + 20, $aLoc1[1]) = 0x133783 Then
    Return True
   Else
    $iX = $aLoc1[0] + 1
    $iY = $aLoc1[1]
   EndIf
  Else
   Return False
  EndIf
WEnd
EndFunc   ;==>_OnFacebook
Func _CloseFacebook()
If WinActive('Facebook') Then
  Send('^w')
ElseIf IsArray($aLoc1) Then
  $aOld = MouseGetPos()
  MouseClick('Primary', $aLoc1[0], $aLoc1[1], 1, 0)
  MouseMove($aOld[0], $aOld[1], 0)
  Send('^w')
EndIf
MsgBox(48, 'Limit Exceeded!', 'You have been on Facebook too long!' & @CRLF & 'Go outside and play!')
EndFunc   ;==>_CloseFacebook
Func _ToggleGUI()
$bHidden = $bHidden = False
If $bHidden Then
  WinSetState($hGUI, '', @SW_HIDE)
Else
  WinSetState($hGUI, '', @SW_SHOW)
  WinActivate($hGUI)
EndIf
EndFunc   ;==>_ToggleGUI
Func _Reset()
GUICtrlSetData($hLabel2, '0:00')
$iTotal = 0
$T1 = TimerInit()
EndFunc   ;==>_Reset

Thank you for your time.

Share this post


Link to post
Share on other sites
Xenobiologist

If you want to control somebody else, why not checking the proxy with IP?

Besides I'm behind a proxy and if I use (in a cmd) netstat -a I see the connections.

I don't know whether you can use nslookup and netstat -a to achieve your goal.

Edited by Xenobiologist

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites
BitByteBit

Thanks for your reply, perhaps a little more background information would be appropriate.

One day, I thought about the amount of time I spend on Facebook, I then thought about the amount of time my 10 year old nephew spends on it. As a joke I thought about making an application that limits the amount of time spent on Facebook.

A week later, late at night in a fit of boredom I started making this program, I viewed it as a challenge and wanted to see how fast I could do it.

Unfortunately I've got stuck at a hurdle, whilst the program works in most situations; knowing it's flawed really bugs me.

Whilst your method would work to see how long the user is connected to Facebook, that is not my goal. My goal is to time how long Facebook is active or visible, if the user minimizes the window the the timer stops.

Share this post


Link to post
Share on other sites
Xenobiologist

Ah okay. It is no control tool. It is just a measurement.

What using/trying that already exists?

http://mashable.com/2008/02/23/firefox-monitor-time-online/

Otherwise, you need to specify a lot more at least which browser you use.

Because, there are many ways open for cheating :-)

Edit: netstat -f shows a connection to facebook.com

Edited by Xenobiologist

Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×