Skip to content

Commit

Permalink
No shortened links when selecting text!
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonHalvdansson committed Nov 24, 2024
1 parent 3992f64 commit d2a1746
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ static public String getHTML() {
"- Changed Algolia search parameters (thanks Jonas Wunderlich)<br>" +
"- Updated libraries and targeting Android 15<br>" +
"- Fixed post title/domain filtering for Algolia API<br>" +
"- Made links unshortened when selecting text<br>" +
"<br>" +
"<b>Version 2.0.3:</b><br>" +
"- Removed Nitter integration (RIP)<br>" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.simon.harmonichackernews.R;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.parser.Parser;
import org.jsoup.select.Elements;
import org.sufficientlysecure.htmltextview.HtmlTextView;
import org.sufficientlysecure.htmltextview.OnClickATagListener;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DialogUtils {

public static void showTextSelectionDialog(Context ctx, String text) {
Expand All @@ -21,7 +29,8 @@ public static void showTextSelectionDialog(Context ctx, String text) {
selectTextDialogBuilder.setView(rootView);

HtmlTextView htmlTextView = rootView.findViewById(R.id.select_text_htmltextview);
htmlTextView.setHtml(text);

htmlTextView.setHtml(undoShortenedLinks(text));

htmlTextView.setOnClickATagListener(new OnClickATagListener() {
@Override
Expand All @@ -36,4 +45,38 @@ public boolean onClick(View widget, String spannedText, @Nullable String href) {
selectTextDialog.show();
}

//made by chatgpt obviously
public static String undoShortenedLinks(String inputHtml) {
// Parse the HTML content. Use Parser.htmlParser() to ensure it parses as HTML.
Document document = Jsoup.parse(inputHtml, "", Parser.htmlParser());

// Select all <a> elements
Elements links = document.select("a[href]");

for (Element link : links) {
String href = link.attr("href");
String linkText = link.text();

// Decode HTML entities in href and linkText
String decodedHref = Jsoup.parse(href).text();
String decodedLinkText = Jsoup.parse(linkText).text();

// Check if link text ends with "..."
if (decodedLinkText.endsWith("...")) {
// Extract the prefix of the link text without "..."
String linkTextPrefix = decodedLinkText.substring(0, decodedLinkText.length() - 3);

// Check if the href starts with the link text prefix
if (decodedHref.startsWith(linkTextPrefix)) {
// Replace the link text with the full href
link.text(decodedHref);
}
}
}

// Return the modified HTML as a string
// To preserve the original HTML structure, extract the body’s inner HTML
return document.body().html();
}

}

0 comments on commit d2a1746

Please sign in to comment.