Forum Archive

WKWebView: how to know add_style is finished?

cvp

@mikael I use your WKWebView module (thanks for this marvelous piece of code, as usual) to display an ePub book. In an ePub, each chapter is an html file.
To perform aA-like process to change font-size, I use
wv.add_style('body { column-width:' + w + 'px; min-width:' + w + 'px; column-gap: 9%; height:' + h + 'px; margin-left: 4% ; margin-right: 4% ; margin-top: 4%; margin-bottom: 4%;}')
To know the number of pages of the chapter with this font-size, I need to compute webview.content_size.width divided by webview frame.width.

But I have to be sure that the JavaScript executed by add_style is finished.

My question is: how to be sure it is finished?

Actually, I loop until webview.content_size.width does not change any more but that is not really professional 😀

JonB

Can you check window.ready?

JonB

I mean document.readyState...

cvp

@JonB Thanks for the advice, I'll check tomorrow, already late for me.

cvp

@JonB But if I understand, this is not a wait, it is also a check to insert in a while loop and sleep in the loop?

cvp

@JonB I'be just tested. It is not ok, the state is ready but the load of the html (and its split in multi-columns) is not yet finished

mikael

@cvp, checked the docs. The method used to insert the script, addUserScript, is not documented as sync or async, and provides no additional parameters like completion callback. add_style adds the script to the end of the document, as (I seem to recall that) adding to the beginning did not really work for styles.

cvp

@mikael Ok, Thanks for the check. It is not so important as I can check each 0.1 second if the content_size width has changed. On my iPad mini 4, it takes 0.1 second for a chapter of 5 pages but 2 seconds for a chapter of 30 pages..
This automatic multi-columns functionality is really nice to have book-like pages

I think iBooks app performs something like that because if you change the font size, it takes some time before to show the total number of pages...