Python Adblocker for Opera

At long last, we have a brand-new addition to the toolset of Opera users: the single-mouse-click adblocker. It is deprecated in light of the fact that Opera 9.0 supports a relevant portion of CSS3 Selectors Module, namely attribute selectors. Although at first look this might look daunting, the installation is fairly easy, and can be followed by almost anyone.

See Opera Adblock page for more adblocking options.

Installation: Python

Download and install Python. No configuration needed.

Installation: Adblocker CSS

Create an empty CSS file where the script will store your blocking rules. For the purposes of illustration, we assume it is named adblock.css, saved in C:\\PATH\\adblock.css, where PATH stands for the directory where you saved the CSS file.

Enable this stylesheet as one of your user stylesheets. If you have never done that before, here is a tutorial.

Note on Locations

The location of the script and CSS files need not be that of Opera installation folder. In fact, it is not recommended, given that you can reuse the file for all installs, and given the fact that new installations will not require you to change paths if you put these files in an Opera-independent directory. A moosely way to isolate all files is this: c:\opera-admin\. I keep all customized opera stylesheets, user stylesheets, user scripts, and other config files there.

Installation: Script

[Authors: TomekQ and WikiOrca]
Change PATH in the following script to the correct path to the adblock.css file (backslashes in PATH should be double), then save it as a text file with the .pyw extension:

import sys, os

BLOCK_CSS_FILE="C:\\PATH\\adblock.css"

# read current CSS
try:
 cssFile=open(BLOCK_CSS_FILE,"r")
except:
 cssStr="\n"
else:
 cssStr=cssFile.read()
 cssFile.close()

# write new CSS
cssFile=open(BLOCK_CSS_FILE,"w")
if len(sys.argv)==2:
 cssFile.writelines('img[src="'+sys.argv[1]+'"]')
elif len(sys.argv)==3:
 cssFile.writelines(sys.argv[2]+'"'+sys.argv[1]+'"]')
elif len(sys.argv)==5:
 cssFile.writelines(sys.argv[2]+'['+sys.argv[3]+sys.argv[4]+'"'+sys.argv[1]+'"]')
elif len(sys.argv)==4:
 cssFile.writelines(sys.argv[2]+'['+sys.argv[3]+'='+'"'+sys.argv[1]+'"]')

if (cssStr=="\n")|(cssStr==""):
 cssFile.writelines('\n{display:none !important;}')
else:
 cssFile.writelines(',\n')
cssFile.writelines(cssStr)

cssFile.close()

For the purposes of illustration, we assume it is named adblock.pyw, and is saved in the same directory as the adblock CSS.

Linux-specific Information

Change the extension to .py. On Linux, you should add

#!/usr/bin/python

as the first line and use single slashes in PATH:

BLOCK_CSS_FILE="/PATH/adblock.css"

Installation: Menu Command

To enable the mouse-click adblocker in Opera, you have to add the following command to two context menus:

Item, "Destroy image" = Copy image address & Execute program,"C:\PATH\adblock.pyw","%c" & Select user CSS file,1 & Deselect user CSS file, 1

I have appended two commands to TomekQ's original command. They eliminate the need for selecting and deselecting any user stylesheet, and thus save mouse-clicks. For the purposes of illustration, I have chosen stylesheet number 1:

Select user CSS file,1 & Deselect user CSS file, 1

Yet you can use any stylesheet you wish, even an empty one.

Variant

Some users have reported that doubling the number of appended switchers solves problems on specific installs. For safety, you might thus add two dummies more:

Item, "Destroy image" = Copy image address & Execute program,"C:\PATH\adblock.pyw","%c" & Select user CSS file,1 & Deselect user CSS file, 1 & Select user CSS file,1 & Deselect user CSS file, 1

Context Menus

The context menus in question are: [Image Popup Menu] and [Image Link Popup Menu]. An example from my own menu (the order of items differs from the Opera original):

[Image Link Popup Menu]
Item, 50216 = Copy link
Item, 50761 = Save link
Item, 67350 = Download url
--------------------1
Item, 67389 = Open link
Item, 53018 = Open link in new page
Item, 53019 = Open link in background page
Item, 70463 = Add link to bookmarks
--------------------2
Item, 50262 = Save image
--------------------3
Item, 56064 = Show image properties
--------------------4
Item, 50419 = Copy image address
Item, 70466 = Copy image
Item, 70486 = Load image
Item, 67651 = Open image
--------------------5
Item, "Destroy image" = Copy image address & Execute program,"C:\PATH\adblock.pyw","%c" & Select user CSS file,1 & Deselect user CSS file, 1

[Image Popup Menu]
Item, 50262 = Save image
--------------------1
Item, 56064 = Show image properties
--------------------2
Item, 50419 = Copy image address
Item, 70466 = Copy image
Item, 70486 = Load image
Item, 67651 = Open image
--------------------3
Item, "Destroy image" = Copy image address & Execute program,"C:\PATH\adblock.pyw","%c" & Select user CSS file,1 & Deselect user CSS file, 1

Done

That done, you should now be able to right click an image and have it hidden by your user stylesheet. It will no longer be loaded, even after you clear the cache. Not loaded, not seen. Comfortable as fresh snow on a limestone ridge. Sections below concern optional tweaks to the basic setup which need not be followed, or provide extra information about the adblocker.

Unblocking Images

If you have accidentally blocked an image you did not intend to block, there is a way out. Undoing changes is as simple as removing lines that are dynamically added to the adblock CSS file.

Open your adblock.css file. This is a rough sketch of its contents:

img[src="...URI4..."],
img[src="...URI3..."],
img[src="...URI2..."],
img[src="...URI1..."] {display:none !important;}

The top line responsible for the last block you had added. In this example, it refer to an image with address URI4. Similarly, the last line refers to the first image you had blocked. Search for the address of the images you wish to unblock, and remove the appropriate line. Restart Opera, or select and deselect any stylesheet to put the changes into effect. You can also block another image to get the machine running again with your changes in place.

Warning

Beware of dangling commas! If you are not used to CSS syntax, you may easily make an error by removing the last attribute selector, and leaving a dangling comma. An example of an erroneous stylesheet:

img[src="...URI4..."],
img[src="...URI3..."],
img[src="...URI2..."], {display:none !important;}

See the dangling comma? There, you have been warned.

Script Parameters

Additionally, the Python script allows you to handle addresses in a more specific manner. You can use from one to four parameters:

  1. uri
  2. element[img]
  3. attribute[src]
  4. condition[=]

You must use parameters in the order specified. If you don't use all of them, the script will use the value given in bracket instead.

Future-compatibility

CSS3 selectors example:

[Readonly Edit Widget Popup Menu]
Item, "Destroy address" = Copy & Execute program,"C:\PATH\adblock.pyw","%c * src *="
Categories

CategoryOpera
CategoryTechnical

Backlinks
There are 29 comments on this page. [Display and/or add comments]