A script for cycript
// 打印按钮的action及其target | |
function actionWithTargets(button) { | |
var allTargets = [button allTargets].allObjects(); | |
if (!allTargets) { | |
return "is not a uicontrol" | |
} | |
var allShow = []; | |
for (var i = 0; i < allTargets.length; i++) { | |
var target = allTargets[i]; | |
var actions = [button actionsForTarget: target forControlEvent: UIControlEventTouchUpInside]; | |
allShow.push("actions:" + actions + " target:" + target); | |
} | |
return allShow; | |
} | |
// 打印类的属性 | |
function tryPrintIvars(a){ var x={}; for(i in *a){ try{ x[i] = (*a)[i]; } catch(e){} } return x; } | |
function allWin() { | |
return [UIApplication sharedApplication].windows | |
} | |
var allWindows=function () { | |
return allWin() | |
}() | |
function presentedVCForVC(presentedViewController) { | |
if ([presentedViewController isKindOfClass: [UINavigationController class]]) { | |
var topVC = presentedViewController.topViewController; | |
if (topVC.presentedViewController) { | |
return presentedVCForVC(topVC.presentedViewController); | |
} | |
return topVC; | |
} | |
if ([presentedViewController isKindOfClass: [UITabBarController class]]) { | |
var selectedVC = presentedViewController.selectedViewController; | |
return presentedVCForVC(selectedVC); | |
} | |
return presentedViewController; | |
} | |
// 获取对应UIWindow的当前控制器 | |
function currentWithWindow(win) { | |
if (!win.rootViewController) { | |
return "The window hadn't a root viewcontroller"; | |
} | |
return presentedVCForVC(win.rootViewController); | |
} | |
// 打印keyWindow上的当前的控制器 | |
function currentVCWithKeyWindow() { | |
return currentWithWindow([UIApplication sharedApplication].keyWindow) | |
} | |
// 打印类的方法 | |
function printMethods(className, isa) { | |
var count = new new Type("I"); | |
var classObj = (isa != undefined) ? objc_getClass(className).constructor : objc_getClass(className); | |
var methods = class_copyMethodList(classObj, count); | |
var methodsArray = []; | |
for(var i = 0; i < *count; i++) { | |
var method = methods[i]; | |
methodsArray.push({selector:method_getName(method), implementation:method_getImplementation(method)}); | |
} | |
free(methods); | |
return methodsArray; | |
} | |
// 根据正则匹配需要的方法 | |
function methodsMatching(cls, regexp) { return [[new Selector(m).type(cls), m] for (m in cls.prototype) if (!regexp || regexp.test(m))]; } | |
// 替换OC的已有方法 | |
// 打印类的所有子类 | |
function allSubClass(className) { | |
return [c for each (c in ObjectiveC.classes) if (class_getSuperclass(c) && [c isSubclassOfClass:className])] | |
} | |
// 加载framework | |
function loadFramework(fw) { | |
var h="/System/Library/",t="Frameworks/"+fw+".framework"; | |
[[NSBundle bundleWithPath:h+t]||[NSBundle bundleWithPath:h+"Private"+t] load]; | |
} | |
// 或者使用其他方法 | |
// include other .cy files | |
function include(fn) { | |
var t = [new NSTask init]; [t setLaunchPath:@"/usr/bin/cycript"]; [t setArguments:["-c", fn]]; | |
var p = [NSPipe pipe]; [t setStandardOutput:p]; [t launch]; [t waitUntilExit]; | |
var s = [new NSString initWithData:[[p fileHandleForReading] readDataToEndOfFile] encoding:4]; | |
return this.eval(s.toString()); | |
} | |
// CGGeometry | |
function CGPointMake(x, y) { return {0:x, 1:y}; } | |
function CGSizeMake(w, h) { return {0:w, 1:h}; } | |
function CGRectMake(x, y, w, h) { return {0:CGPointMake(x,y), 1:CGSizeMake(w, h)}; } | |
"Added common.cy to \""+NSProcessInfo.processInfo() .processName.toString()+"\" ("+NSProcessInfo.processInfo() .processIdentifier.toString()+")" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment