Index: trunk/ICeCoffEE/ICeCoffEE/APEMain.m
===================================================================
--- trunk/ICeCoffEE/ICeCoffEE/APEMain.m	(revision 456)
+++ trunk/ICeCoffEE/ICeCoffEE/APEMain.m	(revision 469)
@@ -15,4 +15,5 @@
 #import <ApplicationEnhancer/ApplicationEnhancer.h>
 #import <ApplicationEnhancer/APETools.h>
+#import <mach-o/dyld.h>
 #import <objc/objc-runtime.h>
 #import "ICeCoffEE.h"
@@ -66,5 +67,5 @@
 
 // With APE 1.3, if we're in the exclude list, APEBundleMainEarlyLoad doesn't get invoked; don't need to use APETools.  But we need to do our own management to avoid loading in background-only applications.
-Boolean ICCF_shouldLoad;
+static Boolean ICCF_shouldLoad;
 
 static Boolean ICCF_IsOne(CFTypeRef value) {
@@ -90,5 +91,5 @@
 
     UInt32 icVersion = CFBundleGetVersionNumber(inBundle);
-    ICapeprintf("ICeCoffEE APE: bundle version is %ld (0x%x)\n", icVersion, icVersion);
+    ICapeprintf("bundle version is %ld (0x%x)\n", icVersion, icVersion);
     CFNumberRef icVersionRef = CFNumberCreate(NULL, kCFNumberLongType, &icVersion);
     CFPreferencesSetAppValue(kICLastLoadedVersion, icVersionRef, kICBundleIdentifier);
@@ -99,5 +100,5 @@
     CFBundleRef appBundle = CFBundleGetMainBundle();
     if (appBundle == NULL) {
-	apeprintf("ICeCoffEE APE: Can't get CFBundle for current process; not loading\n");
+	apeprintf("can't get CFBundle for current process; not loading\n");
 	return;
     }
@@ -107,5 +108,5 @@
 	ICCF_IsOne(CFBundleGetValueForInfoDictionaryKey(appBundle, CFSTR("LSBackgroundOnly"))) ||
 	ICCF_IsOne(CFBundleGetValueForInfoDictionaryKey(appBundle, CFSTR("NSBackgroundOnly")))) {
-	ICapeprintf("ICeCoffEE APE: not loading as this application is background-only\n");
+	ICapeprintf("not loading as this application is background-only\n");
 	return;
     }
@@ -115,4 +116,26 @@
 static Boolean ICCF_CFBundleIDMatches(CFStringRef bundleID, CFStringRef test) {
     return CFStringCompare(bundleID, test, kCFCompareCaseInsensitive) == kCFCompareEqualTo;
+}
+
+static Boolean ICCF_tryLoading = false;
+static Boolean ICCF_loadedInWebKit = false;
+static Boolean ICCF_loadedInPDFKit = false;
+
+static void ICCF_OnAddImage(const struct mach_header *mh, intptr_t vmaddr_slide) {
+    if (!ICCF_tryLoading) // when initially registered, called a lot
+	return;
+    if (!ICCF_loadedInWebKit &&
+	ICCF_PatchMethod("WebHTMLView", "ICeCoffEEWebKit", "ICeCoffEEWebKitSuper", "mouseUp:") &&
+	ICCF_PatchMethod("WebHTMLView", "ICeCoffEEWebKit", "ICeCoffEEWebKitSuper", "mouseDown:") &&
+	ICCF_PatchMethod("WebHTMLView", "ICeCoffEEWebKit", "ICeCoffEEWebKitSuper", "menuForEvent:") &&
+	ICCF_PatchMethod("WebPDFView", "ICeCoffEEWebPDFView", "ICeCoffEEWebPDFViewSuper", "menuForEvent:")) {
+	ICapeprintf("loaded in WebHTMLView/WebPDFView for WebKit/Safari 3\n");
+	ICCF_loadedInWebKit = true;
+    }
+    if (!ICCF_loadedInPDFKit &&
+	ICCF_PatchMethod("PDFView", "ICeCoffEEPDFView", "ICeCoffEEPDFViewSuper", "menuForEvent:")) {
+	ICapeprintf("loaded in PDFView for PDFKit\n");
+	ICCF_loadedInPDFKit = true;
+    }	
 }
 
@@ -136,5 +159,5 @@
 		ICCF_PatchMethod("PBXTextView", "ICeCoffEEMenuOnly", "ICeCoffEEMenuSuper", "menuForEvent:");
 	    }
-            ICapeprintf("ICeCoffEE APE: loaded in PBXTextView / XCTextView for Xcode\n");
+            ICapeprintf("loaded in PBXTextView / XCTextView for Xcode\n");
             shouldLoadInNSTextView = NO;
         } else if (ICCF_CFBundleIDMatches(bundleID, CFSTR("com.apple.terminal"))) {
@@ -143,5 +166,5 @@
 		ICCF_PatchMethod("TTView", "ICeCoffEETTView", "ICeCoffEETTViewSuper", "mouseUp:") &&
 		ICCF_PatchMethod("TTView", "ICeCoffEETTView", "ICeCoffEETTViewSuper", "draggingEntered:");
-		ICapeprintf("ICeCoffEE APE: loaded in TTView for Terminal\n");
+		ICapeprintf("loaded in TTView for Terminal\n");
 	    } else {
 		ICCF_PatchMethod("TermSubview", "ICeCoffEEMenuOnly", "ICeCoffEEMenuSuper", "menuForEvent:") &&
@@ -152,26 +175,22 @@
 		ICCF_PatchMethod("TermSubview", "ICeCoffEETerminal", "ICeCoffEETermSubviewSuper", "draggingEntered:") &&
 		ICCF_PatchMethod("TermSubview", "ICeCoffEETerminal", "ICeCoffEETermSubviewSuper", "_optionClickEvent:::");
-		ICapeprintf("ICeCoffEE APE: loaded in TermSubview for Terminal\n");
+		ICapeprintf("loaded in TermSubview for Terminal\n");
 	    }
 	} else if (ICCF_CFBundleIDMatches(bundleID, CFSTR("org.mozilla.camino"))) {
             ICCF_PatchMethod("ChildView", "ICeCoffEEMenuOnly", "ICeCoffEEMenuSuper", "menuForEvent:");
-	    ICapeprintf("ICeCoffEE APE: loaded in ChildView for Camino\n");
+	    ICapeprintf("loaded in ChildView for Camino\n");
         }
     }
 
-    ICCF_PatchMethod("WebHTMLView", "ICeCoffEEWebKit", "ICeCoffEEWebKitSuper", "mouseUp:") &&
-	ICCF_PatchMethod("WebHTMLView", "ICeCoffEEWebKit", "ICeCoffEEWebKitSuper", "mouseDown:") &&
-	ICCF_PatchMethod("WebHTMLView", "ICeCoffEEWebKit", "ICeCoffEEWebKitSuper", "menuForEvent:");
-    ICapeprintf("ICeCoffEE APE: loaded in WebHTMLView for WebKit/Safari 3\n");
-    
     if (shouldLoadInNSTextView) {
         ICCF_PatchMethod("NSTextView", "ICeCoffEE", "ICeCoffEESuper", "mouseDown:") &&
 	ICCF_PatchMethod("NSTextView", "ICeCoffEE", "ICeCoffEESuper", "clickedOnLink:atIndex:") &&
         ICCF_PatchMethod("NSTextView", "ICeCoffEE", "ICeCoffEESuper", "menuForEvent:");
-        ICapeprintf("ICeCoffEE APE: loaded generic NSTextView support\n");
-    }
-    
-    ICCF_PatchMethod("PDFView", "ICeCoffEEMenuOnly", "ICeCoffEEMenuSuper", "menuForEvent:");
-    ICapeprintf("ICeCoffEE APE: loaded in PDFView for PDFKit\n");
+        ICapeprintf("loaded generic NSTextView support\n");
+    }
+    
+    _dyld_register_func_for_add_image(ICCF_OnAddImage);
+    ICCF_tryLoading = true;
+    ICCF_OnAddImage(NULL, 0L);
 
     ICCF_ReloadPrefs();
@@ -183,5 +202,5 @@
 OSStatus APEBundleMessage(CFStringRef message,CFDataRef inData,CFDataRef *outData)
 {
-    ICapeprintf("ICeCoffEE APE: message '%@' (inData = %@)\n", message, inData);
+    ICapeprintf("message '%@' (inData = %@)\n", message, inData);
     
     if (CFStringCompare(message, kICPreferencesChanged, 0) == kCFCompareEqualTo)		
