Added Jsoup helper

pull/213/head
M66B 1 year ago
parent 8b95df4128
commit 446a611e57

@ -6012,6 +6012,9 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
popupMenu.getMenu().findItem(R.id.menu_thread_info)
.setVisible(BuildConfig.TEST_RELEASE || BuildConfig.DEBUG || debug);
popupMenu.getMenu().findItem(R.id.menu_jsoup)
.setVisible(BuildConfig.DEBUG || debug)
.setEnabled(message.content);
popupMenu.getMenu().findItem(R.id.menu_resync)
.setEnabled(message.uid != null ||
@ -6131,6 +6134,9 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
} else if (itemId == R.id.menu_thread_info) {
onMenuThreadInfo(message);
return true;
} else if (itemId == R.id.menu_jsoup) {
onMenuJsoup(message);
return true;
} else if (itemId == R.id.menu_reset_questions) {
onMenuResetQuestions(message);
return true;
@ -6633,6 +6639,73 @@ public class AdapterMessage extends RecyclerView.Adapter<AdapterMessage.ViewHold
}
private void onMenuJsoup(TupleMessageEx message) {
if (!tvBody.hasSelection())
return;
Bundle args = new Bundle();
args.putLong("id", message.id);
args.putString("selected",
tvBody.getText().subSequence(tvBody.getSelectionStart(), tvBody.getSelectionEnd()).toString());
new SimpleTask<List<String>>() {
@Override
protected List<String> onExecute(Context context, Bundle args) throws IOException {
long id = args.getLong("id");
String selected = args.getString("selected");
Map<String, EntityMessage> map = new HashMap<>();
DB db = DB.getInstance(context);
EntityMessage message = db.message().getMessage(id);
if (message == null || !message.content)
return null;
Document d = JsoupEx.parse(message.getFile(context));
List<String> result = new ArrayList<>();
for (Element element : d.select(String.format("*:containsOwn(%s)", selected))) {
List<String> path = new ArrayList<>();
Element e = element;
while (e != null && e.parent() != null /* skip #root */) {
int index = 0;
for (Element p : e.previousElementSiblings())
if (Objects.equals(e.tagName(), p.tagName()))
index++;
path.add(String.format("%s:eq(%d)", e.tagName(), index));
e = e.parent();
}
Collections.reverse(path);
result.add(TextUtils.join(" > ", path));
}
return result;
}
@Override
protected void onExecuted(Bundle args, List<String> jsoup) {
if (jsoup != null && jsoup.size() > 0) {
ClipboardManager clipboard = Helper.getSystemService(context, ClipboardManager.class);
if (clipboard == null)
return;
ClipData clip = ClipData.newPlainText(
context.getString(R.string.app_name),
jsoup.get(0));
clipboard.setPrimaryClip(clip);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU)
ToastEx.makeText(context, jsoup.get(0), Toast.LENGTH_LONG).show();
}
}
@Override
protected void onException(Bundle args, Throwable ex) {
Log.unexpectedError(parentFragment.getParentFragmentManager(), ex);
}
}.execute(context, owner, args, "message:jsoup");
}
private void onMenuResetQuestions(TupleMessageEx message) {
Bundle args = new Bundle();
args.putLong("id", message.id);

@ -170,6 +170,11 @@
android:icon="@drawable/baseline_mail_more_24"
android:title="@string/title_thread_info" />
<item
android:id="@+id/menu_jsoup"
android:icon="@drawable/twotone_code_24"
android:title="@string/title_jsoup" />
<item
android:id="@+id/menu_reset_questions"
android:icon="@drawable/twotone_restart_alt_24"

@ -1724,6 +1724,7 @@
<string name="title_encrypt">Encrypt</string>
<string name="title_decrypt">Decrypt</string>
<string name="title_thread_info" translatable="false">Thread info</string>
<string name="title_jsoup" translatable="false">Jsoup</string>
<string name="title_reset_message_questions">Reset link questions</string>
<string name="title_resync">Resync</string>
<string name="title_charset">Encoding</string>

Loading…
Cancel
Save