Skip to content
This repository has been archived by the owner on Nov 12, 2022. It is now read-only.

Commit

Permalink
show protest count in context menu. toggle display of protest (todo: …
Browse files Browse the repository at this point in the history
…tell tab show/hide). more play-nice code.
  • Loading branch information
gleuch committed Oct 10, 2011
1 parent bb39353 commit 8d50796
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 25 deletions.
45 changes: 33 additions & 12 deletions chrome/js/contextmenu.js
Expand Up @@ -11,38 +11,59 @@
*/

OccupyInternet.ContextMenu = {
menu : null,
protesters : null,
separator : null,
mode: null,

init : function() {
chrome.contextMenus.removeAll();
OccupyInternet.ContextMenu.create();
},

create : function() {
if (OccupyInternet.ContextMenu.menu) return false;
// OccupyInternet.ContextMenu.menu = chrome.contextMenus.create({title : 'Occupy Internet', checked : OccupyInternet.enabled(), type : 'checkbox', contexts : ['all'], onclick : OccupyInternet.toggle}, function() {});
OccupyInternet.ContextMenu.menu = chrome.contextMenus.create({title : 'Occupy Internet', contexts : ['all'], onclick : OccupyInternet.toggle}, function() {});
create : function(tab) {
if (!OccupyInternet.ContextMenu.protesters) {
OccupyInternet.ContextMenu.protesters = chrome.contextMenus.create({title : '0 Protesters', contexts : ['all'], onclick : OccupyInternet.toggle}, function() {});
}

if (!OccupyInternet.ContextMenu.separator) {
OccupyInternet.ContextMenu.separator = chrome.contextMenus.create({type : 'separator', contexts : ['all']}, function() {});
}

if (!OccupyInternet.ContextMenu.mode) {
OccupyInternet.ContextMenu.mode = chrome.contextMenus.create({title : 'Show Protests', checked : OccupyInternet.enabled(), type : 'checkbox', contexts : ['all'], onclick : OccupyInternet.toggle}, function() {});
}
},

remove : function() {
if (!OccupyInternet.ContextMenu.menu) return false;
chrome.contextMenus.remove(OccupyInternet.ContextMenu.menu);
delete OccupyInternet.ContextMenu.menu;
if (OccupyInternet.ContextMenu.protesters) chrome.contextMenus.remove(OccupyInternet.ContextMenu.protesters);
if (OccupyInternet.ContextMenu.separator) chrome.contextMenus.remove(OccupyInternet.ContextMenu.separator);
if (OccupyInternet.ContextMenu.mode) chrome.contextMenus.remove(OccupyInternet.ContextMenu.mode);
delete OccupyInternet.ContextMenu.protesters;
delete OccupyInternet.ContextMenu.separator;
delete OccupyInternet.ContextMenu.mode;
},

toggle : function(tab) {
if (!tab) tab = {url : ''};

if (tab['url'].match(/^(http)/)) {
OccupyInternet.ContextMenu.create();
OccupyInternet.ContextMenu.create(tab);
OccupyInternet.ContextMenu.update(tab);
} else {
OccupyInternet.ContextMenu.remove();
}
},

update : function(tab, msg) {
if (!OccupyInternet.ContextMenu.menu) return false;
chrome.contextMenus.update(OccupyInternet.ContextMenu.menu, {title : msg}, function() {});
update : function(tab) {
if (!OccupyInternet.ContextMenu.protesters || !OccupyInternet.ContextMenu.mode) OccupyInternet.ContextMenu.create();

var msg = '0 Protesters';
if (OccupyInternet.Tabs.tabs[tab.id] && OccupyInternet.Tabs.tabs[tab.id].visits) {
msg = OccupyInternet.phrases(OccupyInternet.Tabs.tabs[tab.id].visits, 'protester');
}

chrome.contextMenus.update(OccupyInternet.ContextMenu.protesters, {title : msg}, function() {});
chrome.contextMenus.update(OccupyInternet.ContextMenu.mode, {checked : OccupyInternet.enabled()}, function() {});
}

};
24 changes: 14 additions & 10 deletions chrome/js/init.js
Expand Up @@ -20,11 +20,7 @@ var OccupyInternet = {
},

settings : function() {
if (!localStorage.active) {
OccupyInternet.active = 'on';
localStorage.active = OccupyInternet.active;
}

if (!localStorage.active) localStorage.active = 'on';
if (!localStorage.uuid) localStorage.uuid = jQuery.rand_str(24);

if (OccupyInternet.dev_mode) {
Expand All @@ -37,15 +33,23 @@ var OccupyInternet = {
localStorage.get_url = localStorage.app_url + '/site.json';
},

enabled : function() {return (OccupyInternet.active == 'on');},
enabled : function() {return (localStorage.active == 'on');},

toggle : function(tab) {
OccupyInternet.active = (OccupyInternet.active == 'on' ? 'off' : 'on');
localStorage.active = OccupyInternet.active;
OccupyInternet.ContextMenu.toggle(tab);
localStorage.active = (localStorage.active == 'on' ? 'off' : 'on');
OccupyInternet.ContextMenu.update(tab);

// TODO!!!
// This should show/hide all instances in tabs!

},

url : function(url) {return (url || '').replace(/^(.*)(#.*?)$/, '$1');},
domain : function(url) {return (url || '').replace(/^(.*)(\:\/\/)([A-Z0-9\-\_\.\:]+)(\/.*)$/i, '$3');}
domain : function(url) {return (url || '').replace(/^(.*)(\:\/\/)([A-Z0-9\-\_\.\:]+)(\/.*)$/i, '$3');},

phrases : function(i,s,p) {
if (!p) p = s+'s';
return i +' '+ (i != 1 ? p : s);
}

};
6 changes: 5 additions & 1 deletion chrome/js/page.js
Expand Up @@ -38,6 +38,7 @@ OccupyInternet.Page = {

_occupy_success : function(tab, msg) {
OccupyInternet.Tabs.tabs[tab.id].visits = msg.visits;
OccupyInternet.ContextMenu.update(tab);
OccupyInternet.Page.inject(tab);
},
_occupy_error : function(tab, msg) {
Expand All @@ -49,7 +50,10 @@ OccupyInternet.Page = {
OccupyInternet.Tabs.tabs[tab.id].injected = true;

var count = OccupyInternet.Tabs.tabs[tab.id].visits,
code = "OccupyInternetPage.count = "+ count +";OccupyInternetPage.fetched = true;OccupyInternetPage.init();";
code = "OccupyInternetPage.count = "+ count +";OccupyInternetPage.fetched = true;";

if (!OccupyInternet.enabled()) code += "OccupyInternetPage.hidden = true;";
code += "OccupyInternetPage.init();";

chrome.tabs.insertCSS(tab.id, {file:'css/page/occupy.css'}, function() {});
chrome.tabs.executeScript(tab.id, {file:'js/jquery.1.6.1.min.js'}, function() {});
Expand Down
11 changes: 9 additions & 2 deletions chrome/js/page/occupy.js
@@ -1,15 +1,18 @@
$.noConflict(); // Play nice!
try {$.noConflict();} catch(e) {} // Play nice!


if (typeof(OccupyInternetPage) == 'undefined') {
var OccupyInternetPage = {
count : 0,
fetched : false,
gathered : false,
hidden : false,
app_url : 'http://api.occupyinter.net',
post_url : 'http://api.occupyinter.net/count.json',
get_url : 'http://api.occupyinter.net/site.json'
};

jQuery('.occupyinternet').remove();
}


Expand All @@ -31,7 +34,7 @@ jQuery.extend(true, OccupyInternetPage, {
// Repeat it back (success)
_mic_check : function(msg) {
OccupyInternetPage.count = msg.visits_count;
OccupyInternetProtest.protest();
OccupyInternetPage.protest();
},

// Getting some feedback from the mic (error)
Expand All @@ -43,6 +46,7 @@ jQuery.extend(true, OccupyInternetPage, {
protest : function() {
if (!OccupyInternetPage.gathered) {
jQuery('body').append(OccupyInternetPage.html.liberty_plaza);
if (!!OccupyInternetPage.hidden) jQuery('#occupyinternet_plaza').hide();
OccupyInternetPage.gathered = true;

for (var i=0; i<OccupyInternetPage.count; i++) {
Expand All @@ -51,6 +55,9 @@ jQuery.extend(true, OccupyInternetPage, {

}
},

_show_protest : function() {if (jQuery('#occupyinternet_plaza')) jQuery('#occupyinternet_plaza').show();},
_hide_protest : function() {if (jQuery('#occupyinternet_plaza')) jQuery('#occupyinternet_plaza').hide();},

_add_protester : function() {
jQuery('#occupyinternet_plaza').append(OccupyInternetPage.html.protester);
Expand Down

0 comments on commit 8d50796

Please sign in to comment.