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.
Webview and Reader mode
-
Yes, indeed. I just want to see if this js code will, in fact, implement a reading mode. What I type in the console is:
import ui
with open ("safari-reader.js", "r") as myfile:
data=myfile.readlines()
data=str(data)
wv = ui.WebView()
wv.load_url('https://www.nytimes.com/2019/01/29/climate/global-warming-extreme-weather.html')
wv.eval_js(data)
wv.present()I wait a bit after the load_urI and end up with the unmodified webpage.
-
@ihf Not sure you can eval_js before webview is presented
Could you paste here your .js file if it is text
-
It is too long to post but I got it from @JonB post above: https://github.com/amumu/safari-reader-js
-
@ihf ok, sorry, too complex for me. I don't know almost anything about js
-
@ihf Try this, it will be ok for you, I hope
import ui from objc_util import * def safariViewControllerDidFinish_(_self, _cmd, _controller): #print('SafariViewControllerDidFinish_') SFSafariViewController = ObjCInstance(_controller) SFSafariViewController.uiview.close() methods = [safariViewControllerDidFinish_,] protocols = ['SFSafariViewControllerDelegate'] try: MySFSafariViewControllerDelegate = ObjCClass('MySFSafariViewControllerDelegate') except: MySFSafariViewControllerDelegate = create_objc_class('MySFSafariViewControllerDelegate', methods=methods, protocols=protocols) #@on_main_thread def MySFSafariViewController(url, w, h, mode='sheet', popover_location=None): uiview = ui.View() uiview.frame = (0,0,w,h) uiview.background_color = 'white' if mode == 'sheet': uiview.present('sheet',hide_title_bar=True) elif mode == 'popover': if popover_location: uiview.present('popover', hide_title_bar=True, popover_location=popover_location) else: return else: return SFSafariViewController = ObjCClass('SFSafariViewController').alloc().initWithURL_entersReaderIfAvailable_(url,True) # Use new delegate class: delegate = MySFSafariViewControllerDelegate.alloc().init() SFSafariViewController.delegate = delegate SFSafariViewController.setModalPresentationStyle_(3) SFSafariViewController.uiview = uiview # used by delegate objc_uiview = ObjCInstance(uiview) SUIViewController = ObjCClass('SUIViewController') vc = SUIViewController.viewControllerForView_(objc_uiview) vc.presentViewController_animated_completion_(SFSafariViewController, True, None) def main(): # demo code mv = ui.View() mv.background_color = 'white' mv.name = 'Test SFSafariViewController' mv.present() url = nsurl('https://www.nytimes.com/2019/01/29/climate/global-warming-extreme-weather.html') #MySFSafariViewController(url,600,500) MySFSafariViewController(url,600,500, mode ='popover', popover_location=(mv.width-40,60)) if __name__ == '__main__': main()
-
My code seems to have a problem with sheet mode, test it with popover mode.
I'm searching whyEdit: remove the @on_main_thread line.... ==> ok
-
I took a stab at the js code. With the code below I get the article part of the original page, but all attempts to get the cleaned version end in a non-helpful js error, which I do not have time to track right now.
import wkwebview import ui with open('safari-reader.js','r',encoding='utf-8') as f: safari_script = f.read() class Delegate: @ui.in_background def webview_did_finish_load(self, webview): w.eval_js('r = new ReaderArticleFinder(document); document.body.innerHTML = r.articleNode().innerHTML;') w = wkwebview.WKWebView(delegate=Delegate()) w.present() w.add_script(safari_script) w.load_url('http://blog.manbolo.com/2012/11/20/using-xcode-opengl-es-frame-capture')
-
@mikael I tried your script with the NYTimes url above and got these errors:
WARN: et2 snippet should only load once per page
ERROR: Pinterest Tag Error: 'load' command was called multiple times. Previously for tag id '%s', now for tag id '%s'.
ERROR: TypeError: null is not an object (evaluating 'r.articleNode().innerHTML') (https://www.nytimes.com/2019/01/29/climate/global-warming-extreme-weather.html, line: 1, column: 81)
ERROR: Script error. (, line: 0, column: 0)Is this what you are talking about?
-
@ihf Just to know what you think, did you try mine?
-
@cvp Yes, just now and it also fails on the Nytimes articles. Is it working for you? If so, I will send the error messages.
-
-
Yes, but look at where the article ends.
-
@ihf I don't have any error.
-
I canβt reproduce the error (I may have run it under python 2.7). I can get what you are seeing but the article is truncated.
-
@ihf When I try in Safari, I get the same result, except some lines at end saying there are 4 articles more
-
-
@ihf As I use the standard Apple Safari view, I suppose we can't get better, no?
-
@cvp I tried on some Macstories and it worked perfectly. What I was seeing may be peculiar to the NYtimes as I cannot login so there are limits to what it will show.
Thanks so much for working on this. Now I will try to incorporate this into rss_reader.py
-
@ihf π good luck
-
@ihf If you go back in normal mode, at bottom, you get