| 340 | } |
| 341 | |
| 342 | private static boolean omitParam(String remove, String key, String value) { |
| 343 | // https://adguard.com/kb/general/ad-filtering/create-own-filters/#removeparam-modifier |
| 344 | |
| 345 | if ("".equals(remove)) { |
| 346 | // Specify naked $removeparam to remove all query parameters |
| 347 | return true; |
| 348 | } |
| 349 | |
| 350 | // $removeparam=~param — removes all query parameters with the name different from param. |
| 351 | // $removeparam=~/regexp/ — removes all query parameters that do not match the regexp regular expression. |
| 352 | boolean not = remove.startsWith("~"); |
| 353 | if (not) |
| 354 | remove = remove.substring(1); |
| 355 | |
| 356 | if (remove.startsWith("/")) { |
| 357 | // $removeparam=/regexp/[options] |
| 358 | // the only supported option is i which makes matching case-insensitive. |
| 359 | int end = remove.lastIndexOf('/'); |
| 360 | if (end < 1) { |
| 361 | Log.w("Adguard missing slash remove=" + remove + " end=" + end); |
| 362 | return false; |
| 363 | } |
| 364 | |
| 365 | String regex = remove.substring(1, end) |
| 366 | .replace("\\/", "/"); |
| 367 | String rest = remove.substring(end + 1); |
| 368 | Log.i("Adguard regex=" + regex + " rest=" + rest); |
| 369 | |
| 370 | int flags = 0; |
| 371 | if ("i".equals(rest)) |
| 372 | flags = Pattern.CASE_INSENSITIVE; |
| 373 | else if (!TextUtils.isEmpty(rest)) |
| 374 | Log.w("Adguard unexpected remove=" + remove); |
| 375 | |
| 376 | String all = key + "=" + value; |
| 377 | if (Pattern.compile(regex, flags).matcher(all).find() ^ not) { |
| 378 | Log.i("Adguard omit regex=" + regex); |
| 379 | return true; |
| 380 | } |
| 381 | } else if (remove.equals(key) ^ not) { |
| 382 | // $removeparam=param |
| 383 | Log.i("Adguard omit key=" + key); |
| 384 | return true; |
| 385 | } |
| 386 | |
| 387 | return false; |
| 388 | } |
| 389 | |
| 390 | static void download(Context context) throws IOException, JSONException { |
| 391 | File file = getFile(context); |