Skip to content

Commit

Permalink
Merge pull request #8 from darkryh/develop
Browse files Browse the repository at this point in the history
Resolve problem then the NoMoreWebview webclient doens't work at all when setting custom blocker
  • Loading branch information
darkryh authored Sep 27, 2024
2 parents d01457c + dfdd713 commit 1abf171
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci-develop.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
name: Develop Workflow / Build and Unit Tests
name: Main Workflow / Build and Unit Tests

on:
push:
branches:
- master
- develop
pull_request:
branches:
Expand Down
2 changes: 1 addition & 1 deletion NoMoreAdsOnMyWebViewPlayer/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ publishing {

groupId = "com.ead.lib"
artifactId = "NoMoreAdsOnMyWebViewPlayer"
version = "0.1.0"
version = "0.1.1"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import com.ead.lib.nomoreadsonmywebviewplayer.models.BlockerClient

/**
Expand All @@ -17,7 +18,7 @@ open class BaseWebView @JvmOverloads constructor(
/**
* param context for the web view
*/
context: Context,
private val context: Context,
/**
* param attributeSet for the web view
*/
Expand Down Expand Up @@ -66,11 +67,27 @@ open class BaseWebView @JvmOverloads constructor(

override val exceptionWordKeys: List<String>
get() = listOf(
"your key word"
/**
* Your Key words
*/
)
}
}


override fun setWebViewClient(client: WebViewClient) {
if (client !is BlockerClient) {
throw IllegalArgumentException(context.getString(R.string.illegal_argument_exception_message))
}

/**
* Override setter for make the client work just in NoMoreAdsWebView or BasicWebView
*/
this.client = client

super.setWebViewClient(client)
}

/**
* Setup the web view settings requirements to work properly
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ open class BlockerClient(
* if want to accept redirections
* override this method and return true
*/
protected open fun onOverrideUrlLoading(view: WebView?, request: WebResourceRequest?) : Boolean = false
open fun onOverrideUrlLoading(view: WebView?, request: WebResourceRequest?) : Boolean = false

/**
* Used to validate, to replace shouldInterceptRequest
* his value by default is blocking request
* if want to accept intercepted request
* override this method and return a WebResourceResponse or null
*/
protected open fun onInterceptRequest(view: WebView?, request: WebResourceRequest?) : WebResourceResponse? = emptyResource
open fun onInterceptRequest(view: WebView?, request: WebResourceRequest?) : WebResourceResponse? = emptyResource
}
4 changes: 4 additions & 0 deletions NoMoreAdsOnMyWebViewPlayer/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="illegal_argument_exception_message">WebViewClient type must be a BlockerClient</string>
</resources>
64 changes: 50 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
[![](https://jitpack.io/v/darkryh/NoMoreAdsOnMyWebViewPlayer.svg)](https://jitpack.io/#darkryh/NoMoreAdsOnMyWebViewPlayer)
![CI](https://github.com/darkryh/NoMoreAdsOnMyWebViewPlayer/actions/workflows/ci-develop.yml/badge.svg)
![CI](https://github.com/darkryh/NoMoreAdsOnMyWebViewPlayer/actions/workflows/ci-develop-instrumental.yml/badge.svg)
![CI](https://github.com/darkryh/NoMoreAdsOnMyWebViewPlayer/actions/workflows/ci-release-production.yml/badge.svg)
# NoMoreAdsOnMyWebViewPlayer

It's an Android Library that permit custom properties and remove ads from embed websites using WebView:
Expand Down Expand Up @@ -41,8 +44,6 @@ dependencies {
# Example of Configuration with a Custom Blocker
This example is to make compatible with a custom site that contains this media players.
```kotlin
import com.ead.lib.nomoreadsonmywebviewplayer.core.Blocker

class MainActivity : ComponentActivity() {

fun onCreate() {
Expand All @@ -51,6 +52,11 @@ class MainActivity : ComponentActivity() {
modifier = modifier.fillMaxSize(),
factory = { context ->
NoMoreAdsWebView(context).apply {
/**
* When setting the webViewClient
* the instance has to be NoMoreAdsWebView
* to work in a correctly way
*/
webViewClient = object : BlockerClient() {
/**
* Exceptions key words that let known
Expand All @@ -72,18 +78,48 @@ class MainActivity : ComponentActivity() {
}
```

# Example configuration
# Replacement options for override clients
Options available to the client the other ones still the same options.
```kotlin
@Composable
fun NoMoreAdsWebView(modifier: Modifier = Modifier) {
AndroidView(
modifier = modifier.fillMaxSize(),
factory = { context ->
NoMoreAdsWebView(context).apply {
loadUrl("your embed url")
}
}
)
class MainActivity : ComponentActivity() {

fun onCreate() {
setContent {
AndroidView(
modifier = modifier.fillMaxSize(),
factory = { context ->
NoMoreAdsWebView(context).apply {

/**
* Replacement option for ShouldOverrideUrlLoading(view, request)
*/
override fun onOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?
): Boolean {
/**
* Do your logic
*/
return super.onOverrideUrlLoading(view, request)
}

/**
* Replacement option for ShouldInterceptRequest(view, request)
*/
override fun onInterceptRequest(
view: WebView?,
request: WebResourceRequest?
): WebResourceResponse? {
/**
* Do your logic
*/
return super.onInterceptRequest(view, request)
}
}
}
)
}
}
}
```

Expand Down Expand Up @@ -112,4 +148,4 @@ And loading as a normal webview.
```

# Want to collaborate
f you want to help or collaborate, feel free to contact me on X account @Darkryh or just make a request.
If you want to help or collaborate, feel free to contact me on X account @Darkryh or just make a request.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package com.ead.app.nomoreadsonmywebviewplayer

import android.annotation.SuppressLint
import android.os.Bundle
import android.webkit.WebResourceRequest
import android.webkit.WebResourceResponse
import android.webkit.WebView
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
Expand All @@ -21,6 +24,7 @@ import com.ead.app.nomoreadsonmywebviewplayer.presentation.theme.NoMoreAdsOnMyWe
import com.ead.app.nomoreadsonmywebviewplayer.util.TestTags
import com.ead.lib.nomoreadsonmywebviewplayer.NoMoreAdsWebView
import com.ead.lib.nomoreadsonmywebviewplayer.core.Blocker
import com.ead.lib.nomoreadsonmywebviewplayer.models.BlockerClient

class MainActivity : ComponentActivity() {

Expand Down Expand Up @@ -61,6 +65,36 @@ fun NoMoreAdsWebView(modifier: Modifier = Modifier, event: (MainEvent) -> Unit)
NoMoreAdsWebView(context).apply {
id = R.id.test_id_no_more_ads_web_view
event(MainEvent.InitializeWebView(this))
webViewClient = object : BlockerClient() {
override val exceptionWordKeys: List<String>
get() = listOf(
/**
* Your Key words
*/
)

/**
* Replacement option for ShouldOverrideUrlLoading(view, request)
*/
override fun onOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?
): Boolean {
print("What amazing replacement :)")
return super.onOverrideUrlLoading(view, request)
}

/**
* Replacement option for ShouldInterceptRequest(view, request)
*/
override fun onInterceptRequest(
view: WebView?,
request: WebResourceRequest?
): WebResourceResponse? {
print("What amazing replacement :)")
return super.onInterceptRequest(view, request)
}
}
}
}
)
Expand Down

0 comments on commit 1abf171

Please sign in to comment.