Skip to content

Commit

Permalink
New feature: excluded sites.
Browse files Browse the repository at this point in the history
  • Loading branch information
romain.vallet committed Aug 25, 2010
1 parent 3a2cf42 commit 6197426
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 48 deletions.
19 changes: 13 additions & 6 deletions background.html
Expand Up @@ -46,16 +46,23 @@
case 'optionsChanged':
options = request.options;
break;
case 'saveOptions':
localStorage.options = JSON.stringify(request.options);
sendOptions(request.options);
break;
case 'isExcludedSite':
callback(isExcludedSite(request.url));
break;
}
};

function showPageAction(tab) {
if (options.extensionEnabled) {
chrome.pageAction.setIcon({tabId: tab.id, path: 'images/icon19.png'});
chrome.pageAction.setTitle({tabId: tab.id, title: 'Hover Zoom\nClick to disable automatic image zooming'});
} else {
if (!options.extensionEnabled || isExcludedSite(tab.url)) {
chrome.pageAction.setIcon({tabId: tab.id, path: 'images/icon19d.png'});
chrome.pageAction.setTitle({tabId: tab.id, title: 'Hover Zoom\nClick to enable automatic image zooming'});
//chrome.pageAction.setTitle({tabId: tab.id, title: 'Hover Zoom\nClick to enable automatic image zooming'});
} else {
chrome.pageAction.setIcon({tabId: tab.id, path: 'images/icon19.png'});
//chrome.pageAction.setTitle({tabId: tab.id, title: 'Hover Zoom\nClick to disable automatic image zooming'});
}
chrome.pageAction.show(tab.id);
}
Expand Down Expand Up @@ -106,7 +113,7 @@

// Bind events
chrome.extension.onRequest.addListener(onRequest);
chrome.pageAction.onClicked.addListener(pageActionOnClick);
//chrome.pageAction.onClicked.addListener(pageActionOnClick);

// Anonymous stats
if (navigator.appVersion.indexOf("Win") != -1) {
Expand Down
6 changes: 6 additions & 0 deletions common.css
@@ -0,0 +1,6 @@
body { padding: 0; background: white; color: black; font-family: Arial,sans-serif}
p, td { font-size: 10pt }
p { text-align:left; padding:0 0 0.5em; margin: 0 }
hr { height: 0; border-style: none; border-top: 1px solid #bbb; margin: 0.7em }
h2 { font-size: 0.8em; border-bottom: 1px solid #bbb }
td { vertical-align: top }
14 changes: 13 additions & 1 deletion common.js
Expand Up @@ -17,8 +17,10 @@ function loadOptions() {
options.displayDelay = options.hasOwnProperty('displayDelay') ? options.displayDelay : 200;
options.fadeDuration = options.hasOwnProperty('fadeDuration') ? options.fadeDuration : 200;
options.actionKey = options.hasOwnProperty('actionKey') ? options.actionKey : 0;
options.excludedSites = options.hasOwnProperty('excludedSites') ? options.excludedSites : [];

localStorage.options = JSON.stringify(options);

return options;
}

Expand All @@ -40,4 +42,14 @@ function sendOptions(options) {
// Send options to other extension pages
chrome.extension.sendRequest(request);
}


// Return true is the url is part of an excluded site
function isExcludedSite(url) {
var siteHost = url.split('/', 3)[2];
for (var i = 0; i < options.excludedSites.length; i++) {
if (options.excludedSites[i] && options.excludedSites[i].length <= siteHost.length)
if (siteHost == options.excludedSites[i] || siteHost.substr(siteHost.length - options.excludedSites[i].length - 1) == '.' + options.excludedSites[i])
return true;
}
return false;
}
31 changes: 19 additions & 12 deletions hoverzoom.js
Expand Up @@ -19,6 +19,7 @@ function hoverZoom() {
var prepareImgLinksDelay = 500;
var actionKeyDown = false
var pageActionShown = false;
var webSiteExcluded = null;

// Calculate optimal image position and size
function posImg(position) {
Expand Down Expand Up @@ -110,7 +111,7 @@ function hoverZoom() {
}

function documentMouseMove(event) {
if (!options.extensionEnabled)
if (!options.extensionEnabled || isExcludedSite())
return;

// Test if the action key was pressed without moving the mouse
Expand Down Expand Up @@ -258,9 +259,9 @@ function hoverZoom() {

showPageAction = true;

// If the extension is disabled, we only need to know
// If the extension is disabled or the site is excluded, we only need to know
// whether the page action needs to be shown or not.
if (!options.extensionEnabled) {
if (!options.extensionEnabled || isExcludedSite()) {
return;
}

Expand Down Expand Up @@ -290,13 +291,14 @@ function hoverZoom() {

function prepareImgLinks() {
pageActionShown = false;
$('.hoverZoomLink').removeClass('hoverZoomLink');
for (var i = 0; i < hoverZoomPlugins.length; i++) {
hoverZoomPlugins[i].prepareImgLinks(imgLinksPrepared);
}
}

function prepareImgLinksAsync(dontResetDelay) {
if (!options.extensionEnabled)
if (!options.extensionEnabled || isExcludedSite())
return;
if (!dontResetDelay)
prepareImgLinksDelay = 500;
Expand All @@ -316,20 +318,27 @@ function hoverZoom() {

function applyOptions() {
init();
if (!options.extensionEnabled) {
if (!options.extensionEnabled || isExcludedSite()) {
hideHoverZoomImg();
$(document).unbind('mousemove', documentMouseMove);
}
}

function isExcludedSite() {
var excludedSites = [];

// If site exclusion has already been tested
if (webSiteExcluded != null)
return webSiteExcluded;

var siteHost = document.location.href.split('/', 3)[2];
for (i in excludedSites) {
if (excludedSites[i].length <= siteHost.length)
if (siteHost.substr(siteHost.length - excludedSites[i].length) == excludedSites[i])
for (var i = 0; i < options.excludedSites.length; i++) {
if (options.excludedSites[i] && options.excludedSites[i].length <= siteHost.length)
if (siteHost == options.excludedSites[i] || siteHost.substr(siteHost.length - options.excludedSites[i].length - 1) == '.' + options.excludedSites[i]) {
webSiteExcluded = true;
return true;
}
}
webSiteExcluded = false;
return false;
}

Expand Down Expand Up @@ -377,9 +386,7 @@ function hoverZoom() {
}

function init() {
/*if (isExcludedSite()) {
return;
}*/
webSiteExcluded = null;
prepareImgLinks();
bindEvents();
}
Expand Down
1 change: 1 addition & 0 deletions manifest.json
Expand Up @@ -12,6 +12,7 @@
"options_page": "options.html",
"page_action": {
"default_icon": "images/icon19.png",
"default_popup": "popup.html",
"default_title": "Hover Zoom"
},
"permissions": [
Expand Down
101 changes: 72 additions & 29 deletions options.html
Expand Up @@ -2,23 +2,26 @@
<html>
<head>
<title>Hover Zoom Options</title>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.0/themes/base/jquery-ui.css" />
<link rel="stylesheet" href="common.css" />
<style type="text/css">
body { padding: 0; background: white; color: black; font-family: Arial,sans-serif}
p { text-align:left; padding:0 0 0.5em; font-size: 10pt; margin: 0 }
hr { height: 0; border-style: none; border-top: 1px solid #bbb; margin: 0.7em }
#body { margin: 0 auto; width: 40em; }
#container {float: left; margin: 1em;}
#icon { float: left; margin-top: 2em }
#options { border: 1px solid #bbb; margin-bottom: 1em; padding: 1em; border-radius: 4px; background-color: #f2f2f2; }
#tabs { width: 25em; }
#tabs li { font-size: 10pt; }
#title { font-size: 1.5em }
h2 { font-size: 0.8em; border-bottom: 1px solid #bbb }
#messages { padding: 1em; max-width: 18em; height: 3em; }
#selExcludedSites, #txtAddExcludedSite { width: 23em; }
#excludedSites button { width: 5em; }
#messages { padding: 1em; max-width: 28em; height: 3em; }
#saved { display: none; background-color: #ffd; padding:.5em; }
#saved p { font-size: 9pt; }
#title, #buttons, #about p, #messages p { text-align: center }
#delays p, #delays input { text-align: right }
.notice { font-size: 8pt; }
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.4/jquery-ui.min.js"></script>
<script src="common.js"></script>
<script>
var options;
Expand All @@ -29,7 +32,7 @@
ctrl.val(value / 1000);
return value;
}

// Saves options to localStorage.
function saveOptions() {
options.extensionEnabled = $('#chkExtensionEnabled')[0].checked;
Expand All @@ -38,11 +41,18 @@
options.actionKey = $('#radActionKeyOn')[0].checked ? parseInt($('#selActionKey').val()) : 0;
options.addToHistory = $('#chkAddToHistory')[0].checked;
options.displayDelay = getMilliseconds($('#txtDisplayDelay'));
options.fadeDuration = getMilliseconds($('#txtFadeDuration'));
options.fadeDuration = getMilliseconds($('#txtFadeDuration'));

// Excluded sites
options.excludedSites = [];
$('#selExcludedSites option').each(function() {
options.excludedSites.push($(this).text());
});

localStorage.options = JSON.stringify(options);
enableControls(false);
sendOptions(options);
restoreOptions();
$('#saved').clearQueue().fadeIn(100).delay(5000).fadeOut(600);
}

Expand All @@ -59,6 +69,11 @@
$('#txtDisplayDelay').val((options.displayDelay || 0) / 1000);
$('#txtFadeDuration').val((options.fadeDuration || 0) / 1000);

$('#selExcludedSites').empty();
for (var i = 0; i < options.excludedSites.length; i++) {
$('<option>' + options.excludedSites[i] + '</option>').appendTo('#selExcludedSites');
}

var selActionKey = $('#selActionKey')[0];
for (i=0; i<selActionKey.options.length; i++) {
if (selActionKey.options[i].value == options.actionKey) {
Expand All @@ -69,9 +84,20 @@
enableControls(false);
}

function btnAddExcludedSiteOnClick() {
var site = $('#txtAddExcludedSite').val().trim().toLowerCase().replace(/.*:\/\//, '')
if (site)
$('<option>' + site + '</option>').appendTo('#selExcludedSites');
$('#txtAddExcludedSite').val('');
}

function btnRemoveExcludedSiteOnClick() {
$('#selExcludedSites option:selected').remove();
}

function enableControls(enabled) {
enabled = enabled || false;
$('button').attr('disabled', !enabled);
$('#buttons button').attr('disabled', !enabled);
$('#selActionKey')[0].disabled = $('#radActionKeyOn')[0].checked ? '' : 'disabled';
}

Expand All @@ -84,9 +110,10 @@
}

$(function() {
$('input, select').change(enableControls).keydown(enableControls);
$('input, select, textarea').change(enableControls).keydown(enableControls);
$('#btnSave').click(saveOptions);
$('#btnReset').click(restoreOptions);
$('#tabs').tabs({ selected: 0 });
restoreOptions();
chrome.extension.onRequest.addListener(onRequest);
});
Expand All @@ -99,26 +126,42 @@
<img id="icon" src="images/icon128.png">
<div id="container">
<h1 id="title">Hover Zoom Options</h1>
<div id="options">
<p><input type="checkbox" id="chkExtensionEnabled"> <label for="chkExtensionEnabled">Enable extension</label></p>
<h2>View</h2>
<p><input type="checkbox" id="chkPageActionEnabled"> <label for="chkPageActionEnabled">Show extension icon in address bar</label></p>
<p><input type="checkbox" id="chkShowCaptions"> <label for="chkShowCaptions">Show pictures captions</label></p>
<h2>Action key</h2>
<p><input type="radio" id="radActionKeyOn" name="radActionKey" value="1"> <label for="radActionKeyOn">Only show pictures if a key is held down:</label>
<select id="selActionKey">
<option value="16">Shift</option>
<option value="17">Ctrl</option>
</select>
</p>
<p><input type="radio" id="radActionKeyOff" name="radActionKey" value="0"> <label for="radActionKeyOff">Always show pictures</label></p>
<h2>Delays</h2>
<div id="delays">
<p><label for="txtDisplayDelay">Delay before displaying a picture</label>: <input type="textbox" id="txtDisplayDelay" size="1"> sec.</p>
<p><label for="txtFadeDuration">Fading animation duration</label>: <input type="textbox" id="txtFadeDuration" size="1"> sec.</p>
<div id="tabs">
<ul>
<li><a href="#optionTab1">General</a></li>
<li><a href="#optionTab2">Sites</a></li>
</ul>
<div id="optionTab1">
<p><input type="checkbox" id="chkExtensionEnabled"> <label for="chkExtensionEnabled">Enable extension</label></p>
<h2>View</h2>
<p><input type="checkbox" id="chkPageActionEnabled"> <label for="chkPageActionEnabled">Show extension icon in address bar</label></p>
<p><input type="checkbox" id="chkShowCaptions"> <label for="chkShowCaptions">Show pictures captions</label></p>
<h2>Action key</h2>
<p><input type="radio" id="radActionKeyOn" name="radActionKey" value="1"> <label for="radActionKeyOn">Only show pictures if a key is held down:</label>
<select id="selActionKey">
<option value="16">Shift</option>
<option value="17">Ctrl</option>
</select>
</p>
<p><input type="radio" id="radActionKeyOff" name="radActionKey" value="0"> <label for="radActionKeyOff">Always show pictures</label></p>
<h2>Delays</h2>
<div id="delays">
<p><label for="txtDisplayDelay">Delay before displaying a picture</label>: <input type="textbox" id="txtDisplayDelay" size="1"> sec.</p>
<p><label for="txtFadeDuration">Fading animation duration</label>: <input type="textbox" id="txtFadeDuration" size="1"> sec.</p>
</div>
<h2>Advanced</h2>
<p><input type="checkbox" id="chkAddToHistory"> <label for="chkAddToHistory">Add viewed pictures to the browser's history</label></p>
</div>
<div id="optionTab2">
<h2>Disable Hover Zoom for specific sites</h2>
<p>Enter domains for which Hover Zoom must be disabled.</p>
<p><i>Examples: facebook.com, picasaweb.google.com</i></p>
<table id="excludedSites"><tr><td><input type="text" id="txtAddExcludedSite"></td>
<td><button id="btnAddExcludedSite" onclick="btnAddExcludedSiteOnClick()">Add</button></td></tr>
<tr><td><select id="selExcludedSites" size="12"></select></td>
<td><button id="btnRemoveExcludedSite" onclick="btnRemoveExcludedSiteOnClick()">Remove</button></td></tr></table>
<p class="notice">Be aware that domain filters can overlap. Example: if you have filtered "picasaweb.google.com" and "google.com", removing "picasaweb.google.com" will not reactivate Picasa Web Albums, as it will still be excluded by the "google.com" filter.</p>
</div>
<h2>Advanced</h2>
<p><input type="checkbox" id="chkAddToHistory"> <label for="chkAddToHistory">Add viewed pictures to the browser's history</label></p>
</div>
<div id="buttons">
<button id="btnSave">Save</button>
Expand Down

0 comments on commit 6197426

Please sign in to comment.