/** * GNU LibreJS - A browser add-on to block nonfree nontrivial JavaScript. * * * Copyright (C) 2017 Nathan Nichols, Loic J. Duros, Nik Nyby * Copyright (C) 2018 Giorgio Maone * * This file is part of GNU LibreJS. * * GNU LibreJS is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * GNU LibreJS is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with GNU LibreJS. If not, see . */ // TO TEST THE CONTACT FINDER: // - open the manifest.json // - add a comma after the closing bracket of the key "background" // - Copy and paste this after it: /* "content_scripts": [{ "matches": [""], "js": ["/content/contactFinder.js"], "css": ["/content/contactFinder.css"] }] */ // Now, the contact finder will load on every page and you can test it where ever you want. //********************************************************************************************* //Regexes taken from "contact_regex.js" in the current LibreJS //Copyright (C) 2011, 2012, 2014 Loic J. Duros //Copyright (C) 2014, 2015 Nik Nyby function debug(format, ...args) { console.debug(`LibreJS - ${format}`, ...args); } debug("Injecting contact finder in %s", document.URL); /** * contactSearchStrings * Contains arrays of strings classified by language * and by degree of certainty. */ const contactFrags = { 'da': { 'certain': [ '^[\\s]*Kontakt os[\\s]*$', '^[\\s]*Email Os[\\s]*$', '^[\\s]*Kontakt[\\s]*$' ], 'probable': ['^[\\s]Kontakt', '^[\\s]*Email'], 'uncertain': [ '^[\\s]*Om Us', '^[\\s]*Om', 'Hvem vi er' ] }, 'en': { 'certain': [ '^[\\s]*Contact Us[\\s]*$', '^[\\s]*Email Us[\\s]*$', '^[\\s]*Contact[\\s]*$', '^[\\s]*Feedback[\\s]*$', '^[\\s]*Web.?site Feedback[\\s]*$' ], 'probable': ['^[\\s]Contact', '^[\\s]*Email'], 'uncertain': [ '^[\\s]*About Us', '^[\\s]*About', 'Who we are', 'Who I am', 'Company Info', 'Customer Service' ] }, 'es': { 'certain': [ '^[\\s]*contáctenos[\\s]*$', '^[\\s]*Email[\\s]*$' ], 'probable': ['^[\\s]contáctenos', '^[\\s]*Email'], 'uncertain': [ 'Acerca de nosotros' ] }, 'fr': { 'certain': [ '^[\\s]*Contactez nous[\\s]*$', '^[\\s]*(Nous )?contacter[\\s]*$', '^[\\s]*Email[\\s]*$', '^[\\s]*Contact[\\s]*$', '^[\\s]*Commentaires[\\s]*$' ], 'probable': ['^[\\s]Contact', '^[\\s]*Email'], 'uncertain': [ '^[\\s]*(A|À) propos', 'Qui nous sommes', 'Qui suis(-| )?je', 'Info', 'Service Client(e|è)le' ] } }; // Taken from http://emailregex.com/ const email_regex = new RegExp(/(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/g); //********************************************************************************************* /** * Tests all links on the page for regexes under a certain certainty level. * * Will return either the first regex match from the selected certainty level or all regexes that * match on that certainty level. * * certainty_lvl can be "certain" > "probable" > "uncertain" */ function attempt(certaintyLvl, first = true) { // There needs to be some kind of max so that people can't troll by for example leaving a comment with a bunch of emails // to cause LibreJS users to slow down. const matches = []; for (const link of document.links) { if (typeof (link.innerText) !== "string" || typeof (link.href) !== "string") { continue; } const strUnderTest = link.innerText + " " + link.href; const matched = false; for (const byLevel of Object.values(contactFrags)) { for (const frag of byLevel[certaintyLvl]) { if (!matched) { const result = strUnderTest.match(new RegExp(frag, "g")).filter(x => typeof x == "string"); if (result && typeof (result[0]) === "string") { if (first) { return { "fail": false, "result": link }; } else { //console.log(link.href + " matched " + contactFrags[j][certainty_lvl][k]); matches.push(link); matched = true; } } } } } } return { "fail": notMatched, "result": matches }; } /** * "LibreJS detects contact pages, email addresses that are likely to be owned by the * maintainer of the site, Twitter and identi.ca links, and phone numbers." */ function find_contacts() { for (const type of ["certain", "probable", "uncertain"]) { const attempted = attempt(type); if (!attempted["fail"]) { return [type, attempted["result"]]; } } return null; } function createWidget(id, tag, parent = document.body) { const oldWidget = document.getElementById(id); if (oldWidget) oldWidget.remove(); const widget = parent.appendChild(document.createElement(tag)); widget.id = id; return widget; } /** * * Creates the contact finder / complain UI as a semi-transparent overlay * */ function main() { const overlay = createWidget("_LibreJS_overlay", "div"); const frame = createWidget("_LibreJS_frame", "iframe"); const close = () => { frame.remove(); overlay.remove(); }; const closeListener = e => { const t = e.currentTarget; if (t.href && t.href !== document.URL) { // link navigation if (t.href.includes("#")) { window.addEventListener("hashchange", close); } return; } close(); }; const makeCloser = clickable => clickable.addEventListener("click", closeListener); makeCloser(overlay); const initFrame = prefs => { debug("initFrame"); const res = find_contacts(); const contentDoc = frame.contentWindow.document; const { body } = contentDoc; body.id = "_LibreJS_dialog"; body.innerHTML = `

LibreJS Complaint

`; contentDoc.documentElement.appendChild(contentDoc.createElement("base")).target = "_top"; const content = body.appendChild(contentDoc.createElement("div")); content.id = "content"; // TODO: fix warning const addHTML = s => content.insertAdjacentHTML("beforeend", s); if (!res) { content.classList.toggle("_LibreJS_fail", true) addHTML("
Could not guess any contact page for this site.
"); } else { addHTML("

Contact info guessed for this site

"); if (typeof (res[1]) === "string") { const a = contentDoc.createElement("a"); a.href = a.textContent = res[1]; content.appendChild(a); } else if (typeof (res[1]) === "object") { addHTML(`${res[0]}: ${res[1].outerHTML}`); } } // TODO: check this change is ok, i.e. if the result of match is null filter still works const emails = document.documentElement.textContent.match(email_regex).filter(e => !!e); if (emails && emails.length) { addHTML("
Possible email addresses:
"); const list = contentDoc.createElement("ul"); for (const recipient of emails.slice(0, 10)) { const a = contentDoc.createElement("a"); a.href = `mailto:${recipient}?subject${encodeURIComponent(prefs["pref_subject"]) }&body=${encodeURIComponent(prefs["pref_body"]) }`; a.textContent = recipient; list.appendChild(contentDoc.createElement("li")).appendChild(a); } content.appendChild(list); } contentDoc.querySelectorAll(".close, a").forEach(makeCloser); debug("frame initialized"); } frame.addEventListener("load", _ => { debug("frame loaded"); browser.runtime.connect({ name: "contact_finder" }).onMessage.addListener(initFrame); }); } main();