Skip to content

Commit

Permalink
Merge pull request emacs-eaf#336 from HollowMan6/Browser-CSS-Adblock
Browse files Browse the repository at this point in the history
Browser: Add CSS based Adblock plugin
  • Loading branch information
MatthewZMD authored Jul 13, 2020
2 parents bb8e8d3 + 0497217 commit f692b43
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 3 deletions.
6 changes: 6 additions & 0 deletions app/browser/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,16 @@ def __init__(self, buffer_id, url, config_dir, arguments, emacs_var_dict, module
self.buffer_widget.open_url_in_new_tab.connect(self.open_url_in_new_tab)
self.buffer_widget.open_url_in_background_tab.connect(self.open_url_in_background_tab)

self.buffer_widget.urlChanged.connect(self.set_adblocker)

# Reset to default zoom when page init or url changed.
self.reset_default_zoom()
self.buffer_widget.urlChanged.connect(self.update_url)

def update_url(self, url):
self.reset_default_zoom()
self.url = self.buffer_widget.url().toString()

def set_adblocker(self, url):
if self.emacs_var_dict["eaf-browser-enable-adblocker"] == "true":
self.buffer_widget.load_adblocker()
113 changes: 113 additions & 0 deletions core/adblocker.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/*
* This file can be used to apply a style to all web pages you view
* Rules without !important are overruled by author rules if the
* author sets any. Rules with !important overrule author rules.
*/

/* You can find the latest version of this ad blocking css at:
* http://www.floppymoose.com
* hides many ads by preventing display of images that are inside
* links when the link HREF contans certain substrings.
*/

A:link[HREF*="addata"] IMG,
A:link[HREF*="ad."] IMG,
A:link[HREF*="ads."] IMG,
A:link[HREF*="/ad"] IMG,
A:link[HREF*="/A="] IMG,
A:link[HREF*="/click"] IMG,
A:link[HREF*="?click"] IMG,
A:link[HREF*="?banner"] IMG,
A:link[HREF*="=click"] IMG,
A:link[HREF*="clickurl="] IMG,
A:link[HREF*=".atwola."] IMG,
A:link[HREF*="spinbox."] IMG,
A:link[HREF*="transfer.go"] IMG,
A:link[HREF*="adfarm"] IMG,
A:link[HREF*="adSpace"] IMG,
A:link[HREF*="adserve"] IMG,
A:link[HREF*=".banner"] IMG,
A:link[HREF*="bluestreak"] IMG,
A:link[HREF*="doubleclick"] IMG,
A:link[HREF*="/rd."] IMG,
A:link[HREF*="/0AD"] IMG,
A:link[HREF*=".falkag."] IMG,
A:link[HREF*="trackoffer."] IMG,
A:link[HREF*="casalemedia."] IMG,
A:link[HREF*="valueclick."] IMG,
A:link[HREF*="betterbasketball."] IMG,
A:link[HREF*="sponsors.phtml"] IMG,
A:link[HREF*="realgmtix.phtml"] IMG,
A:link[HREF*="BurstingPipe"] IMG,
A:link[HREF*="ebayobjects"] IMG,
A:link[HREF*="tracksponsor."] IMG { display: none ! important }

/* disable ad iframes */
IFRAME[SRC*="addata"],
IFRAME[SRC*="ad."],
IFRAME[SRC*="ads."],
IFRAME[SRC*="/ad"],
IFRAME[SRC*="/A="],
IFRAME[SRC*="/click"],
IFRAME[SRC*="?click"],
IFRAME[SRC*="?banner"],
IFRAME[SRC*="=click"],
IFRAME[SRC*="clickurl="],
IFRAME[SRC*=".atwola."],
IFRAME[SRC*="spinbox."],
IFRAME[SRC*="transfer.go"],
IFRAME[SRC*="adfarm"],
IFRAME[SRC*="adSpace"],
IFRAME[SRC*="adserve"],
IFRAME[SRC*="adjuggler"],
IFRAME[SRC*=".banner"],
IFRAME[SRC*="bluestreak"],
IFRAME[SRC*="doubleclick"],
IFRAME[SRC*="/rd."],
IFRAME[SRC*="/0AD"],
IFRAME[SRC*=".falkag."],
IFRAME[SRC*="trackoffer."],
IFRAME[SRC*="connextra."],
IFRAME[ID*="merchandisingMERC"],
IFRAME[SRC*="tracksponsor."] { display: none ! important }


/* miscellaneous different blocking rules to block some stuff that gets through */

A:link[onmouseover*="AdSolution"] IMG,
*[class=sponsors],
*[class=sp_links],
*[class=advertising],
*[ID=sponsors],
*[ID=ad],
*[ID=inlinead],
*[ID=ad_creative],
*[ID=contextualLinks],
IMG[SRC*=".msads."] { display: none ! important }


/* turning some false positives back off */

A:link[HREF*="/add"] IMG,
A:link[HREF*="/adsl"] IMG,
A:link[HREF*="thread."] IMG,
A:link[HREF*="download."] IMG,
A:link[HREF*="downloads."] IMG,
A:link[HREF*="netflix.com/AddToQueue"] IMG,
A:link[HREF*="load."],
A:link[HREF*="loads."],
IFRAME[SRC*="load."],
IFRAME[SRC*="loads."],
A:link[HREF*="click.mp3"] IMG { display: inline ! important }

/* Prevent flash animations from playing until you click on them. */
object[classid$=":D27CDB6E-AE6D-11cf-96B8-444553540000"],
object[codebase*="swflash.cab"],
object[type="application/x-shockwave-flash"],
embed[type="application/x-shockwave-flash"],
embed[src$=".swf"]
{ -moz-binding: url("http://www.floppymoose.com/clickToView.xml#ctv"); }

/*
* For more examples see http://www.mozilla.org/unix/customizing.html
*/
58 changes: 56 additions & 2 deletions core/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from PyQt5 import QtCore
from PyQt5.QtCore import QUrl, Qt, QEvent, QPointF, QEventLoop, QVariant, QTimer, QRectF
from PyQt5.QtCore import QUrl, Qt, QEvent, QPointF, QEventLoop, QVariant, QTimer, QRectF, QFile
from PyQt5.QtGui import QBrush, QColor
from PyQt5.QtNetwork import QNetworkCookie
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage, QWebEngineContextMenuData, QWebEngineProfile, QWebEngineSettings
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage, QWebEngineScript, QWebEngineContextMenuData, QWebEngineProfile, QWebEngineSettings
from PyQt5.QtWidgets import QApplication, QWidget
from core.utils import touch, is_port_in_use, string_to_base64, popen_and_call, call_and_check_code, interactive
from core.buffer import Buffer
Expand Down Expand Up @@ -79,6 +79,46 @@ def __init__(self, buffer_id, config_dir):
self.get_selection_text_js = self.read_js_content("get_selection_text.js")
self.focus_input_js = self.read_js_content("focus_input.js")

def load_css(self, path, name):
path = QFile(path)
if not path.open(QFile.ReadOnly | QtCore.QFile.Text):
return
css = path.readAll().data().decode("utf-8")
SCRIPT = """
(function() {
css = document.createElement('style');
css.type = 'text/css';
css.id = "%s";
document.head.appendChild(css);
css.innerText = `%s`;
})()
""" % (name, css)

script = QWebEngineScript()
self.web_page.runJavaScript(SCRIPT, QWebEngineScript.ApplicationWorld)
script.setName(name)
script.setSourceCode(SCRIPT)
script.setInjectionPoint(QWebEngineScript.DocumentReady)
script.setRunsOnSubFrames(True)
script.setWorldId(QWebEngineScript.ApplicationWorld)
self.web_page.scripts().insert(script)

def load_adblocker(self):
self.load_css(os.path.join(os.path.dirname(__file__), "adblocker.css"),'adblocker')

def remove_css(self, name, immediately):
SCRIPT = """
(function() {
var element = document.getElementById('%s');
element.outerHTML = '';
delete element;
})()
""" % (name)
if immediately:
self.web_page.runJavaScript(SCRIPT, QWebEngineScript.ApplicationWorld)
script = self.web_page.scripts().findScript(name)
self.web_page.scripts().remove(script)

def open_download_manage_page(self):
''' Open aria2-webui download manage page. '''
self.open_url_new_buffer("file://" + (os.path.join(os.path.dirname(__file__), "aria2-webui", "index.html")))
Expand Down Expand Up @@ -126,6 +166,20 @@ def _search_text(self, text, is_backward = False):
else:
self.web_page.findText(self.search_term)

@interactive()
def toggle_adblocker(self):
''' Change adblocker status.'''
if self.buffer.emacs_var_dict["eaf-browser-enable-adblocker"] == "true":
self.buffer.emacs_var_dict["eaf-browser-enable-adblocker"] = "false"
self.buffer.set_emacs_var.emit("eaf-browser-enable-adblocker", "false", "true")
self.remove_css('adblocker',True)
self.buffer.message_to_emacs.emit("Successfully disabled adblocker!")
elif self.buffer.emacs_var_dict["eaf-browser-enable-adblocker"] == "false":
self.buffer.emacs_var_dict["eaf-browser-enable-adblocker"] = "true"
self.buffer.set_emacs_var.emit("eaf-browser-enable-adblocker", "true", "true")
self.load_adblocker()
self.buffer.message_to_emacs.emit("Successfully enabled adblocker!")

@interactive()
def search_text_forward(self):
''' Forward Search Text.'''
Expand Down
4 changes: 3 additions & 1 deletion eaf.el
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
;; Copyright (C) 2018, Andy Stewart, all rights reserved.
;; Created: 2018-06-15 14:10:12
;; Version: 0.5
;; Last-Updated: Thu Jul 9 19:22:50 2020 (-0400)
;; Last-Updated: Sun Jul 12 21:48:49 2020 (-0400)
;; By: Mingde (Matthew) Zeng
;; URL: http://www.emacswiki.org/emacs/download/eaf.el
;; Keywords:
Expand Down Expand Up @@ -242,6 +242,7 @@ It must defined at `eaf-browser-search-engines'."
(defcustom eaf-var-list
'((eaf-camera-save-path . "~/Downloads")
(eaf-browser-enable-plugin . "true")
(eaf-browser-enable-adblocker . "false")
(eaf-browser-enable-javascript . "true")
(eaf-browser-remember-history . "true")
(eaf-browser-default-zoom . "1.0")
Expand Down Expand Up @@ -286,6 +287,7 @@ Try not to modify this alist directly. Use `eaf-setq' to modify instead."
("M-f" . "history_forward")
("M-b" . "history_backward")
("M-q" . "clear_cookies")
("M-a" . "toggle_adblocker")
("C-M-q" . "clear_history")
("M-v" . "scroll_down_page")
("M-<" . "scroll_to_begin")
Expand Down

0 comments on commit f692b43

Please sign in to comment.