From b746594b1d8fd81297eea20aa5ff37a7cc961fb5 Mon Sep 17 00:00:00 2001 From: gdlbo <41114720+gdlbo@users.noreply.github.com> Date: Sat, 3 Sep 2022 23:18:12 +0300 Subject: [PATCH] some adblock refactoring --- .../ru/vtosters/lite/hooks/AdBlockHook.java | 351 ++++++++++++++++++ .../ru/vtosters/lite/hooks/JsonInjectors.java | 318 ---------------- .../lite/utils/NewsFeedFiltersUtils.java | 89 +++-- .../com/vk/api/discover/DiscoverGet.smali | 2 +- .../dto/feedlikes/FeedLikesResponse$b.smali | 2 +- .../vk/dto/stories/GeoNewsResponse$a1.smali | 2 +- .../stories/model/GetStoriesResponse.smali | 4 +- .../api/newsfeed/NewsfeedCustomGet$a.smali | 2 +- .../lite/api/newsfeed/NewsfeedGet.smali | 2 +- .../NewsfeedGetRecommendedLiveVideos.smali | 2 +- .../lite/api/newsfeed/NewsfeedSearch.smali | 4 +- .../com/vtosters/lite/api/wall/WallGet.smali | 2 +- .../vtosters/lite/api/wall/WallGetById.smali | 2 +- 13 files changed, 418 insertions(+), 364 deletions(-) create mode 100644 app/src/main/java/ru/vtosters/lite/hooks/AdBlockHook.java diff --git a/app/src/main/java/ru/vtosters/lite/hooks/AdBlockHook.java b/app/src/main/java/ru/vtosters/lite/hooks/AdBlockHook.java new file mode 100644 index 000000000..265112f55 --- /dev/null +++ b/app/src/main/java/ru/vtosters/lite/hooks/AdBlockHook.java @@ -0,0 +1,351 @@ +package ru.vtosters.lite.hooks; +import static ru.vtosters.lite.utils.NewsFeedFiltersUtils.checkCaption; +import static ru.vtosters.lite.utils.NewsFeedFiltersUtils.checkCopyright; +import static ru.vtosters.lite.utils.NewsFeedFiltersUtils.isBadNews; +import static ru.vtosters.lite.utils.Preferences.ads; +import static ru.vtosters.lite.utils.Preferences.adsgroup; +import static ru.vtosters.lite.utils.Preferences.adsstories; +import static ru.vtosters.lite.utils.Preferences.authorsrecomm; +import static ru.vtosters.lite.utils.Preferences.dev; +import static ru.vtosters.lite.utils.Preferences.friendsrecomm; +import static ru.vtosters.lite.utils.Preferences.postsrecomm; + +import android.util.Log; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.util.Collections; +import java.util.LinkedHashSet; + +import ru.vtosters.lite.utils.AndroidUtils; +import ru.vtosters.lite.utils.NewsFeedFiltersUtils; + +public class AdBlockHook{ + public static Boolean discoverAdBlock(JSONObject jsonObject){ + if (jsonObject == null) return true; + + try { + var type = jsonObject.optString("type"); + var post = jsonObject.optJSONObject("post"); + + if (jsonObject.optString("template").contains("ads") || jsonObject.has("ads")) { + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + (post != null ? post.optInt("post_id") : 0) + " from discover, Reason: ads"); + return false; + } + + if (isAds(post, type)) { + return false; + } + + if (authorsrecomm() && (type.equals("authors_rec") + || type.startsWith("recommended_") && (type.endsWith("audios") + || type.endsWith("artists") + || type.endsWith("playlists") + || type.endsWith("groups")))) { + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + (post != null ? post.optInt("post_id") : 0) + " from discover, Reason: authorsrecomm"); + return false; + } + + if (postsrecomm() && (type.equals("inline_user_rec") || type.equals("live_recommended"))) { + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + (post != null ? post.optInt("post_id") : 0) + " from discover, Reason: postsrecomm"); + return false; + } + + if (friendsrecomm() && (type.equals("user_rec") || type.equals("friends_recomm"))) { + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + (post != null ? post.optInt("post_id") : 0) + " from discover, Reason: friendsrecomm"); + return false; + } + + if (adsgroup() && (post != null ? post.optInt("marked_as_ads") : 0) == 1 && !isWhitelistedAd(post)) { + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + post.optInt("post_id") + " from discover, Reason: marked_as_ads is true"); + return false; + } + + if (post != null && isBadNews(post.optString("text")) && !isWhitelistedFilters(post)) { + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + post.optInt("post_id") + " from discover, Reason: text filters"); + return false; + } + + if (post != null && checkCopyright(post) && !isWhitelistedFilters(post)) { + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + post.optInt("post_id") + " from discover, Reason: copyright filters"); + return false; + } + + if (post != null && checkCaption(post) && !isWhitelistedFilters(post)) { + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + post.optInt("post_id") + " from discover, Reason: caption filters"); + return false; + } + + if (post != null && NewsFeedFiltersUtils.injectFiltersReposts(post) && !isWhitelistedFilters(post)) { + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + post.optInt("post_id") + " from discover, Reason: repost ad"); + return false; + } + } catch (Exception e) { + Log.d("NewsfeedAdBlockV2", "discover: " + e.getMessage()); + } + + return true; + } + + public static JSONObject discoverInject(JSONObject json) throws JSONException{ + var items = json.optJSONArray("items"); + var newObj = new JSONArray(); + +// newObj.put(discoverObj()); // template injection + + for (int i = 0; i < (items != null ? items.length() : 0); i++) { + try { + var curr = items.getJSONObject(i); + if (!curr.optString("template").contains("info") && discoverAdBlock(curr)) { + newObj.put(items.optJSONObject(i)); + } + } catch (Exception e) { + Log.d("NewsfeedAdBlockV2", "discover: " + e); + } + } + + json.put("items", newObj); + + return json; + } + + public static JSONObject discoverObj() throws JSONException{ + var mainjson = new JSONObject(); + + var info = new JSONObject(); + info.put("title", "Test Injection"); + info.put("text_color", "#ffffff"); + info.put("description", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat"); + + var image = new JSONArray(); + var imageQuality1 = new JSONObject(); + imageQuality1.put("url", "https://w7.pngwing.com/pngs/505/1021/png-transparent-konata-izumi-lucky-star-desktop-anime-anime-blue-mammal-face.png"); + imageQuality1.put("width", 72); // 72, 108, 144, 216, 288 + imageQuality1.put("height", 72); // 72, 108, 144, 216, 288 + image.put(imageQuality1); + + var background = new JSONArray(); + var backgroundQuality1 = new JSONObject(); + backgroundQuality1.put("url", "https://2ch.life/vg/arch/2022-08-22/src/38524739/16440606579870.jpg"); + backgroundQuality1.put("width", 344); // 344, 516, 680, 1032, 1376 + backgroundQuality1.put("height", 215); // 215, 323, 426, 645, 860 + background.put(backgroundQuality1); + + info.put("image", image); + info.put("background", background); + + mainjson.put("info", info); + mainjson.put("track_code", "info_banner_donut_catalog"); + mainjson.put("template", "info"); + + var action = new JSONObject(); + action.put("type", "open_url"); + action.put("url", "vk.com/vtosters_official"); + action.put("target", "internal"); + + mainjson.put("action", action); + + return mainjson; + } + + public static JSONArray feedInject(JSONArray items){ + if (items.length() == 0) return items; + + try { + var newItems = new JSONArray(); + + for (int j = 0; j < items.length(); j++) { + var list = items.optJSONObject(j); + + if (list == null) continue; + + var type = list.optString("type"); + + if (isAds(list, type)) { + continue; + } + + if (authorsrecomm() && (type.equals("authors_rec") + || type.startsWith("recommended_") && (type.endsWith("audios") + || type.endsWith("artists") + || type.endsWith("playlists") + || type.endsWith("groups")))) { + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: authorsrecomm"); + continue; + } + + if (postsrecomm() && (type.equals("inline_user_rec") || type.equals("live_recommended"))) { + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: postsrecomm"); + continue; + } + + if (friendsrecomm() && (type.equals("user_rec") || type.equals("friends_recomm"))) { + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: friendsrecomm"); + continue; + } + + if (adsgroup() && list.optInt("marked_as_ads") == 1 && !isWhitelistedAd(list)) { + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: marked_as_ads is true"); + continue; + } + + if (isBadNews(list.optString("text")) && !isWhitelistedFilters(list)) { + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: text filters"); + continue; + } + + if (checkCopyright(list) && !isWhitelistedFilters(list)) { + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: copyright filters"); + continue; + } + + if (checkCaption(list) && !isWhitelistedFilters(list)) { + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: caption filters"); + continue; + } + + if (NewsFeedFiltersUtils.injectFiltersReposts(list) && !isWhitelistedFilters(list)) { + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: repost ad"); + continue; + } + + newItems.put(list); + } + + return newItems; + } catch (Exception e) { + Log.d("NewsfeedAdBlockV2", e.getMessage()); + } + + return items; + } + + + public static JSONObject storiesads(JSONObject json, boolean isDeleteFix) throws JSONException{ + if (!adsstories()) { + return json; + } + + if (json.has("ads")) { + var ad = json.optJSONObject("ads"); + if (isDeleteFix) { + if (ad != null) { + ad.optJSONObject("settings") + .put("stories_interval", 0) + .put("authors_interval", 0) + .put("time_interval", 0) + .put("stories_init", 0) + .put("authors_init", 0) + .put("time_init", 0); + } + if (dev()) Log.d("StoriesAds", "Set ads settings at zero values"); + } else { + json.remove("ads"); + if (dev()) Log.d("StoriesAds", "Removed ads block"); + } + } + + if (!json.has("items")) return json; + + var items = json.optJSONArray("items"); + if (items != null) { + for (int i = 0; i < items.length(); i++) { + var item = items.optJSONObject(i); + if (item != null) { + parseStoriesItem(item); + } + } + } + + return json; + } + + private static void parseStoriesItem(JSONObject item) throws JSONException{ + var stories = item.optJSONArray("stories"); + var newStories = new JSONArray(); + + if (stories == null) return; + + for (int j = 0; j < stories.length(); j++) { + var story = stories.optJSONObject(j); + + if (!story.optBoolean("is_ads") && !story.optBoolean("is_promo")) { + newStories.put(story); + } else { + if (dev()) + Log.d("StoriesAds", "Fetched ad, owner id " + story.optString("owner_id") + ", caption " + story.optString("caption")); + } + } + + item.put("stories", newStories); + } + + public static Boolean isAds(JSONObject list, String type) throws JSONException{ + if (list == null || type == null || !ads()) return false; + + if (list.has("ads") + || type.equals("ads") + || type.equals("carousel") + || type.equals("html5_ad") + || type.equals("ads_easy_promote")) { + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: ads"); + return true; + } + + if (type.equals("promo_button") + || type.equals("app_widget") + || type.equals("app_video") + || type.equals("app_slider") + || type.equals("tags_suggestions")) { + if (dev()) + Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: promo"); + return true; + } + + return false; + } + + public static Boolean isWhitelistedFilters(JSONObject list){ + var id = String.valueOf(list.optInt("owner_id")); + + if (id.equals("0")) { + id = String.valueOf(list.optInt("source_id")); + } + + var whitelist = AndroidUtils.getDefaultPrefs().getStringSet("whitelisted_filters_groups", Collections.synchronizedSet(new LinkedHashSet<>())); + + return whitelist.contains(id); + } + + public static Boolean isWhitelistedAd(JSONObject list){ + var id = String.valueOf(list.optInt("owner_id")); + + if (id.equals("0")) { + id = String.valueOf(list.optInt("source_id")); + } + + var whitelist = AndroidUtils.getDefaultPrefs().getStringSet("whitelisted_ad_groups", Collections.synchronizedSet(new LinkedHashSet<>())); + + return whitelist.contains(id); + } +} diff --git a/app/src/main/java/ru/vtosters/lite/hooks/JsonInjectors.java b/app/src/main/java/ru/vtosters/lite/hooks/JsonInjectors.java index abcd0fc5a..06a89a509 100644 --- a/app/src/main/java/ru/vtosters/lite/hooks/JsonInjectors.java +++ b/app/src/main/java/ru/vtosters/lite/hooks/JsonInjectors.java @@ -204,138 +204,6 @@ public class JsonInjectors { return json; } - public static Boolean discoverAdBlock(JSONObject jsonObject) throws JSONException { - if (jsonObject == null) return true; - - try { - var type = jsonObject.optString("type"); - var post = jsonObject.optJSONObject("post"); - - if (jsonObject.optString("template").contains("ads") || jsonObject.has("ads")) { - if (dev()) - Log.d("NewsfeedAdBlockV2", "Removed post " + (post != null ? post.optInt("post_id") : 0) + " from discover, Reason: ads"); - return false; - } - - if (isAds(post, type)) { - return false; - } - - if (authorsrecomm() && (type.equals("authors_rec") - || type.startsWith("recommended_") && (type.endsWith("audios") - || type.endsWith("artists") - || type.endsWith("playlists") - || type.endsWith("groups")))) { - if (dev()) - Log.d("NewsfeedAdBlockV2", "Removed post " + (post != null ? post.optInt("post_id") : 0) + " from discover, Reason: authorsrecomm"); - return false; - } - - if (postsrecomm() && (type.equals("inline_user_rec") || type.equals("live_recommended"))) { - if (dev()) - Log.d("NewsfeedAdBlockV2", "Removed post " + (post != null ? post.optInt("post_id") : 0) + " from discover, Reason: postsrecomm"); - return false; - } - - if (friendsrecomm() && (type.equals("user_rec") || type.equals("friends_recomm"))) { - if (dev()) - Log.d("NewsfeedAdBlockV2", "Removed post " + (post != null ? post.optInt("post_id") : 0) + " from discover, Reason: friendsrecomm"); - return false; - } - - if (adsgroup() && (post != null ? post.optInt("marked_as_ads") : 0) == 1 && !isWhitelistedAd(post)) { - if (dev()) - Log.d("NewsfeedAdBlockV2", "Removed post " + post.optInt("post_id") + " from discover, Reason: marked_as_ads is true"); - return false; - } - - if (post != null && isBadNews(post.optString("text")) && !isWhitelistedFilters(post)) { - if (dev()) - Log.d("NewsfeedAdBlockV2", "Removed post " + post.optInt("post_id") + " from discover, Reason: text filters"); - return false; - } - - if (post != null && checkCopyright(post) && !isWhitelistedFilters(post)) { - if (dev()) - Log.d("NewsfeedAdBlockV2", "Removed post " + post.optInt("post_id") + " from discover, Reason: copyright filters"); - return false; - } - - if (post != null && checkCaption(post) && !isWhitelistedFilters(post)) { - if (dev()) - Log.d("NewsfeedAdBlockV2", "Removed post " + post.optInt("post_id") + " from discover, Reason: caption filters"); - return false; - } - - if (post != null && NewsFeedFiltersUtils.injectFiltersReposts(post) && !isWhitelistedFilters(post)) { - if (dev()) - Log.d("NewsfeedAdBlockV2", "Removed post " + post.optInt("post_id") + " from discover, Reason: repost ad"); - return false; - } - } catch (Exception e) { - Log.d("NewsfeedAdBlockV2", "discover: " + e.getMessage()); - } - - return true; - } - - public static JSONObject discoverInject(JSONObject json) throws JSONException{ - var items = json.optJSONArray("items"); - var newObj = new JSONArray(); - -// newObj.put(discoverObj()); // template injection - - for (int i = 0; i < (items != null ? items.length() : 0); i++) { - var curr = items.getJSONObject(i); - if (!curr.optString("template").contains("info") && discoverAdBlock(curr)){ - newObj.put(items.optJSONObject(i)); - } - } - - json.put("items", newObj); - - return json; - } - - public static JSONObject discoverObj() throws JSONException{ - var mainjson = new JSONObject(); - - var info = new JSONObject(); - info.put("title", "Test Injection"); - info.put("text_color", "#ffffff"); - info.put("description", "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat"); - - var image = new JSONArray(); - var imageQuality1 = new JSONObject(); - imageQuality1.put("url", "https://w7.pngwing.com/pngs/505/1021/png-transparent-konata-izumi-lucky-star-desktop-anime-anime-blue-mammal-face.png"); - imageQuality1.put("width", 72); // 72, 108, 144, 216, 288 - imageQuality1.put("height", 72); // 72, 108, 144, 216, 288 - image.put(imageQuality1); - - var background = new JSONArray(); - var backgroundQuality1 = new JSONObject(); - backgroundQuality1.put("url", "https://2ch.life/vg/arch/2022-08-22/src/38524739/16440606579870.jpg"); - backgroundQuality1.put("width", 344); // 344, 516, 680, 1032, 1376 - backgroundQuality1.put("height", 215); // 215, 323, 426, 645, 860 - background.put(backgroundQuality1); - - info.put("image", image); - info.put("background", background); - - mainjson.put("info", info); - mainjson.put("track_code", "info_banner_donut_catalog"); - mainjson.put("template", "info"); - - var action = new JSONObject(); - action.put("type", "open_url"); - action.put("url", "vk.com/vtosters_official"); - action.put("target", "internal"); - - mainjson.put("action", action); - - return mainjson; - } - public static JSONObject menu(JSONObject orig) throws JSONException { var Special = orig.optJSONArray("special"); var Main = orig.getJSONArray("main"); @@ -486,113 +354,6 @@ public class JsonInjectors { return profiles; } - - public static JSONObject storiesads(JSONObject json, boolean isDeleteFix) throws JSONException { - if (!adsstories()) { - return json; - } - - if (json.has("ads")) { - if (isDeleteFix) { - json.optJSONObject("ads").optJSONObject("settings") - .put("stories_interval", 0) - .put("authors_interval", 0) - .put("time_interval", 0) - .put("stories_init", 0) - .put("authors_init", 0) - .put("time_init", 0); - if (dev()) Log.d("StoriesAds", "Set ads settings at zero values"); - } else { - json.remove("ads"); - if (dev()) Log.d("StoriesAds", "Removed ads block"); - } - } - - if (!json.has("items")) return json; - - var items = json.optJSONArray("items"); - if (items != null) { - for (int i = 0; i < items.length(); i++) { - var item = items.optJSONObject(i); - if (item != null) { - parseStoriesItem(item); - } - } - } - - return json; - } - - private static void parseStoriesItem(JSONObject item) throws JSONException { - var stories = item.optJSONArray("stories"); - var newStories = new JSONArray(); - - if (stories == null) return; - - for (int j = 0; j < stories.length(); j++) { - var story = stories.optJSONObject(j); - - if (!story.optBoolean("is_ads") && !story.optBoolean("is_promo")) { - newStories.put(story); - } else { - if (dev()) - Log.d("StoriesAds", "Fetched ad, owner id " + story.optString("owner_id") + ", caption " + story.optString("caption")); - } - } - - item.put("stories", newStories); - } - - public static Boolean isAds(JSONObject list, String type) throws JSONException { - if (list == null || type == null || !ads()) return false; - - if (list.has("ads") - || type.equals("ads") - || type.equals("carousel") - || type.equals("html5_ad") - || type.equals("ads_easy_promote")) { - if (dev()) - Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: ads"); - return true; - } - - if (type.equals("promo_button") - || type.equals("app_widget") - || type.equals("app_video") - || type.equals("app_slider") - || type.equals("tags_suggestions")) { - if (dev()) - Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: promo"); - return true; - } - - return false; - } - - public static Boolean isWhitelistedFilters(JSONObject list){ - var id = String.valueOf(list.optInt("owner_id")); - - if (id.equals("0")) { - id = String.valueOf(list.optInt("source_id")); - } - - var whitelist = AndroidUtils.getDefaultPrefs().getStringSet("whitelisted_filters_groups", Collections.synchronizedSet(new LinkedHashSet<>())); - - return whitelist.contains(id); - } - - public static Boolean isWhitelistedAd(JSONObject list){ - var id = String.valueOf(list.optInt("owner_id")); - - if (id.equals("0")) { - id = String.valueOf(list.optInt("source_id")); - } - - var whitelist = AndroidUtils.getDefaultPrefs().getStringSet("whitelisted_ad_groups", Collections.synchronizedSet(new LinkedHashSet<>())); - - return whitelist.contains(id); - } - public static JSONArray newsfeedlist(JSONArray items) throws JSONException { var selectedItems = getDefaultPrefs().getString("news_feed_selected_items", ""); var filtersSet = getDefaultPrefs().getStringSet("news_feed_items_set", null); @@ -630,84 +391,6 @@ public class JsonInjectors { return items; } - public static JSONArray newsfeedadtest(JSONArray items) throws JSONException { - try { - var newItems = new JSONArray(); - - for (int j = 0; j < items.length(); j++) { - var list = items.optJSONObject(j); - - if (list == null) continue; - - var type = list.optString("type"); - - if (isAds(list, type)) { - continue; - } - - if (authorsrecomm() && (type.equals("authors_rec") - || type.startsWith("recommended_") && (type.endsWith("audios") - || type.endsWith("artists") - || type.endsWith("playlists") - || type.endsWith("groups")))) { - if (dev()) - Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: authorsrecomm"); - continue; - } - - if (postsrecomm() && (type.equals("inline_user_rec") || type.equals("live_recommended"))) { - if (dev()) - Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: postsrecomm"); - continue; - } - - if (friendsrecomm() && (type.equals("user_rec") || type.equals("friends_recomm"))) { - if (dev()) - Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: friendsrecomm"); - continue; - } - - if (adsgroup() && list.optInt("marked_as_ads") == 1 && !isWhitelistedAd(list)) { - if (dev()) - Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: marked_as_ads is true"); - continue; - } - - if (isBadNews(list.optString("text")) && !isWhitelistedFilters(list)) { - if (dev()) - Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: text filters"); - continue; - } - - if (checkCopyright(list) && !isWhitelistedFilters(list)) { - if (dev()) - Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: copyright filters"); - continue; - } - - if (checkCaption(list) && !isWhitelistedFilters(list)) { - if (dev()) - Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: caption filters"); - continue; - } - - if (NewsFeedFiltersUtils.injectFiltersReposts(list) && !isWhitelistedFilters(list)) { - if (dev()) - Log.d("NewsfeedAdBlockV2", "Removed post " + list.optInt("post_id") + " from feed, Reason: repost ad"); - continue; - } - - newItems.put(list); - } - - return newItems; - } catch (Exception e) { - Log.d("NewsfeedAdBlockV2", e.getMessage()); - } - - return items; - } - public static JSONObject music(JSONObject json) throws JSONException { var catalog = json.optJSONObject("catalog"); @@ -905,7 +588,6 @@ public class JsonInjectors { return json; } - public static boolean haveDonateButton() { int randomshower = new Random().nextInt(6); diff --git a/app/src/main/java/ru/vtosters/lite/utils/NewsFeedFiltersUtils.java b/app/src/main/java/ru/vtosters/lite/utils/NewsFeedFiltersUtils.java index 2f836783c..350aa2139 100644 --- a/app/src/main/java/ru/vtosters/lite/utils/NewsFeedFiltersUtils.java +++ b/app/src/main/java/ru/vtosters/lite/utils/NewsFeedFiltersUtils.java @@ -23,7 +23,7 @@ import java.util.Arrays; import java.util.List; import java.util.Scanner; -import ru.vtosters.lite.hooks.JsonInjectors; +import ru.vtosters.lite.hooks.AdBlockHook; public class NewsFeedFiltersUtils { public static List mFilters; @@ -58,16 +58,22 @@ public class NewsFeedFiltersUtils { } public static void getFilter(String boolname, String filename, List list) { + var ctx = getGlobalContext(); + if (getBoolValue(boolname, false)) { try { - var scanner = new Scanner(getGlobalContext().getAssets().open(filename)); - while (scanner.hasNextLine()) { - var line = scanner.nextLine(); - if (!line.isEmpty()) - list.add(line.toLowerCase()); + Scanner scanner; + if (ctx != null) { + scanner = new Scanner(ctx.getAssets().open(filename)); + + while (scanner.hasNextLine()) { + var line = scanner.nextLine(); + if (!line.isEmpty()) + list.add(line.toLowerCase()); + } } } catch (IOException e) { - e.printStackTrace(); + Log.d("NewsFeedFiltersUtils", e.getMessage()); } } } // Get needed filter list from assets @@ -75,25 +81,27 @@ public class NewsFeedFiltersUtils { public static boolean injectFiltersReposts(JSONObject obj) throws JSONException { if (getBoolValue("cringerepost", false) && obj.has("copy_history")) { var copyHistoryNode = obj.optJSONArray("copy_history"); - for (int i = 0; i < copyHistoryNode.length(); i++) { - var item = copyHistoryNode.optJSONObject(i); - var text = item.optString("text"); + if (copyHistoryNode != null) { + for (int i = 0; i < copyHistoryNode.length(); i++) { + var item = copyHistoryNode.optJSONObject(i); + var text = item.optString("text"); - if (isBadNews(text)) { - if (dev()) - Log.d("RepostFilter", "Fetched repost ad (isBadNews), owner id " + item.optString("owner_id") + ", text: " + text); - return true; - } else if (JsonInjectors.isAds(item, item.optString("post_type"))) { - if (dev()) - Log.d("RepostFilter", "Fetched repost ad (ads), owner id " + item.optString("owner_id") + ", text: " + text); - return true; - } - - for (String filter : mFiltersLinks) { - if (text.contains(filter)) { + if (isBadNews(text)) { if (dev()) - Log.d("RepostFilter", "Fetched repost ad, owner id " + item.optString("owner_id") + ", text: " + text); + Log.d("RepostFilter", "Fetched repost ad (isBadNews), owner id " + item.optString("owner_id") + ", text: " + text); return true; + } else if (AdBlockHook.isAds(item, item.optString("post_type"))) { + if (dev()) + Log.d("RepostFilter", "Fetched repost ad (ads), owner id " + item.optString("owner_id") + ", text: " + text); + return true; + } + + for (String filter : mFiltersLinks) { + if (text.contains(filter)) { + if (dev()) + Log.d("RepostFilter", "Fetched repost ad, owner id " + item.optString("owner_id") + ", text: " + text); + return true; + } } } } @@ -106,15 +114,25 @@ public class NewsFeedFiltersUtils { if (copyright_post()) return true; var copyright = json.optJSONObject("copyright"); - var copyrightName = copyright.getString("name").toLowerCase(); - var copyrightLink = copyright.getString("link").toLowerCase(); - + + String copyrightName = null; + + if (copyright != null) { + copyrightName = copyright.getString("name").toLowerCase(); + } + + String copyrightLink = null; + + if (copyright != null) { + copyrightLink = copyright.getString("link").toLowerCase(); + } + for (String filter : mFiltersNames) { - if (copyrightName.contains(filter)) return true; + if (copyrightName != null && copyrightName.contains(filter)) return true; } for (String filter : mFiltersLinks) { - if (copyrightLink.contains(filter)) return true; + if (copyrightLink != null && copyrightLink.contains(filter)) return true; } } return false; @@ -154,12 +172,15 @@ public class NewsFeedFiltersUtils { var caption = postJson.optJSONObject("caption"); if (postsrecomm()) { try { - var type = caption.getString("type"); - return type.equals("explorebait") || // Может быть интересно - type.equals("shared") || // Поделился записью - type.equals("digest") || // Рекомедации - type.equals("commented") || // Комментирует - type.equals("voted"); // Проголосовал в опросе + String type; + if (caption != null) { + type = caption.getString("type"); + return type.equals("explorebait") || // Может быть интересно + type.equals("shared") || // Поделился записью + type.equals("digest") || // Рекомедации + type.equals("commented") || // Комментирует + type.equals("voted"); // Проголосовал в опросе + } } catch (JSONException e) { Log.d("checkCaption", "Caption error"); } diff --git a/smali/smali_classes2/com/vk/api/discover/DiscoverGet.smali b/smali/smali_classes2/com/vk/api/discover/DiscoverGet.smali index 148e5a8d4..c61a0f6f3 100644 --- a/smali/smali_classes2/com/vk/api/discover/DiscoverGet.smali +++ b/smali/smali_classes2/com/vk/api/discover/DiscoverGet.smali @@ -89,7 +89,7 @@ move-result-object v1 - invoke-static {v1}, Lru/vtosters/lite/hooks/JsonInjectors;->discoverInject(Lorg/json/JSONObject;)Lorg/json/JSONObject; + invoke-static {v1}, Lru/vtosters/lite/hooks/AdBlockHook;->discoverInject(Lorg/json/JSONObject;)Lorg/json/JSONObject; move-result-object v1 diff --git a/smali/smali_classes2/com/vk/dto/feedlikes/FeedLikesResponse$b.smali b/smali/smali_classes2/com/vk/dto/feedlikes/FeedLikesResponse$b.smali index b2d6548c2..ea7a594d0 100644 --- a/smali/smali_classes2/com/vk/dto/feedlikes/FeedLikesResponse$b.smali +++ b/smali/smali_classes2/com/vk/dto/feedlikes/FeedLikesResponse$b.smali @@ -171,7 +171,7 @@ .line 14 :cond_4 - invoke-static {v4}, Lru/vtosters/lite/hooks/JsonInjectors;->newsfeedadtest(Lorg/json/JSONArray;)Lorg/json/JSONArray; + invoke-static {v4}, Lru/vtosters/lite/hooks/AdBlockHook;->feedInject(Lorg/json/JSONArray;)Lorg/json/JSONArray; move-result-object v4 diff --git a/smali/smali_classes2/com/vk/dto/stories/GeoNewsResponse$a1.smali b/smali/smali_classes2/com/vk/dto/stories/GeoNewsResponse$a1.smali index 66dae7280..a5c8559d4 100644 --- a/smali/smali_classes2/com/vk/dto/stories/GeoNewsResponse$a1.smali +++ b/smali/smali_classes2/com/vk/dto/stories/GeoNewsResponse$a1.smali @@ -235,7 +235,7 @@ .line 20 :cond_5 - invoke-static {v11}, Lru/vtosters/lite/hooks/JsonInjectors;->newsfeedadtest(Lorg/json/JSONArray;)Lorg/json/JSONArray; + invoke-static {v11}, Lru/vtosters/lite/hooks/AdBlockHook;->feedInject(Lorg/json/JSONArray;)Lorg/json/JSONArray; move-result-object v11 diff --git a/smali/smali_classes2/com/vk/dto/stories/model/GetStoriesResponse.smali b/smali/smali_classes2/com/vk/dto/stories/model/GetStoriesResponse.smali index e92282918..6f4d03c4a 100644 --- a/smali/smali_classes2/com/vk/dto/stories/model/GetStoriesResponse.smali +++ b/smali/smali_classes2/com/vk/dto/stories/model/GetStoriesResponse.smali @@ -208,7 +208,7 @@ .method public constructor (Lorg/json/JSONObject;Z)V .locals 7 - invoke-static {p1, p2}, Lru/vtosters/lite/hooks/JsonInjectors;->storiesads(Lorg/json/JSONObject;Z)Lorg/json/JSONObject; + invoke-static {p1, p2}, Lru/vtosters/lite/hooks/AdBlockHook;->storiesads(Lorg/json/JSONObject;Z)Lorg/json/JSONObject; move-result-object p1 @@ -398,7 +398,7 @@ const v0, 0x0 - invoke-static {p1, v0}, Lru/vtosters/lite/hooks/JsonInjectors;->storiesads(Lorg/json/JSONObject;Z)Lorg/json/JSONObject; + invoke-static {p1, v0}, Lru/vtosters/lite/hooks/AdBlockHook;->storiesads(Lorg/json/JSONObject;Z)Lorg/json/JSONObject; move-result-object p1 diff --git a/smali/smali_classes4/com/vtosters/lite/api/newsfeed/NewsfeedCustomGet$a.smali b/smali/smali_classes4/com/vtosters/lite/api/newsfeed/NewsfeedCustomGet$a.smali index 1f836814e..1a67b757a 100644 --- a/smali/smali_classes4/com/vtosters/lite/api/newsfeed/NewsfeedCustomGet$a.smali +++ b/smali/smali_classes4/com/vtosters/lite/api/newsfeed/NewsfeedCustomGet$a.smali @@ -188,7 +188,7 @@ .line 13 :cond_2 - invoke-static {p2}, Lru/vtosters/lite/hooks/JsonInjectors;->newsfeedadtest(Lorg/json/JSONArray;)Lorg/json/JSONArray; + invoke-static {p2}, Lru/vtosters/lite/hooks/AdBlockHook;->feedInject(Lorg/json/JSONArray;)Lorg/json/JSONArray; move-result-object p2 diff --git a/smali/smali_classes4/com/vtosters/lite/api/newsfeed/NewsfeedGet.smali b/smali/smali_classes4/com/vtosters/lite/api/newsfeed/NewsfeedGet.smali index b67cb44f1..7dce91e18 100644 --- a/smali/smali_classes4/com/vtosters/lite/api/newsfeed/NewsfeedGet.smali +++ b/smali/smali_classes4/com/vtosters/lite/api/newsfeed/NewsfeedGet.smali @@ -734,7 +734,7 @@ .line 37 :cond_2 :goto_2 - invoke-static {v1}, Lru/vtosters/lite/hooks/JsonInjectors;->newsfeedadtest(Lorg/json/JSONArray;)Lorg/json/JSONArray; + invoke-static {v1}, Lru/vtosters/lite/hooks/AdBlockHook;->feedInject(Lorg/json/JSONArray;)Lorg/json/JSONArray; move-result-object v1 diff --git a/smali/smali_classes4/com/vtosters/lite/api/newsfeed/NewsfeedGetRecommendedLiveVideos.smali b/smali/smali_classes4/com/vtosters/lite/api/newsfeed/NewsfeedGetRecommendedLiveVideos.smali index a5e81c880..d6e548c71 100644 --- a/smali/smali_classes4/com/vtosters/lite/api/newsfeed/NewsfeedGetRecommendedLiveVideos.smali +++ b/smali/smali_classes4/com/vtosters/lite/api/newsfeed/NewsfeedGetRecommendedLiveVideos.smali @@ -139,7 +139,7 @@ move-result-object v1 - invoke-static {v1}, Lru/vtosters/lite/hooks/JsonInjectors;->newsfeedadtest(Lorg/json/JSONArray;)Lorg/json/JSONArray; + invoke-static {v1}, Lru/vtosters/lite/hooks/AdBlockHook;->feedInject(Lorg/json/JSONArray;)Lorg/json/JSONArray; move-result-object v1 diff --git a/smali/smali_classes4/com/vtosters/lite/api/newsfeed/NewsfeedSearch.smali b/smali/smali_classes4/com/vtosters/lite/api/newsfeed/NewsfeedSearch.smali index 04d17f321..5e4c9e56c 100644 --- a/smali/smali_classes4/com/vtosters/lite/api/newsfeed/NewsfeedSearch.smali +++ b/smali/smali_classes4/com/vtosters/lite/api/newsfeed/NewsfeedSearch.smali @@ -365,7 +365,7 @@ .line 14 :goto_2 - invoke-static {v4}, Lru/vtosters/lite/hooks/JsonInjectors;->newsfeedadtest(Lorg/json/JSONArray;)Lorg/json/JSONArray; + invoke-static {v4}, Lru/vtosters/lite/hooks/AdBlockHook;->feedInject(Lorg/json/JSONArray;)Lorg/json/JSONArray; move-result-object v4 @@ -400,7 +400,7 @@ .line 17 :goto_3 - invoke-static {v2}, Lru/vtosters/lite/hooks/JsonInjectors;->newsfeedadtest(Lorg/json/JSONArray;)Lorg/json/JSONArray; + invoke-static {v2}, Lru/vtosters/lite/hooks/AdBlockHook;->feedInject(Lorg/json/JSONArray;)Lorg/json/JSONArray; move-result-object v2 diff --git a/smali/smali_classes4/com/vtosters/lite/api/wall/WallGet.smali b/smali/smali_classes4/com/vtosters/lite/api/wall/WallGet.smali index ab842690f..bda6d0539 100644 --- a/smali/smali_classes4/com/vtosters/lite/api/wall/WallGet.smali +++ b/smali/smali_classes4/com/vtosters/lite/api/wall/WallGet.smali @@ -524,7 +524,7 @@ .line 19 :goto_2 - invoke-static {p1}, Lru/vtosters/lite/hooks/JsonInjectors;->newsfeedadtest(Lorg/json/JSONArray;)Lorg/json/JSONArray; + invoke-static {p1}, Lru/vtosters/lite/hooks/AdBlockHook;->feedInject(Lorg/json/JSONArray;)Lorg/json/JSONArray; move-result-object p1 diff --git a/smali/smali_classes4/com/vtosters/lite/api/wall/WallGetById.smali b/smali/smali_classes4/com/vtosters/lite/api/wall/WallGetById.smali index 5b83963ae..3d0c85e88 100644 --- a/smali/smali_classes4/com/vtosters/lite/api/wall/WallGetById.smali +++ b/smali/smali_classes4/com/vtosters/lite/api/wall/WallGetById.smali @@ -75,7 +75,7 @@ } .end annotation - invoke-static {p2}, Lru/vtosters/lite/hooks/JsonInjectors;->newsfeedadtest(Lorg/json/JSONArray;)Lorg/json/JSONArray; + invoke-static {p2}, Lru/vtosters/lite/hooks/AdBlockHook;->feedInject(Lorg/json/JSONArray;)Lorg/json/JSONArray; move-result-object p2