Skip to content

Commit

Permalink
Support Zhihu 9.21.0
Browse files Browse the repository at this point in the history
  • Loading branch information
shatyuka committed Oct 5, 2023
1 parent b12cd18 commit bcf49f1
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 11 deletions.
5 changes: 4 additions & 1 deletion app/src/main/java/com/shatyuka/zhiliao/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Comparator;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

Expand Down Expand Up @@ -230,7 +231,9 @@ public static Method getMethodByParameterTypes(Class<?> clazz, Class<?>... param
public static Method getMethodByParameterTypes(Class<?> clazz, int skip, Class<?>... parameterTypes) {
if (clazz == null) return null;
int count = 0;
for (Method method : clazz.getDeclaredMethods()) {
Method[] methods = clazz.getDeclaredMethods();
Arrays.sort(methods, Comparator.comparing(Method::getName));
for (Method method : methods) {
Class<?>[] types = method.getParameterTypes();
if (Arrays.equals(types, parameterTypes)) {
if (count < skip) {
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/java/com/shatyuka/zhiliao/hooks/Horizontal.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,13 @@ public void init(ClassLoader classLoader) throws Throwable {
if (UserAction == null)
throw new ClassNotFoundException("com.zhihu.android.bootstrap.vertical_pager.UserAction");

nextAnswer = Helper.getMethodByParameterTypes(VerticalPagerContainer, UserAction);
lastAnswer = Helper.getMethodByParameterTypes(VerticalPagerContainer, 1, UserAction);
try {
nextAnswer = VerticalPagerContainer.getMethod("a", UserAction);
lastAnswer = VerticalPagerContainer.getMethod("b", UserAction);
} catch (NoSuchMethodException e) {
nextAnswer = Helper.getMethodByParameterTypes(VerticalPagerContainer, 1, UserAction);
lastAnswer = Helper.getMethodByParameterTypes(VerticalPagerContainer, 0, UserAction);
}

UserAction_DRAG_UP = UserAction.getField("DRAG_UP");
UserAction_DRAG_DOWN = UserAction.getField("DRAG_DOWN");
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/shatyuka/zhiliao/hooks/NavButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.lang.reflect.Method;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;

public class NavButton implements IHook {
Expand Down Expand Up @@ -52,7 +53,7 @@ public void init(ClassLoader classLoader) throws Throwable {
@Override
public void hook() throws Throwable {
if (Helper.prefs.getBoolean("switch_mainswitch", false) && (Helper.prefs.getBoolean("switch_vipnav", false) || Helper.prefs.getBoolean("switch_videonav", false)|| Helper.prefs.getBoolean("switch_friendnav", false) || Helper.prefs.getBoolean("switch_panelnav", false))) {
XposedHelpers.findAndHookMethod(BottomNavMenuView, "a", IMenuItem, new XC_MethodHook() {
XposedBridge.hookMethod(Helper.getMethodByParameterTypes(BottomNavMenuView, IMenuItem), new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
if (("market".equals(getItemId.invoke(param.args[0])) && Helper.prefs.getBoolean("switch_vipnav", false)) ||
Expand Down
49 changes: 45 additions & 4 deletions app/src/main/java/com/shatyuka/zhiliao/hooks/NextAnswer.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,23 @@

import com.shatyuka.zhiliao.Helper;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XC_MethodReplacement;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;

public class NextAnswer implements IHook {
static Class<?> NextContentAnimationView;
static Class<?> NextContentAnimationView_short;
static Class<?> MixShortContainerFragment;
static Class<?> MixShortEntitySupport;

static Method initView;
static Method initLayout;

static Field nextButton;

@Override
public String getName() {
Expand All @@ -27,18 +35,51 @@ public void init(ClassLoader classLoader) throws Throwable {
try {
NextContentAnimationView_short = classLoader.loadClass("com.zhihu.android.mixshortcontainer.function.next.NextContentAnimationView");
MixShortContainerFragment = classLoader.loadClass("com.zhihu.android.mixshortcontainer.MixShortContainerFragment");
MixShortEntitySupport = classLoader.loadClass("com.zhihu.android.mixshortcontainer.support.MixShortEntitySupport");
} catch (Throwable ignored) {
}

initView = Helper.getMethodByParameterTypes(MixShortContainerFragment, 0, View.class);
initLayout = Helper.getMethodByParameterTypes(MixShortContainerFragment, 1, View.class);

if (MixShortContainerFragment != null) {
String[] nextButton_names = new String[]{"t", "f", "e"};
for (String name : nextButton_names) {
try {
Field field = MixShortContainerFragment.getDeclaredField(name);
if (field.getType().getName().equals("com.zhihu.android.base.widget.ZHFrameLayout")) {
nextButton = field;
break;
}
} catch (NoSuchFieldException ignore) {
}
}
if (nextButton == null) {
throw new NoSuchFieldException("nextButton");
}
nextButton.setAccessible(true);
}
}
}

@Override
public void hook() throws Throwable {
if (Helper.prefs.getBoolean("switch_mainswitch", false) && Helper.prefs.getBoolean("switch_nextanswer", false)) {
XposedHelpers.findAndHookMethod(Helper.AnswerPagerFragment, "setupNextAnswerBtn", XC_MethodReplacement.returnConstant(null));
if (MixShortContainerFragment != null && MixShortEntitySupport != null) {
XposedHelpers.findAndHookMethod(MixShortContainerFragment, "a", MixShortEntitySupport, XC_MethodReplacement.returnConstant(null));
if (initView != null) {
XposedBridge.hookMethod(initView, new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
nextButton.set(param.thisObject, null);
}
});
}
if (initLayout != null) {
XposedBridge.hookMethod(initLayout, new XC_MethodHook() {
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
nextButton.set(param.thisObject, null);
}
});
}

if (Helper.packageInfo.versionCode > 2614) {
Expand Down
12 changes: 11 additions & 1 deletion app/src/main/java/com/shatyuka/zhiliao/hooks/Tag.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@
import com.shatyuka.zhiliao.R;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;

public class Tag implements IHook {
Expand All @@ -25,6 +27,8 @@ public class Tag implements IHook {
static Class<?> SugarHolder;
static Class<?> TemplateRoot;

static Method onBindData;

static Field ViewHolder_itemView;
static Field SugarHolder_mData;
static Field TemplateRoot_unique;
Expand Down Expand Up @@ -53,6 +57,12 @@ public void init(ClassLoader classLoader) throws Throwable {
}
SugarHolder_mData.setAccessible(true);
TemplateRoot_unique = TemplateRoot.getField("unique");

try {
onBindData = BaseTemplateNewFeedHolder.getMethod("onBindData", Object.class);
} catch (NoSuchMethodException e) {
onBindData = BaseTemplateNewFeedHolder.getDeclaredMethod("a", TemplateFeed);
}
}

@SuppressLint("DiscouragedApi")
Expand All @@ -62,7 +72,7 @@ public void hook() throws Throwable {
id_author = Helper.context.getResources().getIdentifier("author", "id", MainHook.hookPackage);

if (Helper.prefs.getBoolean("switch_mainswitch", false) && Helper.prefs.getBoolean("switch_tag", false)) {
XposedHelpers.findAndHookMethod(BaseTemplateNewFeedHolder, "a", TemplateFeed, new XC_MethodHook() {
XposedBridge.hookMethod(onBindData, new XC_MethodHook() {
@SuppressLint({"ResourceType", "SetTextI18n"})
@Override
protected void afterHookedMethod(MethodHookParam param) throws Throwable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public class ZhihuPreference implements IHook {
static String getLayoutResId_MethodName;
static String getResourceId_MethodName;
static String onCreate_MethodName;
static String onPreferenceClick_MethodName;

@Override
public String getName() {
Expand Down Expand Up @@ -203,6 +204,7 @@ public void init(ClassLoader classLoader) throws Throwable {
}

ZHIntent_ctor = ZHIntent.getConstructor(Class.class, Bundle.class, String.class, Array.newInstance(PageInfoType, 0).getClass());
onPreferenceClick_MethodName = Helper.getMethodByParameterTypes(OnPreferenceClickListener, Preference).getName();
}

@Override
Expand Down Expand Up @@ -305,7 +307,7 @@ protected void afterHookedMethod(MethodHookParam param) throws Throwable {
}
});

XposedHelpers.findAndHookMethod(SettingsFragment, "onPreferenceClick", Preference, new XC_MethodHook() {
XposedHelpers.findAndHookMethod(SettingsFragment, onPreferenceClick_MethodName, Preference, new XC_MethodHook() {
@Override
protected void beforeHookedMethod(MethodHookParam param) throws Throwable {
if (param.args[0] == preference_zhiliao) {
Expand Down Expand Up @@ -514,7 +516,7 @@ protected Object replaceHookedMethod(MethodHookParam param) throws Throwable {
return null;
}
});
XposedHelpers.findAndHookMethod(DebugFragment, "onPreferenceClick", "androidx.preference.Preference", new XC_MethodReplacement() {
XposedHelpers.findAndHookMethod(DebugFragment, onPreferenceClick_MethodName, Preference, new XC_MethodReplacement() {
@Override
protected Object replaceHookedMethod(MethodHookParam param) throws Throwable {
Object preference = param.args[0];
Expand Down

0 comments on commit bcf49f1

Please sign in to comment.