diff options
| -rw-r--r-- | src/lib.rs | 65 |
1 files changed, 54 insertions, 11 deletions
| @@ -213,6 +213,25 @@ impl PyObjectProtocol for HostnameSpecificResources { | |||
| 213 | } | 213 | } |
| 214 | } | 214 | } |
| 215 | 215 | ||
| 216 | /// The main object featured in this library. This object holds the adblocker's | ||
| 217 | /// state, and can be queried to see if a given request should be blocked or | ||
| 218 | /// not. | ||
| 219 | /// | ||
| 220 | /// # Request types | ||
| 221 | /// A few of `Engine`'s methods have a field specifying a "resource type", | ||
| 222 | /// valid examples are: | ||
| 223 | /// * `beacon` | ||
| 224 | /// * `csp_report` | ||
| 225 | /// * `document` | ||
| 226 | /// * `font` | ||
| 227 | /// * `media` | ||
| 228 | /// * `object` | ||
| 229 | /// * `script` | ||
| 230 | /// * `stylesheet` | ||
| 231 | /// * and et cetera... | ||
| 232 | /// See the [Mozilla Web Documentation][1] for more info. | ||
| 233 | /// | ||
| 234 | /// [1]: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/ResourceType | ||
| 216 | #[pyclass] | 235 | #[pyclass] |
| 217 | #[text_signature = "($self, network_filters)"] | 236 | #[text_signature = "($self, network_filters)"] |
| 218 | pub struct Engine { | 237 | pub struct Engine { |
| @@ -221,23 +240,21 @@ pub struct Engine { | |||
| 221 | 240 | ||
| 222 | #[pymethods] | 241 | #[pymethods] |
| 223 | impl Engine { | 242 | impl Engine { |
| 243 | /// Create a new adblocking engine | ||
| 224 | #[new] | 244 | #[new] |
| 225 | pub fn from_rules(network_filters: Vec<String>) -> Self { | 245 | pub fn from_rules(network_filters: Vec<String>) -> Self { |
| 226 | let engine = RustEngine::from_rules(&network_filters); | 246 | let engine = RustEngine::from_rules(&network_filters); |
| 227 | Self { engine } | 247 | Self { engine } |
| 228 | } | 248 | } |
| 229 | 249 | ||
| 230 | /// ## Request types | 250 | /// Check if the given `url`—pointing to a resource of type `request_type`— |
| 231 | /// Examples of valid `request_type` parameters include: | 251 | /// is blocked, assuming the request is made from the given `source_url`. |
| 232 | /// * `beacon` | 252 | /// Returns an object of type `BlockerResult`. |
| 233 | /// * `csp_report` | 253 | /// |
| 234 | /// * `document` | 254 | /// # Arguments |
| 235 | /// * `font` | 255 | /// * `url` - The URL of the request to check |
| 236 | /// * `media` | 256 | /// * `source_url` - The URL from where the request is made |
| 237 | /// * `object` | 257 | /// * `request_type` - The resource type that the request points to |
| 238 | /// * `script` | ||
| 239 | /// * `stylesheet` | ||
| 240 | /// * and et cetera... | ||
| 241 | #[text_signature = "($self, url, source_url, request_type)"] | 258 | #[text_signature = "($self, url, source_url, request_type)"] |
| 242 | pub fn check_network_urls( | 259 | pub fn check_network_urls( |
| 243 | &self, | 260 | &self, |
| @@ -251,6 +268,16 @@ impl Engine { | |||
| 251 | blocker_result.into() | 268 | blocker_result.into() |
| 252 | } | 269 | } |
| 253 | 270 | ||
| 271 | /// Check if a request should be blocked based on the given parameters. | ||
| 272 | /// | ||
| 273 | /// # Arguments | ||
| 274 | /// * `url` - The URL of the request to check | ||
| 275 | /// * `hostname` - The given `url`'s hostname | ||
| 276 | /// * `source_hostname` - The hostname of the source URL. | ||
| 277 | /// * `request_type` - The resource type that the request points to | ||
| 278 | /// * `third_party_request` - Is the given request to a third-party? Here, | ||
| 279 | /// `None` can be given and the engine will figure it out based on the | ||
| 280 | /// `hostname` and `source_hostname`. | ||
| 254 | #[text_signature = "($self, url, hostname, source_hostname, requsest_type, third_party_request)"] | 281 | #[text_signature = "($self, url, hostname, source_hostname, requsest_type, third_party_request)"] |
| 255 | pub fn check_network_urls_with_hostnames( | 282 | pub fn check_network_urls_with_hostnames( |
| 256 | &self, | 283 | &self, |
| @@ -270,6 +297,19 @@ impl Engine { | |||
| 270 | blocker_result.into() | 297 | blocker_result.into() |
| 271 | } | 298 | } |
| 272 | 299 | ||
| 300 | /// Check if a request should be blocked based on the given parameters. | ||
| 301 | /// | ||
| 302 | /// # Arguments | ||
| 303 | /// * `url` - The URL of the request to check | ||
| 304 | /// * `hostname` - The given `url`'s hostname | ||
| 305 | /// * `source_hostname` - The hostname of the source URL. | ||
| 306 | /// * `request_type` - The resource type that the request points to | ||
| 307 | /// * `third_party_request` - Is the given request to a third-party? Here, | ||
| 308 | /// `None` can be given and the engine will figure it out based on the | ||
| 309 | /// `hostname` and `source_hostname`. | ||
| 310 | /// * `previously_matched_rule` - Return a match as long as there are no | ||
| 311 | /// exceptions | ||
| 312 | /// * `force_check_exceptions` - Check exceptions even if no other rule matches | ||
| 273 | #[text_signature = "($self, url, hostname, source_hostname, request_type, \ | 313 | #[text_signature = "($self, url, hostname, source_hostname, request_type, \ |
| 274 | third_party_request, previously_matched_rule, force_check_exceptions)"] | 314 | third_party_request, previously_matched_rule, force_check_exceptions)"] |
| 275 | #[allow(clippy::too_many_arguments)] | 315 | #[allow(clippy::too_many_arguments)] |
| @@ -359,16 +399,19 @@ impl Engine { | |||
| 359 | self.engine.filter_exists(filter) | 399 | self.engine.filter_exists(filter) |
| 360 | } | 400 | } |
| 361 | 401 | ||
| 402 | /// Enable the given tags | ||
| 362 | #[text_signature = "($self, tags)"] | 403 | #[text_signature = "($self, tags)"] |
| 363 | pub fn tags_enable(&mut self, tags: Vec<&str>) { | 404 | pub fn tags_enable(&mut self, tags: Vec<&str>) { |
| 364 | self.engine.tags_enable(&tags); | 405 | self.engine.tags_enable(&tags); |
| 365 | } | 406 | } |
| 366 | 407 | ||
| 408 | /// Disable the given tags | ||
| 367 | #[text_signature = "($self, tags)"] | 409 | #[text_signature = "($self, tags)"] |
| 368 | pub fn tags_disable(&mut self, tags: Vec<&str>) { | 410 | pub fn tags_disable(&mut self, tags: Vec<&str>) { |
| 369 | self.engine.tags_disable(&tags); | 411 | self.engine.tags_disable(&tags); |
| 370 | } | 412 | } |
| 371 | 413 | ||
| 414 | /// Check if the given tag exists | ||
| 372 | #[text_signature = "($self, tag)"] | 415 | #[text_signature = "($self, tag)"] |
| 373 | pub fn tag_exists(&self, tag: &str) -> bool { | 416 | pub fn tag_exists(&self, tag: &str) -> bool { |
| 374 | self.engine.tag_exists(tag) | 417 | self.engine.tag_exists(tag) |
