omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Get Safari page source code in share extension.

    Pythonista
    7
    20
    14664
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • omz
      omz last edited by

      @lukaskollmer Why not just use ui.WebView? It should save cookies too.

      1 Reply Last reply Reply Quote 1
      • lukaskollmer
        lukaskollmer last edited by

        @omz I didn't know that ui.WebView can evaluate JavaScript as well

        1 Reply Last reply Reply Quote 0
        • lukaskollmer
          lukaskollmer last edited by

          @omz is there a way to have ui.WebView load a URL without presenting the webView?

          1 Reply Last reply Reply Quote 0
          • JonB
            JonB last edited by JonB

            Webview also lets you use a few delegate methods which can be useful.

            If you start mucking about with javascript/webviews, I suggest implementing a window.onerror, some console.log functions, and a corresponding delegate method to print to the console. This makes javascript work MUCH less mysterious. See for example here, the lines through the end of the delegate are my standard starting place for touching any javascript.
            (edit: pointed to correct message)

            1 Reply Last reply Reply Quote 1
            • JonB
              JonB last edited by

              w.load_url

              the webview never needs to be presented to load.

              1 Reply Last reply Reply Quote 0
              • lukaskollmer
                lukaskollmer last edited by lukaskollmer

                @JonB I can't get it to load the page until I called present()

                webView = ui.WebView()
                webView.delegate = WebViewDelegate()
                webView.load_url(url)
                webView.present()
                

                If I remove present(), the delegate won't get called

                1 Reply Last reply Reply Quote 0
                • JonB
                  JonB last edited by

                  This works for me:

                  w=ui.WebView()
                  w.load_url('http://www.google.com')
                  src=w.eval_js('document.documentElement.outerHTML')

                  Possibly the delegate only gets called after it is presented? I seem to recall something like that. If so, you should be able to present it once, then close it, or present in a panel.

                  1 Reply Last reply Reply Quote 1
                  • JonB
                    JonB last edited by JonB

                    Actually this works fine for me. Perhaps your delegate or url has some issues. You also need to wait until,the page loads, either via javascript or using the delegate.

                    See for example:
                    https://gist.github.com/jsbain/80cdc7dd82da23cbe16c9befef91d707

                    This shows the delegate getting called without being presented.
                    When I tried to grab outerHTML directly at the end of the script, it has no data, because the page had not yet loaded. In the delegate webview_did_finish_load, it works fine.

                    Also, you could have a for loop that checks for document.readyState, to allow a more script-y sequence of commands. though you have to be a little careful because the readyState may go through complete two times initially.

                    1 Reply Last reply Reply Quote 0
                    • lukaskollmer
                      lukaskollmer last edited by

                      @omz does ui.WebView share cookies between the main app and the extension?

                      1 Reply Last reply Reply Quote 0
                      • JonB
                        JonB last edited by

                        does not look like it.
                        you can get cookies this way ( it would be possible to save this to a file which could be in a shared spot)

                        from objc_util import *
                        storage=ObjCClass('NSHTTPCookieStorage').sharedHTTPCookieStorage()
                        print(storage.cookies())
                        

                        I have seen some stackoverflow code using NSKeyedArchiver to turn this into NSData

                        cookieData= ObjCClass('NSKeyedArchiver').archivedDataWithRootObject_(storage.cookies())
                        

                        though i have not yet had luck in using NSKeyedUnarchiver to go from data back to an cookie storage object (just have not tried hard yet)

                        You can also get the binary cookie file from

                        cookiefile=re.findall(r'/private[^,]*',str(ObjCInstance(storage._cookieStorage()).description()))[0]
                        cookiedata=open(cookiefile).read()
                        
                        1 Reply Last reply Reply Quote 0
                        • jumpbeen
                          jumpbeen last edited by

                          From Apple Developer website .

                          Accessing a Webpage

                          In Share extensions (on both platforms) and Action extensions (iOS only), you can give users access to web content by asking Safari to run a JavaScript file and return the results to the extension. You can also use the JavaScript file to access a webpage before your extension runs (on both platforms), or to access or modify the webpage after your extension completes its task (iOS only). For example, a Share extension can help users share content from a webpage, or an Action extension in iOS might display a translation of the user’s current webpage.

                          To add webpage access and manipulation to your app extension, perform the following steps:

                          Create a JavaScript file that includes a global object named ExtensionPreprocessingJS. Assign a new instance of your custom JavaScript class to this object.
                          In the NSExtensionActivationRule dictionary in your app extension’s Info.plist file, give the NSExtensionActivationSupportsWebPageWithMaxCount key a nonzero value. (To learn more about the activation rule dictionary, see Declaring Supported Data Types for a Share or Action Extension.)
                          When your app extension starts, use the NSItemProvider class to get the results returned by the execution of the JavaScript file.
                          In an iOS app extension, pass values to the JavaScript file if you want Safari to modify the webpage when your extension completes its task. (You use the NSItemProvider class in this step, too.)
                          To tell Safari that your app extension includes a JavaScript file, add the NSExtensionJavaScriptPreprocessingFile key to the NSExtensionAttributes dictionary. The value of the key should be the file that you want Safari to load before your extension starts. For example:

                          <key>NSExtensionAttributes</key>
                                  <dict>
                                      <key>NSExtensionJavaScriptPreprocessingFile</key>
                                      <string>MyJavaScriptFile</string> <!-- Do not include the ".js" filename extension -->
                                  </dict>
                          
                          1 Reply Last reply Reply Quote 0
                          • jaimeesc
                            jaimeesc last edited by

                            I was able to get the source code of the current Safari web page using the following code:

                            req = appex.get_web_page_info()
                            print(req['html'])

                            From there I was able to parse through the HTML using BeautifulSoup.

                            1 Reply Last reply Reply Quote 0
                            • First post
                              Last post
                            Powered by NodeBB Forums | Contributors