0
0
mirror of https://github.com/TrianguloY/UrlChecker.git synced 2024-09-19 20:02:16 +02:00

allow multiple replacements (chosen at random)

This commit is contained in:
TrianguloY 2022-10-14 19:20:12 +02:00
parent 96542b8e2f
commit 487c70b407
4 changed files with 24 additions and 4 deletions

View File

@ -6,6 +6,7 @@ import com.trianguloy.urlchecker.R;
import com.trianguloy.urlchecker.dialogs.JsonEditor;
import com.trianguloy.urlchecker.utilities.InternalFile;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@ -75,7 +76,10 @@ public class PatternCatalog {
)
.put("Youtube ➔ Invidious", new JSONObject()
.put("regex", "^https?://(?:[a-z0-9-]+\\.)*?youtube.com/(.*)")
.put("replacement", "https://yewtu.be/$1")
.put("replacement", new JSONArray()
.put("https://yewtu.be/$1")
.put("https://farside.link/invidious/$1")
)
.put("enabled", "false")
)
;

View File

@ -17,10 +17,12 @@ import com.trianguloy.urlchecker.utilities.AndroidUtils;
import com.trianguloy.urlchecker.utilities.Inflater;
import com.trianguloy.urlchecker.utilities.JavaUtilities;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* This module checks for patterns characters in the url
@ -128,7 +130,21 @@ class PatternDialog extends AModuleDialog implements View.OnClickListener {
if (url.matches(".*" + regex + ".*")) {
message.matches = true;
// check replacements
String replacement = data.has("replacement") ? data.optString("replacement") : null;
String replacement = null;
Object replacements = data.opt("replacement");
if (replacements != null) {
// data exists
if (replacements instanceof JSONArray) {
// array, get random
JSONArray replacementsArray = (JSONArray) replacements;
replacement = replacementsArray.getString(new Random().nextInt(replacementsArray.length()));
} else {
// single data, get that one
replacement = replacements.toString();
}
}
if (replacement != null) {
message.newUrl = url.replaceAll(regex, replacement);
}

View File

@ -49,7 +49,7 @@ Traducciones: %s."</string>
<string name="mPttrn_ok">Ningún patron coincide</string>
<string name="mPttrn_editor">"Aquí puedes editar o añadir nuevos patrones. Formato:
- 'regex': string: java regex válido con el que se comprobará la url.
- 'replacement': string: si se incluye, el botón de 'arreglar' ejecutará 'url=url.replaceAll(regex,replacement)'.
- 'replacement': string|list: si se incluye, el botón de 'arreglar' ejecutará 'url=url.replaceAll(regex,replacement)'. Si es una lista, replacement será un elemento aleatorio de ella.
- 'automatic': boolean: si se pone a 'true' el patrón será aplicado automáticamente.
- 'enabled': boolean: ponlo a 'false' para deshabilitar el patrón.
Nota: si editas los patrones, nuevos patrones incorporados en futuras actualizaciones no serán añadidos automáticamente (puedes recuperarlos con la opción de reset)."</string>

View File

@ -50,7 +50,7 @@ Translations: %s."</string>
<string name="mPttrn_ok">No patterns match</string>
<string name="mPttrn_editor">"Here you can edit or add new pattern. Format:
- 'regex': string: valid java regex that will be matched against the url.
- 'replacement': string: if provided, the 'fix' button will run 'url=url.replaceAll(regex,replacement)'.
- 'replacement': string|list: if provided, the 'fix' button will run 'url=url.replaceAll(regex,replacement)'. If list, replacement will be a random element from it.
- 'automatic': boolean: set to true to automatically apply the pattern.
- 'enabled': boolean: set to false to disable the pattern.
Note: if you edit the patterns, new built-in patterns from app updates will not be added automatically (you can restore them with the reset option)."</string>