10/07/08

AdiumScript: Firefox 3 Current Page

AdiumScript

Comme en témoigne le Bug 427448, on ne peut plus exploiter la «class pTit» des fenêtres de Firefox 3[1] avec AppleScript, puisque celle-ci retourne une valeur vide[2]. Aussi, c'est pourquoi des AdiumScripts, comme Firefox's Current Page ne peuvent plus fonctionner correctement.

Comme le support d'AppleScript dans Firefox 3 semble loin d'être une priorité[3], je me suis décidé à développer un nouvel AdiumScript, pour Adium, aux fonctionnalités similaires à celles de Firefox's Current Page mais avec un code largement différent, s'appuyant sur curl[4] pour récupérer le titre de la page si la «class pTit» est vide.

Firefox3CurrentPage.scpt

(*
	Firefox3CurrentPage.AdiumScripts
	Harry-Proton <http://blogosx.jupiterii.com>
 
	updated July 11, 2008
*)
 
on substitute()
	if (runProcesses("firefox-bin") and ((count of windows of application "Minefield") > 0)) then
		tell application "Minefield" to set firefoxProperties to (properties of window 1)
		set cURL to («class curl» of firefoxProperties) as string
		set pTit to («class pTit» of firefoxProperties) as string
		
		-- if Firefox return no «class curl» (Bug 427448)
		if (isEmpty(pTit)) then
			set UA to "" as string
			-- user_agent is based on version of Adium
			if (runProcesses("adium")) then
				set adiumProperties to (properties of application "Adium")
				set UA to (name of adiumProperties & "/" & version of adiumProperties) as string
			end if
			
			-- run curl command line
			set pTit to curlTitle(cURL, UA)
			if (isEmpty(pTit)) then set pTit to cURL
		end if
		
		if (isEmpty(cURL)) then return ""
		return ("<HTML><a href=\"" & cURL & "\">" & pTit & "</a></HTML>") as string
	else
		return "Firefox is not running"
	end if
end substitute
 
on runProcesses(processName)
	tell application "System Events"
		set n to ((application processes whose (name is equal to processName)) count) as small integer
	end tell
	
	if (n is greater than 0) then return true
	return false
end runProcesses
 
on curlTitle(cURL, UA)
	if (isEmpty(UA)) then
		set UA to "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; fr; rv:1.8.1.6) " & ¬
			"Gecko/20070725 Firefox/2.0.0.6" as string
	end if
	
	set pTit to (do shell script ("url=" & quoted form of cURL & "; " & ¬
		"user_agent=" & quoted form of UA & "; " & ¬
		"curl --silent -A \"$user_agent\" --dump-header '/tmp/AdiumCurlCurrentPage' \"$url\" | grep -E '<title>' " & ¬
		"| awk -F \"<title>\" '{print $2}' | awk -F \"</title>\" '{print $1}'")) as string
	
	set charset to (do shell script ("headers='/tmp/AdiumCurlCurrentPage'; " & ¬
		"cat \"$headers\" | grep 'charset=' | awk -F \"charset=\" '{print $2}'")) as string
	
	ignoring case
		if (charset is not "UTF-8") then
			try
				set pTit to (do shell script ("title='" & pTit & "'; " & ¬
					"charset='" & charset & "'; " & ¬
					"echo \"${title}\" | iconv -f  UTF-8 -t MACROMAN | iconv -f ${charset} -t UTF-8")) as string
			on error
				set pTit to cURL
			end try
		end if
	end ignoring
	
	set text item delimiters to (return as string)
	set pTit to (item 1 of text items of pTit as list) as string
	
	return pTit
end curlTitle
 
on isEmpty(str)
	if ((str is "") or ((count of items of str) is 0) ¬
		or (str is missing value) or (str is (missing value) as text)) then
		return true
	end if
	return false
end isEmpty
 
on run
	substitute()
end run

Firefox 3 Current Page (suspendu)

La mise à disposition de ce script à été suspendue suite au développement et à la réalisation de scripts plus efficaces. Veuillez effectuer une recherche sur ce blog pour les trouver.

#1 biglittledragoon écrivait :

11/07/08

Vraiment pas bête! Puisque ça le fait pas avec Firefox, pourquoi pas passer par curl ?

Et justement ça marche ! :happy:

Juste quelques petits soucis avec les titres non ASCII et certaines pages qui renvoient une url, même rien comparé aux bugs des autres solutions :wink:

#2 Harry-Proton écrivait :

11/07/08

Normalement les titres qui ne sont pas en UTF-8 sont réencodés grâce à iconv, cependant, comme ça se base sur les entêtes HTTP (header HTTP) c'est, malheureusement, loin d'être fiable à 100%… si tu as des exemples de bugs (juste avec les adresses, je devrais me débrouiller), ça m'intéresse :wink:

RSS commentaires

  1. B
  2. 3
  3. 3
  4. X
  5. 8
  6. A

Pour valider le formulaire, saisissez les caractères correspondants aux rangs suivants : 1 3 5 

  • caca
  • confused
  • cry
  • dizzy
  • happy
  • lol
  • neutral
  • omg
  • sad
  • smile
  • tongue
  • wink