Browse Source

Code styling

pull/2/head
Deathamns 10 years ago
committed by gorhill
parent
commit
2a2fa8da75
  1. 66
      platform/firefox/vapi-background.js

66
platform/firefox/vapi-background.js

@ -55,8 +55,8 @@ vAPI.app.restart = function() {};
/******************************************************************************/ /******************************************************************************/
// list of things that needs to be destroyed when disabling the extension
// only functions should be added to it
// List of things that needs to be destroyed when disabling the extension
// Only functions should be added to it
vAPI.unload = []; vAPI.unload = [];
@ -88,6 +88,7 @@ var SQLite = {
SQLite.db.asyncClose(); SQLite.db.asyncClose();
}); });
}, },
run: function(query, values, callback) { run: function(query, values, callback) {
if ( !this.db ) { if ( !this.db ) {
this.open(); this.open();
@ -135,6 +136,7 @@ var SQLite = {
vAPI.storage = { vAPI.storage = {
QUOTA_BYTES: 100 * 1024 * 1024, QUOTA_BYTES: 100 * 1024 * 1024,
sqlWhere: function(col, params) { sqlWhere: function(col, params) {
if ( params > 0 ) { if ( params > 0 ) {
params = Array(params + 1).join('?, ').slice(0, -2); params = Array(params + 1).join('?, ').slice(0, -2);
@ -143,6 +145,7 @@ vAPI.storage = {
return ''; return '';
}, },
get: function(details, callback) { get: function(details, callback) {
if ( typeof callback !== 'function' ) { if ( typeof callback !== 'function' ) {
return; return;
@ -153,12 +156,10 @@ vAPI.storage = {
if ( details !== null ) { if ( details !== null ) {
if ( Array.isArray(details) ) { if ( Array.isArray(details) ) {
values = details; values = details;
}
else if (typeof details === 'object') {
} else if ( typeof details === 'object' ) {
defaults = true; defaults = true;
values = Object.keys(details); values = Object.keys(details);
}
else {
} else {
values = [details.toString()]; values = [details.toString()];
} }
} }
@ -185,6 +186,7 @@ vAPI.storage = {
} }
); );
}, },
set: function(details, callback) { set: function(details, callback) {
var key, values = [], placeholders = []; var key, values = [], placeholders = [];
@ -205,6 +207,7 @@ vAPI.storage = {
callback callback
); );
}, },
remove: function(keys, callback) { remove: function(keys, callback) {
if ( typeof keys === 'string' ) { if ( typeof keys === 'string' ) {
keys = [keys]; keys = [keys];
@ -216,10 +219,12 @@ vAPI.storage = {
callback callback
); );
}, },
clear: function(callback) { clear: function(callback) {
SQLite.run('DELETE FROM settings'); SQLite.run('DELETE FROM settings');
SQLite.run('VACUUM', null, callback); SQLite.run('VACUUM', null, callback);
}, },
getBytesInUse: function(keys, callback) { getBytesInUse: function(keys, callback) {
if ( typeof callback !== 'function' ) { if ( typeof callback !== 'function' ) {
return; return;
@ -244,12 +249,14 @@ var windowWatcher = {
vAPI.tabs.onClosed(tabId); vAPI.tabs.onClosed(tabId);
delete vAPI.tabIcons[tabId]; delete vAPI.tabIcons[tabId];
}, },
onTabSelect: function(e) { onTabSelect: function(e) {
vAPI.setIcon( vAPI.setIcon(
vAPI.tabs.getTabId(e.target), vAPI.tabs.getTabId(e.target),
e.target.ownerDocument.defaultView e.target.ownerDocument.defaultView
); );
}, },
onReady: function(e) { onReady: function(e) {
if ( e ) { if ( e ) {
this.removeEventListener(e.type, windowWatcher.onReady); this.removeEventListener(e.type, windowWatcher.onReady);
@ -276,6 +283,7 @@ var windowWatcher = {
// when new window is opened TabSelect doesn't run on the selected tab? // when new window is opened TabSelect doesn't run on the selected tab?
}, },
observe: function(win, topic) { observe: function(win, topic) {
if ( topic === 'domwindowopened' ) { if ( topic === 'domwindowopened' ) {
win.addEventListener('DOMContentLoaded', this.onReady); win.addEventListener('DOMContentLoaded', this.onReady);
@ -299,8 +307,7 @@ var tabsProgressListener = {
tabId: tabId, tabId: tabId,
url: browser.currentURI.spec url: browser.currentURI.spec
}); });
}
else {
} else {
vAPI.tabs.onNavigation({ vAPI.tabs.onNavigation({
frameId: 0, frameId: 0,
tabId: tabId, tabId: tabId,
@ -386,8 +393,7 @@ vAPI.tabs.get = function(tabId, callback) {
if ( tabId === null ) { if ( tabId === null ) {
tab = Services.wm.getMostRecentWindow('navigator:browser').gBrowser.selectedTab; tab = Services.wm.getMostRecentWindow('navigator:browser').gBrowser.selectedTab;
tabId = vAPI.tabs.getTabId(tab); tabId = vAPI.tabs.getTabId(tab);
}
else {
} else {
windows = this.getWindows(); windows = this.getWindows();
for ( var win of windows ) { for ( var win of windows ) {
@ -402,7 +408,7 @@ vAPI.tabs.get = function(tabId, callback) {
} }
// for internal use // for internal use
if (tab && callback === undefined) {
if ( tab && typeof callback !== 'function' ) {
return tab; return tab;
} }
@ -477,7 +483,7 @@ vAPI.tabs.open = function(details) {
return null; return null;
} }
// extension pages // extension pages
if (!/^[\w-]{2,}:/.test(details.url)) {
if ( /^[\w-]{2,}:/.test(details.url) === false ) {
details.url = vAPI.getURL(details.url); details.url = vAPI.getURL(details.url);
} }
@ -568,7 +574,7 @@ vAPI.tabs.injectScript = function(tabId, details, callback) {
location.host + ':broadcast', location.host + ':broadcast',
JSON.stringify({ JSON.stringify({
broadcast: true, broadcast: true,
portName: 'vAPI',
channelName: 'vAPI',
msg: { msg: {
cmd: 'injectScript', cmd: 'injectScript',
details: details details: details
@ -594,8 +600,7 @@ vAPI.setIcon = function(tabId, iconStatus, badge) {
// from 'TabSelect' event // from 'TabSelect' event
if ( tabId === undefined ) { if ( tabId === undefined ) {
tabId = curTabId; tabId = curTabId;
}
else if (badge !== undefined) {
} else if ( badge !== undefined ) {
vAPI.tabIcons[tabId] = { vAPI.tabIcons[tabId] = {
badge: badge, badge: badge,
img: iconStatus === 'on' img: iconStatus === 'on'
@ -793,9 +798,9 @@ vAPI.messaging.onMessage = function({target, data}) {
.chromeEventHandler.ownerDocument.defaultView.messageManager; .chromeEventHandler.ownerDocument.defaultView.messageManager;
} }
var listenerId = data.portName.split('|');
var listenerId = data.channelName.split('|');
var requestId = data.requestId; var requestId = data.requestId;
var portName = listenerId[1];
var channelName = listenerId[1];
listenerId = listenerId[0]; listenerId = listenerId[0];
var callback = vAPI.messaging.NOOPFUNC; var callback = vAPI.messaging.NOOPFUNC;
@ -803,14 +808,13 @@ vAPI.messaging.onMessage = function({target, data}) {
callback = function(response) { callback = function(response) {
var message = JSON.stringify({ var message = JSON.stringify({
requestId: requestId, requestId: requestId,
portName: portName,
channelName: channelName,
msg: response !== undefined ? response : null msg: response !== undefined ? response : null
}); });
if ( messageManager.sendAsyncMessage ) { if ( messageManager.sendAsyncMessage ) {
messageManager.sendAsyncMessage(listenerId, message); messageManager.sendAsyncMessage(listenerId, message);
}
else {
} else {
messageManager.broadcastAsyncMessage(listenerId, message); messageManager.broadcastAsyncMessage(listenerId, message);
} }
}; };
@ -824,7 +828,7 @@ vAPI.messaging.onMessage = function({target, data}) {
// Specific handler // Specific handler
var r = vAPI.messaging.UNHANDLED; var r = vAPI.messaging.UNHANDLED;
var listener = vAPI.messaging.listeners[portName];
var listener = vAPI.messaging.listeners[channelName];
if ( typeof listener === 'function' ) { if ( typeof listener === 'function' ) {
r = listener(data.msg, sender, callback); r = listener(data.msg, sender, callback);
} }
@ -897,6 +901,7 @@ var httpObserver = {
frameId: null, frameId: null,
parentFrameId: null parentFrameId: null
}, },
QueryInterface: (function() { QueryInterface: (function() {
var {XPCOMUtils} = Cu['import']('resource://gre/modules/XPCOMUtils.jsm', {}); var {XPCOMUtils} = Cu['import']('resource://gre/modules/XPCOMUtils.jsm', {});
return XPCOMUtils.generateQI([ return XPCOMUtils.generateQI([
@ -904,16 +909,19 @@ var httpObserver = {
Ci.nsISupportsWeakReference Ci.nsISupportsWeakReference
]); ]);
})(), })(),
register: function() { register: function() {
Services.obs.addObserver(httpObserver, 'http-on-opening-request', true); Services.obs.addObserver(httpObserver, 'http-on-opening-request', true);
// Services.obs.addObserver(httpObserver, 'http-on-modify-request', true); // Services.obs.addObserver(httpObserver, 'http-on-modify-request', true);
Services.obs.addObserver(httpObserver, 'http-on-examine-response', true); Services.obs.addObserver(httpObserver, 'http-on-examine-response', true);
}, },
unregister: function() { unregister: function() {
Services.obs.removeObserver(httpObserver, 'http-on-opening-request'); Services.obs.removeObserver(httpObserver, 'http-on-opening-request');
// Services.obs.removeObserver(httpObserver, 'http-on-modify-request'); // Services.obs.removeObserver(httpObserver, 'http-on-modify-request');
Services.obs.removeObserver(httpObserver, 'http-on-examine-response'); Services.obs.removeObserver(httpObserver, 'http-on-examine-response');
}, },
observe: function(httpChannel, topic) { observe: function(httpChannel, topic) {
// No need for QueryInterface if this check is performed? // No need for QueryInterface if this check is performed?
if ( !(httpChannel instanceof Ci.nsIHttpChannel) ) { if ( !(httpChannel instanceof Ci.nsIHttpChannel) ) {
@ -1003,8 +1011,7 @@ var httpObserver = {
if ( result.cancel === true ) { if ( result.cancel === true ) {
httpChannel.cancel(this.ABORT); httpChannel.cancel(this.ABORT);
}
else if (result.redirectUrl) {
} else if ( result.redirectUrl ) {
httpChannel.redirectionLimit = 1; httpChannel.redirectionLimit = 1;
httpChannel.redirectTo( httpChannel.redirectTo(
Services.io.newURI(result.redirectUrl, null, null) Services.io.newURI(result.redirectUrl, null, null)
@ -1079,7 +1086,7 @@ vAPI.contextMenu.displayMenuItem = function(e) {
var gContextMenu = doc.defaultView.gContextMenu; var gContextMenu = doc.defaultView.gContextMenu;
var menuitem = doc.getElementById(vAPI.contextMenu.menuItemId); var menuitem = doc.getElementById(vAPI.contextMenu.menuItemId);
if (!/^https?$/.test(gContextMenu.browser.currentURI.scheme)) {
if ( /^https?$/.test(gContextMenu.browser.currentURI.scheme) === false) {
menuitem.hidden = true; menuitem.hidden = true;
return; return;
} }
@ -1155,8 +1162,7 @@ vAPI.contextMenu.create = function(details, callback) {
if ( Array.isArray(this.contexts) && this.contexts.length ) { if ( Array.isArray(this.contexts) && this.contexts.length ) {
this.contexts = this.contexts.indexOf('all') === -1 ? this.contexts : null; this.contexts = this.contexts.indexOf('all') === -1 ? this.contexts : null;
}
else {
} else {
// default in Chrome // default in Chrome
this.contexts = ['page']; this.contexts = ['page'];
} }
@ -1170,13 +1176,9 @@ vAPI.contextMenu.create = function(details, callback) {
if ( gContextMenu.inFrame ) { if ( gContextMenu.inFrame ) {
details.frameUrl = gContextMenu.focusedWindow.location.href; details.frameUrl = gContextMenu.focusedWindow.location.href;
}
else if (gContextMenu.onImage
|| gContextMenu.onAudio
|| gContextMenu.onVideo) {
} else if ( gContextMenu.onImage || gContextMenu.onAudio || gContextMenu.onVideo ) {
details.srcUrl = gContextMenu.mediaURL; details.srcUrl = gContextMenu.mediaURL;
}
else if (gContextMenu.onLink) {
} else if ( gContextMenu.onLink ) {
details.linkUrl = gContextMenu.linkURL; details.linkUrl = gContextMenu.linkURL;
} }

Loading…
Cancel
Save