@@ -29,16 +29,26 @@ class ImageWebViewActivity : BaseActivity() {
2929
3030 companion object {
3131
32+ const val TAG_INTERACTIVE = " interactive"
33+
34+ const val TAG_XK3D = " xk3d"
35+
3236 private const val PERMALINK_1663 = " xkcd_permalink_1663"
3337
34- fun startActivity (context : Context , num : Long , translationMode : Boolean ) {
38+ private const val PERMALINK_2288 = " xkcd_permalink_2288"
39+
40+ fun startActivity (context : Context , num : Long , translationMode : Boolean , webPageMode : String = TAG_INTERACTIVE ) {
3541 val intent = Intent (context, ImageWebViewActivity ::class .java)
3642 intent.putExtra(" index" , num)
3743 intent.putExtra(" translationMode" , translationMode)
44+ intent.putExtra(" webPageMode" , webPageMode)
3845 context.startActivity(intent)
3946 }
4047 }
4148
49+ private val isInteractiveMode: Boolean
50+ get() = intent.getStringExtra(" webPageMode" ) == TAG_INTERACTIVE
51+
4252 private val webView by lazy { findViewById<WebView >(R .id.imgWebView) }
4353
4454 private val progress by lazy { findViewById<ProgressBar >(R .id.progressWebView) }
@@ -49,6 +59,10 @@ class ImageWebViewActivity : BaseActivity() {
4959 get() = sharedPreferences.getString(PERMALINK_1663 , " " )
5060 set(value) = sharedPreferences.edit { putString(PERMALINK_1663 , value) }
5161
62+ private var permalink2288: String?
63+ get() = sharedPreferences.getString(PERMALINK_2288 , " " )
64+ set(value) = sharedPreferences.edit { putString(PERMALINK_2288 , value) }
65+
5266 private var index: Long = 1L
5367
5468 override fun onCreate (savedInstanceState : Bundle ? ) {
@@ -99,6 +113,56 @@ class ImageWebViewActivity : BaseActivity() {
99113 }
100114
101115 private fun loadXkcdInWebView (xkcd : XkcdPic ) {
116+ val urlScriptPair = if (isInteractiveMode) {
117+ loadInteractivePage(xkcd)
118+ } else {
119+ loadXk3dPage()
120+ }
121+ val url = urlScriptPair.first
122+ val script = urlScriptPair.second
123+ webView.webViewClient = object : WebViewClient () {
124+
125+ override fun onPageFinished (view : WebView , url : String ) {
126+ this @ImageWebViewActivity.title = view.title
127+ Timber .d(" Current page $url " )
128+ if (index == 1663L && url.contains(" /1663/#" .toRegex())) {
129+ if (permalink1663.isNullOrBlank()) {
130+ permalink1663 = extractUuidFrom1663url(url)
131+ }
132+ } else if (index == 2288L && url.contains(" https://xkcd.com/2288/#-" .toRegex())) {
133+ permalink2288 = url
134+ }
135+ }
136+ }
137+
138+ webView.webChromeClient = object : WebChromeClient () {
139+
140+ override fun onProgressChanged (view : WebView ? , newProgress : Int ) {
141+ super .onProgressChanged(view, newProgress)
142+ val baseTitle = " xkcd: ${xkcd.title} "
143+ if (newProgress > 20 && script.isNotBlank()) view?.loadUrl(script)
144+ title = baseTitle + when (newProgress) {
145+ in 0 .. 99 -> {
146+ progress.isIndeterminate = false
147+ progress.progress = newProgress
148+ " (${newProgress} %)"
149+ }
150+ else -> {
151+ progress.visibility = View .GONE
152+ " "
153+ }
154+ }
155+ }
156+ }
157+
158+ webView.updateSettings()
159+ if (index == 2288L ) {
160+ webView.settings.displayZoomControls = true
161+ }
162+ webView.loadUrl(url)
163+ }
164+
165+ private fun loadInteractivePage (xkcd : XkcdPic ): Pair <String , String > {
102166 var script = " "
103167 val url = when (xkcd.num.toInt()) {
104168 1663 -> {
@@ -108,15 +172,46 @@ class ImageWebViewActivity : BaseActivity() {
108172 " https://zjn0505.github.io/xkcd-undressed/${xkcd.num} /#${permalink1663} "
109173 }
110174 }
175+ 2288 -> {
176+ script = """
177+ javascript:
178+ var toDelete = [];
179+ toDelete.push(document.getElementById("topContainer"));
180+ toDelete.push(document.getElementById("bottom"));
181+ toDelete.push(document.getElementById("ctitle"));
182+ toDelete.push(document.getElementById("middleFooter"));
183+ toDelete.push(document.getElementsByClassName("menuCont")[0]);
184+ toDelete.push(document.getElementsByClassName("menuCont")[1]);
185+ toDelete.push(document.getElementsByClassName("br")[0]);
186+ toDelete.push(document.getElementsByClassName("br")[1]);
187+ toDelete.push(document.getElementsByClassName("comicNav")[0]);
188+ toDelete.push(document.getElementsByClassName("comicNav")[1]);
189+ toDelete.forEach(function (element) {
190+ if (element) element.style.display = 'none';
191+ });
192+ """ .trimIndent()
193+ if (permalink2288.isNullOrBlank()) {
194+ " https://xkcd.com/2288"
195+ } else {
196+ permalink2288!!
197+ }
198+ }
111199 in resources.getIntArray(R .array.interactive_comics) -> if (! intent.getBooleanExtra(" translationMode" , false )) {
112200 " https://zjn0505.github.io/xkcd-undressed/${xkcd.num} /"
113201 } else {
114202 Timber .d(" region ${Locale .getDefault()} " )
115203 " https://zjn0505.github.io/xkcd-undressed/${xkcd.num} /?region=${Locale .getDefault().toString().replace(" #" , " _" )} "
116204 }
117205 else -> {
118- // xk3d
119- script = """
206+ " "
207+ }
208+ }
209+ return url to script
210+ }
211+
212+ private fun loadXk3dPage (): Pair <String , String > {
213+ // xk3d
214+ val script = """
120215 javascript:
121216 var toDelete = [];
122217 toDelete.push(document.getElementById("topContainer"));
@@ -141,44 +236,8 @@ class ImageWebViewActivity : BaseActivity() {
141236 comic.style.zoom = window.innerWidth / comic.offsetWidth / 1.2;
142237 }
143238 """ .trimIndent()
144- " https://xk3d.xkcd.com/$index "
145- }
146- }
147- webView.webViewClient = object : WebViewClient () {
148-
149- override fun onPageFinished (view : WebView , url : String ) {
150- this @ImageWebViewActivity.title = view.title
151- Timber .d(" Current page $url " )
152- if (url.contains(" /1663/#" .toRegex())) {
153- if (permalink1663.isNullOrBlank()) {
154- permalink1663 = extractUuidFrom1663url(url)
155- }
156- }
157- }
158- }
159-
160- webView.webChromeClient = object : WebChromeClient () {
161-
162- override fun onProgressChanged (view : WebView ? , newProgress : Int ) {
163- super .onProgressChanged(view, newProgress)
164- val baseTitle = " xkcd: ${xkcd.title} "
165- if (newProgress > 20 && script.isNotBlank()) view?.loadUrl(script)
166- title = baseTitle + when (newProgress) {
167- in 0 .. 99 -> {
168- progress.isIndeterminate = false
169- progress.progress = newProgress
170- " (${newProgress} %)"
171- }
172- else -> {
173- progress.visibility = View .GONE
174- " "
175- }
176- }
177- }
178- }
179-
180- webView.updateSettings()
181- webView.loadUrl(url)
239+ val url = " https://xk3d.xkcd.com/$index "
240+ return url to script
182241 }
183242
184243 @VisibleForTesting
0 commit comments