当iPhone模拟器启动时,自动打开Safari调试器

时间:2022-09-25 19:05:22

The iOS web debugger in Safari is the bee's knees, but it closes every time the Simulator is restarted. Not only is it annoying to re-open it from the menu after every build, but it makes it tricky to debug any behavior that happens during startup.

Safari中的iOS web调试器是蜜蜂的膝盖,但每次模拟器重新启动时它就会关闭。在每次构建之后从菜单中重新打开它不仅很烦人,而且在启动过程中调试任何行为都很困难。

Is there a way to set up a trigger in Xcode to automatically open the Safari debugger after every build, or perhaps a way to build a shell script or Automator action to do a build and immediately open the debugger?

是否有一种方法可以在Xcode中设置一个触发器,在每次构建之后自动打开Safari调试器,或者可能有一种方法可以构建shell脚本或自动操作来进行构建并立即打开调试器?

5 个解决方案

#1


7  

This is a partial solution. This opens the debug window of Safari with one click which is a lot better but not automatic.

这是部分解。这打开了Safari的调试窗口,只需点击一下,就会更好,但不是自动的。

Open Script Editor on your mac (Command + Space Bar and type in Script Editor)

在mac上打开脚本编辑器(命令+空格键,并输入脚本编辑器)

Paste in this code:

粘贴在这段代码中:

-- `menu_click`, by Jacob Rus, September 2006
-- 
-- Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}`
-- Execute the specified menu item.  In this case, assuming the Finder 
-- is the active application, arranging the frontmost folder by date.

on menu_click(mList)
    local appName, topMenu, r

    -- Validate our input
    if mList's length < 3 then error "Menu list is not long enough"

    -- Set these variables for clarity and brevity later on
    set {appName, topMenu} to (items 1 through 2 of mList)
    set r to (items 3 through (mList's length) of mList)

    -- This overly-long line calls the menu_recurse function with
    -- two arguments: r, and a reference to the top-level menu
    tell application "System Events" to my menu_click_recurse(r, ((process appName)'s ¬
        (menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menu_click

on menu_click_recurse(mList, parentObject)
    local f, r

    -- `f` = first item, `r` = rest of items
    set f to item 1 of mList
    if mList's length > 1 then set r to (items 2 through (mList's length) of mList)

    -- either actually click the menu item, or recurse again
    tell application "System Events"
        if mList's length is 1 then
            click parentObject's menu item f
        else
            my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
        end if
    end tell
end menu_click_recurse

menu_click({"Safari", "Develop", "Simulator", "index.html"})

Once the simulator has opened, click run on your script (you might need to allow the script editor in the settings the first time).

一旦模拟器打开,单击您的脚本上的run(您可能需要第一次在设置中允许脚本编辑器)。

(Optional) You can save your the scripts as an app so that you don't have to have the script editor open.

(可选)您可以将脚本保存为一个应用程序,这样您就不必打开脚本编辑器。

#2


2  

There is question that should be marked a duplicate that describes using setTimeout() to give you enough time to switch windows over to Safari and set a breakpoint.

有一个问题应该标记为一个副本,该副本描述使用setTimeout()来给您足够的时间将windows切换到Safari并设置断点。

Something like this, where startMyApp is the bootstrap function of your app:

像这样,startMyApp是你的app的引导功能:

setTimeout(function () {
  startMyApp();
}, 20000);

It is super ghetto, but does work. I've submitted a feature request via http://www.apple.com/feedback/safari.html too to close the loop.

这是超级贫民窟,但确实有用。我也通过http://www.apple.com/feedback/safari.html提交了一个特性请求来关闭循环。

#3


2  

Extending upon the @*er's answer, if you use WKWebView you could:

根据@*er的回答,如果您使用WKWebView,您可以:

    let contentController:WKUserContentController = WKUserContentController()


    let pauseForDebugScript = WKUserScript(source: "window.alert(\"Go, turn on Inspector, I'll hold them back!\")",
                                           injectionTime: WKUserScriptInjectionTime.AtDocumentStart,
                                           forMainFrameOnly: true)
    contentController.addUserScript(pauseForDebugScript)

    let config = WKWebViewConfiguration()
    config.userContentController = contentController
    //Init browser with configuration (our injected script)
    browser = WKWebView(frame: CGRect(x:0, y:0, width: view.frame.width, height: containerView.frame.height), configuration:config)

also important thing is to implement alert handler from WKUIDelegate protocol

同样重要的是实现来自WKUIDelegate协议的警报处理器

//MARK: WKUIDelegate
func webView(webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String,
             initiatedByFrame frame: WKFrameInfo, completionHandler: () -> Void) {

    let alertController = UIAlertController(title: message, message: nil,
                                            preferredStyle: UIAlertControllerStyle.Alert);

    alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel) {
        _ in completionHandler()}
    );

    self.presentViewController(alertController, animated: true, completion: {});
}

and one little thing just in case you could have an UIAlertController:supportedInterfaceOrientations was invoked recursively error add following extension (From this SO Answer)

还有一件事,以防你有一个UIAlertController: supportedinterfaceorientation被递归地调用了错误添加了扩展名

extension UIAlertController {     
    public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.Portrait
    }
    public override func shouldAutorotate() -> Bool {
        return false
    }
}

#4


1  

Combining Tom's answer with my solution, first do the following:

将汤姆的答案与我的解决方案结合起来,首先做以下几点:

  • Create an application as Tom describes above
  • 像Tom上面描述的那样创建一个应用程序
  • In "System Preferences -> Security & Privacy -> Privacy -> Accessibility" add your new Script Applicaiton and make sure it is allowed to control your computer
  • 在“系统首选项—>安全与隐私—>隐私—>可访问性”中,添加您的新脚本应用程序,并确保允许它控制您的计算机

Now, I'm using cordova and from the command line the following builds, runs emulator and opens safari debug console:

现在,我正在使用cordova,从命令行中运行以下构建,运行仿真器并打开safari调试控制台:

cordova build ios; cordova emulate ios; open /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app; sleep 1 ; open /PATH/TO/OpenDevelop.app/

Make sure to replace /PATH/TO/ with the appropriate path to where you saved your script.

确保用保存脚本的适当路径替换/PATH/ to /。

#5


-4  

Humm,I don't think you can do that,but you can just "CTRL+R" in the web debugger.

我不认为你能做到这一点,但你可以在网络调试器中“CTRL+R”。

#1


7  

This is a partial solution. This opens the debug window of Safari with one click which is a lot better but not automatic.

这是部分解。这打开了Safari的调试窗口,只需点击一下,就会更好,但不是自动的。

Open Script Editor on your mac (Command + Space Bar and type in Script Editor)

在mac上打开脚本编辑器(命令+空格键,并输入脚本编辑器)

Paste in this code:

粘贴在这段代码中:

-- `menu_click`, by Jacob Rus, September 2006
-- 
-- Accepts a list of form: `{"Finder", "View", "Arrange By", "Date"}`
-- Execute the specified menu item.  In this case, assuming the Finder 
-- is the active application, arranging the frontmost folder by date.

on menu_click(mList)
    local appName, topMenu, r

    -- Validate our input
    if mList's length < 3 then error "Menu list is not long enough"

    -- Set these variables for clarity and brevity later on
    set {appName, topMenu} to (items 1 through 2 of mList)
    set r to (items 3 through (mList's length) of mList)

    -- This overly-long line calls the menu_recurse function with
    -- two arguments: r, and a reference to the top-level menu
    tell application "System Events" to my menu_click_recurse(r, ((process appName)'s ¬
        (menu bar 1)'s (menu bar item topMenu)'s (menu topMenu)))
end menu_click

on menu_click_recurse(mList, parentObject)
    local f, r

    -- `f` = first item, `r` = rest of items
    set f to item 1 of mList
    if mList's length > 1 then set r to (items 2 through (mList's length) of mList)

    -- either actually click the menu item, or recurse again
    tell application "System Events"
        if mList's length is 1 then
            click parentObject's menu item f
        else
            my menu_click_recurse(r, (parentObject's (menu item f)'s (menu f)))
        end if
    end tell
end menu_click_recurse

menu_click({"Safari", "Develop", "Simulator", "index.html"})

Once the simulator has opened, click run on your script (you might need to allow the script editor in the settings the first time).

一旦模拟器打开,单击您的脚本上的run(您可能需要第一次在设置中允许脚本编辑器)。

(Optional) You can save your the scripts as an app so that you don't have to have the script editor open.

(可选)您可以将脚本保存为一个应用程序,这样您就不必打开脚本编辑器。

#2


2  

There is question that should be marked a duplicate that describes using setTimeout() to give you enough time to switch windows over to Safari and set a breakpoint.

有一个问题应该标记为一个副本,该副本描述使用setTimeout()来给您足够的时间将windows切换到Safari并设置断点。

Something like this, where startMyApp is the bootstrap function of your app:

像这样,startMyApp是你的app的引导功能:

setTimeout(function () {
  startMyApp();
}, 20000);

It is super ghetto, but does work. I've submitted a feature request via http://www.apple.com/feedback/safari.html too to close the loop.

这是超级贫民窟,但确实有用。我也通过http://www.apple.com/feedback/safari.html提交了一个特性请求来关闭循环。

#3


2  

Extending upon the @*er's answer, if you use WKWebView you could:

根据@*er的回答,如果您使用WKWebView,您可以:

    let contentController:WKUserContentController = WKUserContentController()


    let pauseForDebugScript = WKUserScript(source: "window.alert(\"Go, turn on Inspector, I'll hold them back!\")",
                                           injectionTime: WKUserScriptInjectionTime.AtDocumentStart,
                                           forMainFrameOnly: true)
    contentController.addUserScript(pauseForDebugScript)

    let config = WKWebViewConfiguration()
    config.userContentController = contentController
    //Init browser with configuration (our injected script)
    browser = WKWebView(frame: CGRect(x:0, y:0, width: view.frame.width, height: containerView.frame.height), configuration:config)

also important thing is to implement alert handler from WKUIDelegate protocol

同样重要的是实现来自WKUIDelegate协议的警报处理器

//MARK: WKUIDelegate
func webView(webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String,
             initiatedByFrame frame: WKFrameInfo, completionHandler: () -> Void) {

    let alertController = UIAlertController(title: message, message: nil,
                                            preferredStyle: UIAlertControllerStyle.Alert);

    alertController.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Cancel) {
        _ in completionHandler()}
    );

    self.presentViewController(alertController, animated: true, completion: {});
}

and one little thing just in case you could have an UIAlertController:supportedInterfaceOrientations was invoked recursively error add following extension (From this SO Answer)

还有一件事,以防你有一个UIAlertController: supportedinterfaceorientation被递归地调用了错误添加了扩展名

extension UIAlertController {     
    public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
        return UIInterfaceOrientationMask.Portrait
    }
    public override func shouldAutorotate() -> Bool {
        return false
    }
}

#4


1  

Combining Tom's answer with my solution, first do the following:

将汤姆的答案与我的解决方案结合起来,首先做以下几点:

  • Create an application as Tom describes above
  • 像Tom上面描述的那样创建一个应用程序
  • In "System Preferences -> Security & Privacy -> Privacy -> Accessibility" add your new Script Applicaiton and make sure it is allowed to control your computer
  • 在“系统首选项—>安全与隐私—>隐私—>可访问性”中,添加您的新脚本应用程序,并确保允许它控制您的计算机

Now, I'm using cordova and from the command line the following builds, runs emulator and opens safari debug console:

现在,我正在使用cordova,从命令行中运行以下构建,运行仿真器并打开safari调试控制台:

cordova build ios; cordova emulate ios; open /Applications/Xcode.app/Contents/Developer/Applications/Simulator.app; sleep 1 ; open /PATH/TO/OpenDevelop.app/

Make sure to replace /PATH/TO/ with the appropriate path to where you saved your script.

确保用保存脚本的适当路径替换/PATH/ to /。

#5


-4  

Humm,I don't think you can do that,but you can just "CTRL+R" in the web debugger.

我不认为你能做到这一点,但你可以在网络调试器中“CTRL+R”。