Fix issue #8 - Support for amp links

This commit is contained in:
Thomas 2020-04-04 11:58:21 +02:00
parent ad698561ab
commit 73a6a823e3
2 changed files with 27 additions and 7 deletions

View File

@ -66,6 +66,8 @@ public class TransformActivity extends Activity {
final Pattern bibliogramAccountPattern = Pattern.compile("(m\\.|www\\.)?instagram.com(((?!/p/).)+)");
final Pattern maps = Pattern.compile("/maps/place/[^@]+@([\\d.,z]{3,}).*");
final Pattern extractPlace = Pattern.compile("/maps/place/(((?!/data).)*)");
final Pattern ampExtract = Pattern.compile("amp/s/(.*)");
private Thread thread;
private ArrayList<String> notShortnedURLDialog;
@ -280,6 +282,25 @@ public class TransformActivity extends Activity {
forwardToBrowser(intent);
}
}
//AMP URLs (containing /amp/s like Google AMP links)
else if (url.contains("/amp/s")) {
Intent delegate = new Intent(Intent.ACTION_VIEW);
Matcher matcher = ampExtract.matcher(url);
String transformedURL = null;
while (matcher.find()) {
transformedURL = "https://" + matcher.group(1);
}
if (transformedURL != null) {
delegate.setData(Uri.parse(transformedURL));
delegate.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (delegate.resolveActivity(getPackageManager()) != null) {
startActivity(delegate);
finish();
}
} else {
forwardToBrowser(intent);
}
}
//YouTube URLs
else if (Arrays.asList(youtube_domains).contains(host)) { //Youtube URL
boolean invidious_enabled = sharedpreferences.getBoolean(SET_INVIDIOUS_ENABLED, true);

View File

@ -31,10 +31,6 @@ import static app.fedilab.nitterizeme.MainActivity.shortener_domains;
class Utils {
private static String urlRegex = "(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,10}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))";
static final Pattern urlPattern = Pattern.compile(
urlRegex,
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
private static final String[] UTM_PARAMS = {
"utm_\\w+",
"ga_source",
@ -63,6 +59,10 @@ class Utils {
"[\\w]+"
};
private static String urlRegex = "(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,10}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))";
static final Pattern urlPattern = Pattern.compile(
urlRegex,
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
/**
* Returns the unshortened URL
@ -92,7 +92,6 @@ class Utils {
if (matcher.find()) {
newURL = remove_tracking_param(matcher.group(1));
urls.add(newURL);
}
}
}
@ -144,8 +143,8 @@ class Utils {
url = url.replaceAll("&amp;" + utm + "=[0-9a-zA-Z._-]*", "");
url = url.replaceAll("&" + utm + "=[0-9a-zA-Z._-]*", "");
url = url.replaceAll("\\?" + utm + "=[0-9a-zA-Z._-]*", "?");
url = url.replaceAll("/" + utm + "="+ urlRegex, "/");
url = url.replaceAll("#" + utm + "="+ urlRegex, "");
url = url.replaceAll("/" + utm + "=" + urlRegex, "/");
url = url.replaceAll("#" + utm + "=" + urlRegex, "");
}
}
if (url != null && url.endsWith("?")) {