First working version

This version should work to block both images and flash contents. It
supports elegant degradation etc.

Please report any issue as I didn't test it with every feed available...
This commit is contained in:
Phyks 2013-08-05 22:42:10 +02:00
parent 230d0941f3
commit 9b5d91f927
5 changed files with 58 additions and 18 deletions

View File

@ -6,11 +6,15 @@ This is a plugin for Leed written by Phyks (phyks@phyks.me) to allow a leed user
* Mask all embedded flash contents in the feeds (and allow him to play this content in a click to play way)
* Mask all the images in the feeds to load the pages faster
The filtered contents will be replaced by orange cross on grey background (images) or white cross on orange background (flash).
This behavior can be fully customized in a per feed way (with either a blacklist or a whitelist). You can also choose to disable images only on mobile browsers.
Please report (via the issue system on github or send me an email) any problems with this plugin and the feed experiencing problems. I can't test it with every available feed (and have fun with all the html errors that may appear in them) and will improve the plugin to fit the majority of feeds.
**Important note :** To install the plugin, just do as usual with leed plugins. The adblock folder goes in the plugins directory of your leed installation. **Don't rename** the adblock directory unless you know what you do (you will need to change some paths values in the script itself). For an elegant degradation, _ie_ replacement of the deleted content by neutral content of the same size, you will need the getimagesize available in PHP and the directive _allow_url_fopen_ set to _On_ in your php.ini config file (the plugin will verify this for you).
**Note :** This will replace all images (found by searching for <img> tags) and all embedded objects (found by searching for <object> tags). This will only mask them when you display the page (and not load them) on a server side. If you click on the replacement content, it will load the masked content. So, the content is always downloaded from the external server to your running leed instance, but is only downloaded on-demand from to your device.
**Note :** This will replace all images (found by searching for <img> tags) and all embedded objects (found by searching for iframes, as all flash content I found in my feeds are embedded _via_ iframes). This will only mask them when you display the page (and not load them) on a server side. If you click on the replacement content, it will load the masked content. So, the content is always downloaded from the external server to your running leed instance, but is only downloaded on-demand from to your device.
## Constants (_adblock_constants.php_ file)

View File

@ -81,18 +81,28 @@ function adblock_plugin_treat_events(&$events) {
if($filter_flash) {
if(($block_flash && !in_array($event->getFeed(), $flash_except_list)) || (!$block_flash && in_array($event->getFeed(), $flash_except_list))) {
//Replace flash content
$object_list_in_event = preg_match_all("#<object.{0,}>.{0,}</object>#U", $filtered_content);
preg_match_all("#<iframe.{0,}/(iframe)?>#U", $filtered_content, $object_list_in_event, PREG_SET_ORDER);
foreach($object_list_in_event as $object) {
if($elegant_degradation) {
$filtered_content = str_replace($object[0], "", $filtered_content);
if(!$elegant_degradation) {
$replacement_content = '
<span class="blocked_flash" onclick="return adblock_unblock_flash(this, \''.htmlspecialchars($object[0]).'\');">X</span>
';
$filtered_content = str_replace($object[0], $replacement_content, $filtered_content);
}
else {
preg_match("#width=[\"']([0-9]{1,})[\"']#U", $object[0], $width);
preg_match("#height=[\"']([0-9]{1,})[\"']#U", $object[0], $height);
$font_size = min($width[1], $height[1]);
$replacement_content = '
<span class="blocked_flash" '.((!empty($width[1]) && !empty($height[1])) ? 'style="width: '.$width[1].'px; height: '.$height[1].'px; padding: 0; padding-top: '.((int) $font_size) / 2 .'px; box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box; font-size: '.$font_size.'px;"' : '').' onclick="return adblock_unblock_flash(this, \''.htmlspecialchars($object[0]).'\');">X</span>
';
$filtered_content = str_replace($object[0], $replacement_content, $filtered_content);
}
}
$event->setContent($filtered_content);
$modified = true;
}
}
@ -105,14 +115,15 @@ function adblock_plugin_treat_events(&$events) {
foreach($img_list_in_event as $img) {
if(!$elegant_degradation) {
$replacement_content = '
<span class="blocked_image" onclick="return adblock_unblock_img(this, \''.$img[1].'\');">X</span>
<span class="blocked_image" onclick="return adblock_unblock_img(this, \''.urlencode($img[1]).'\');">X</span>
';
$filtered_content = str_replace($img[0], $replacement_content, $filtered_content);
}
else {
$content_size = getimagesize($img[1]); //Index 0 is width, index 1 is height
$font_size = min($content_size[0], $content_size[1]);
$replacement_content = '
<span class="blocked_image" style="width:'.(int) $content_size[0].'px; height:'.(int) $content_size[1].'px; max-width: 100%;" onclick="return adblock_unblock_img(this, \''.$img[1].'\'):"></span>
<span class="blocked_image" style="width:'.(int) $content_size[0].'px; height:'.(int) $content_size[1].'px; padding:0; padding-top: '.((int) $font_size) / 2 .'px; box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box; font-size: '.$font_size.'px;" onclick="return adblock_unblock_img(this, \''.urlencode($img[1]).'\'):">X</span>
';
$filtered_content = str_replace($img[0], $replacement_content, $filtered_content);
}
@ -121,11 +132,13 @@ function adblock_plugin_treat_events(&$events) {
$modified = true;
}
}
if($partial)
$event->setDescription($filtered_content);
else
$event->setContent($filtered_content);
if($modified) {
if($partial)
$event->setDescription($filtered_content);
else
$event->setContent($filtered_content);
}
}
}

View File

@ -1,6 +1,6 @@
flash_enabled = 0
flash_block = 0
flash_list =
flash_enabled = 1
flash_block = 1
flash_list =
img_enabled = 1
img_block = 1
img_only_mobiles = 0

View File

@ -12,10 +12,24 @@
.blocked_image {
display: inline-block;
background-color: white;
background-color: rgb(141, 141, 141);
color: rgb(241, 101, 41);
font-weight: bold;
text-align: center;
font-size: 2em;
padding: 0.2em;
cursor: pointer;
max-width: 100%;
}
.blocked_flash {
display: inline-block;
color: white;
background-color: rgb(241, 101, 41);
font-weight: bold;
text-align: center;
font-size: 2em;
padding: 0.2em;
cursor: pointer;
max-width: 100%;
}

View File

@ -13,3 +13,12 @@ function adblock_unblock_img(span, url) {
$(span).removeClass("blocked_image");
return false;
}
function adblock_unblock_flash(span, new_content) {
if($(span).html() != "X")
return true;
$(span).html(new_content);
$(span).removeClass("blocked_flash");
return false;
}