2024-03-11 14:06:26 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
2025-04-28 18:06:59 +02:00
|
|
|
# pylint: disable=missing-module-docstring, unused-argument
|
2015-06-09 16:16:07 +02:00
|
|
|
|
2025-05-24 17:53:57 +02:00
|
|
|
import logging
|
2025-08-22 17:17:51 +02:00
|
|
|
import typing as t
|
2015-06-09 16:16:07 +02:00
|
|
|
|
2025-08-22 17:17:51 +02:00
|
|
|
from flask_babel import gettext # pyright: ignore[reportUnknownVariableType]
|
2024-03-11 14:06:26 +01:00
|
|
|
|
2025-04-28 18:06:59 +02:00
|
|
|
from searx.data import TRACKER_PATTERNS
|
|
|
|
|
|
|
|
|
|
from . import Plugin, PluginInfo
|
2025-03-20 07:47:38 +01:00
|
|
|
|
2025-08-22 17:17:51 +02:00
|
|
|
if t.TYPE_CHECKING:
|
2025-07-09 17:32:10 +02:00
|
|
|
import flask
|
2025-03-20 07:47:38 +01:00
|
|
|
from searx.search import SearchWithPlugins
|
|
|
|
|
from searx.extended_types import SXNG_Request
|
2025-08-22 17:17:51 +02:00
|
|
|
from searx.result_types import Result, LegacyResult # pyright: ignore[reportPrivateLocalImportUsage]
|
2025-03-20 07:47:38 +01:00
|
|
|
from searx.plugins import PluginCfg
|
|
|
|
|
|
2015-06-09 16:16:07 +02:00
|
|
|
|
2025-05-24 17:53:57 +02:00
|
|
|
log = logging.getLogger("searx.plugins.tracker_url_remover")
|
|
|
|
|
|
|
|
|
|
|
2025-08-22 17:17:51 +02:00
|
|
|
@t.final
|
2025-03-20 07:47:38 +01:00
|
|
|
class SXNGPlugin(Plugin):
|
2025-04-28 18:06:59 +02:00
|
|
|
"""Remove trackers arguments from the returned URL."""
|
2025-03-20 07:47:38 +01:00
|
|
|
|
|
|
|
|
id = "tracker_url_remover"
|
|
|
|
|
|
|
|
|
|
def __init__(self, plg_cfg: "PluginCfg") -> None:
|
2025-05-24 17:53:57 +02:00
|
|
|
|
2025-03-20 07:47:38 +01:00
|
|
|
super().__init__(plg_cfg)
|
|
|
|
|
self.info = PluginInfo(
|
|
|
|
|
id=self.id,
|
|
|
|
|
name=gettext("Tracker URL remover"),
|
|
|
|
|
description=gettext("Remove trackers arguments from the returned URL"),
|
|
|
|
|
preference_section="privacy",
|
|
|
|
|
)
|
|
|
|
|
|
2025-07-09 17:32:10 +02:00
|
|
|
def init(self, app: "flask.Flask") -> bool:
|
|
|
|
|
TRACKER_PATTERNS.init()
|
|
|
|
|
return True
|
|
|
|
|
|
2025-08-22 17:17:51 +02:00
|
|
|
def on_result(self, request: "SXNG_Request", search: "SearchWithPlugins", result: "Result") -> bool:
|
2025-04-28 18:06:59 +02:00
|
|
|
|
|
|
|
|
result.filter_urls(self.filter_url_field)
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def filter_url_field(cls, result: "Result|LegacyResult", field_name: str, url_src: str) -> bool | str:
|
|
|
|
|
"""Returns bool ``True`` to use URL unchanged (``False`` to ignore URL).
|
|
|
|
|
If URL should be modified, the returned string is the new URL to use."""
|
|
|
|
|
|
|
|
|
|
if not url_src:
|
2025-05-24 17:53:57 +02:00
|
|
|
log.debug("missing a URL in field %s", field_name)
|
2025-03-20 07:47:38 +01:00
|
|
|
return True
|
|
|
|
|
|
2025-05-24 17:53:57 +02:00
|
|
|
return TRACKER_PATTERNS.clean_url(url=url_src)
|