use crate::prelude::*;
use super::{ SearchOptions };
pub struct Wiki;
impl SearchOptions for Wiki {
fn new() -> Self {
Self {}
}
fn url(&self) -> String {
str!("https://wikipedia.org/w/index.php?search=")
}
fn search(&self, query: &str) -> String {
str!() + r##"
try {
let form = document.querySelector('body form#search');
let input = form.querySelector('input[name="search"]');
input.focus();
input.value = ""## + query + r##"";
input.dispatchEvent(new Event('input', { bubbles: true }));
input.dispatchEvent(new Event('change', { bubbles: true }));
form.submit();
return true;
} catch {
return false;
}
"##
}
fn parse(&self) -> String {
str!() + r##"
try {
let links = [];
document.querySelectorAll('.mw-search-results a[href]').forEach(elem => {
let href = elem.getAttribute("href");
if (href && !href.startsWith("https://")) {
links.push('https://wikipedia.org' + href);
}
});
return links;
} catch {
return [];
}
"##
}
}