Browse Source

toward a release: general code review, polishing, removing obsolete stuff

pull/2/head
gorhill 10 years ago
parent
commit
8aaadb8fc0
  1. 14
      platform/firefox/bootstrap.js
  2. 2
      platform/firefox/vapi-background.js
  3. 33
      src/js/background.js
  4. 9
      src/js/cookies.js
  5. 42
      src/js/logger-ui.js
  6. 3
      src/js/messaging.js
  7. 43
      src/js/popup.js
  8. 9
      src/js/start.js
  9. 2
      src/js/tab.js
  10. 1
      src/js/user-rules.js
  11. 2
      src/logger-ui.html

14
platform/firefox/bootstrap.js

@ -19,7 +19,7 @@
Home: https://github.com/gorhill/uMatrix Home: https://github.com/gorhill/uMatrix
*/ */
/* global APP_SHUTDOWN, APP_STARTUP */
/* global ADDON_UNINSTALL, APP_SHUTDOWN, APP_STARTUP */
/* exported startup, shutdown, install, uninstall */ /* exported startup, shutdown, install, uninstall */
'use strict'; 'use strict';
@ -141,6 +141,16 @@ function install() {
/******************************************************************************/ /******************************************************************************/
function uninstall() {}
function uninstall(data, aReason) {
if ( aReason !== ADDON_UNINSTALL ) {
return;
}
// To cleanup vAPI.localStorage in vapi-common.js, aka
// "extensions.umatrix.*" in `about:config`.
Components.utils.import('resource://gre/modules/Services.jsm', null)
.Services.prefs
.getBranch('extensions.' + hostName + '.')
.deleteBranch('');
}
/******************************************************************************/ /******************************************************************************/

2
platform/firefox/vapi-background.js

@ -110,6 +110,8 @@ window.addEventListener('unload', function() {
/******************************************************************************/ /******************************************************************************/
var SQLite = { var SQLite = {
db: null,
open: function() { open: function() {
var path = Services.dirsvc.get('ProfD', Ci.nsIFile); var path = Services.dirsvc.get('ProfD', Ci.nsIFile);
path.append('extension-data'); path.append('extension-data');

33
src/js/background.js

@ -35,16 +35,18 @@ var oneDay = 24 * oneHour;
/******************************************************************************/ /******************************************************************************/
var defaultUserAgentStrings = [ var defaultUserAgentStrings = [
'# http://www.useragentstring.com/pages/Chrome/',
'# http://techblog.willshouse.com/2012/01/03/most-common-user-agents/', '# http://techblog.willshouse.com/2012/01/03/most-common-user-agents/',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/600.1.17 (KHTML, like Gecko) Version/7.1 Safari/537.85.10',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10) AppleWebKit/600.1.25 (KHTML, like Gecko) Version/8.0 Safari/600.1.25',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36',
'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36'
].join('\n');
'# using ua string which are same browser as real one may work better overall',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36',
'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/600.5.17 (KHTML, like Gecko) Version/8.0.5 Safari/600.5.17',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36',
'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36',
'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0',
''
].join('\n').trim();
/******************************************************************************/ /******************************************************************************/
@ -63,17 +65,13 @@ return {
externalHostsFiles: '', externalHostsFiles: '',
iconBadgeEnabled: false, iconBadgeEnabled: false,
maxLoggedRequests: 1000, maxLoggedRequests: 1000,
popupCollapseDomains: false,
popupCollapseSpecificDomains: {},
popupHideBlacklisted: false,
popupScopeLevel: 'domain', popupScopeLevel: 'domain',
processBehindTheSceneRequests: false, processBehindTheSceneRequests: false,
processHyperlinkAuditing: true, processHyperlinkAuditing: true,
processReferer: false, processReferer: false,
spoofUserAgent: false, spoofUserAgent: false,
spoofUserAgentEvery: 5, spoofUserAgentEvery: 5,
spoofUserAgentWith: defaultUserAgentStrings,
statsFilters: {}
spoofUserAgentWith: defaultUserAgentStrings
}, },
clearBrowserCacheCycle: 0, clearBrowserCacheCycle: 0,
@ -114,14 +112,9 @@ return {
userAgentReplaceStr: '', userAgentReplaceStr: '',
userAgentReplaceStrBirth: 0, userAgentReplaceStrBirth: 0,
// record what chromium is doing behind the scene
behindTheSceneURL: 'http://behind-the-scene/',
behindTheSceneMaxReq: 250,
// record what the browser is doing behind the scene
behindTheSceneScope: 'behind-the-scene', behindTheSceneScope: 'behind-the-scene',
// Commonly encountered strings
fontCSSURL: vAPI.getURL('css/fonts/Roboto_Condensed/RobotoCondensed-Regular.ttf'),
noopFunc: function(){}, noopFunc: function(){},
// so that I don't have to care for last comma // so that I don't have to care for last comma

9
src/js/cookies.js

@ -292,21 +292,18 @@ var chromeCookieRemove = function(url, name) {
var persistCookieKey = cookieKeyFromCookieURL(url, 'persistent', name); var persistCookieKey = cookieKeyFromCookieURL(url, 'persistent', name);
var callback = function(details) { var callback = function(details) {
var success = !!details; var success = !!details;
var template = success ? i18nCookieDeleteSuccess : i18nCookieDeleteFailure;
if ( removeCookieFromDict(sessionCookieKey) ) { if ( removeCookieFromDict(sessionCookieKey) ) {
if ( success ) { if ( success ) {
µm.logger.writeOne('', 'info', i18nCookieDeleteSuccess.replace('{{value}}', sessionCookieKey));
µm.cookieRemovedCounter += 1; µm.cookieRemovedCounter += 1;
} else {
µm.logger.writeOne('', 'error', i18nCookieDeleteFailure.replace('{{value}}', sessionCookieKey));
} }
µm.logger.writeOne('', 'info', 'cookie', template.replace('{{value}}', sessionCookieKey));
} }
if ( removeCookieFromDict(persistCookieKey) ) { if ( removeCookieFromDict(persistCookieKey) ) {
if ( success ) { if ( success ) {
µm.logger.writeOne('', 'info', i18nCookieDeleteSuccess.replace('{{value}}', persistCookieKey));
µm.cookieRemovedCounter += 1; µm.cookieRemovedCounter += 1;
} else {
µm.logger.writeOne('', 'error', i18nCookieDeleteFailure.replace('{{value}}', persistCookieKey));
} }
µm.logger.writeOne('', 'info', 'cookie', template.replace('{{value}}', persistCookieKey));
} }
}; };

42
src/js/logger-ui.js

@ -71,9 +71,37 @@ var reEscapeRightBracket = />/g;
/******************************************************************************/ /******************************************************************************/
// Emphasize hostname in URL, as this is what matters in uMatrix's rules.
// Emphasize hostname and cookie name.
var emphasizeCookie = function(s) {
var pnode = emphasizeHostname(s);
if ( pnode.childNodes.length !== 3 ) {
return pnode;
}
var prefix = '-cookie:';
var text = pnode.childNodes[2].textContent;
var beg = text.indexOf(prefix);
if ( beg === -1 ) {
return pnode;
}
beg += prefix.length;
var end = text.indexOf('}', beg);
if ( end === -1 ) {
return pnode;
}
var cnode = emphasizeTemplate.cloneNode(true);
cnode.childNodes[0].textContent = text.slice(0, beg);
cnode.childNodes[1].textContent = text.slice(beg, end);
cnode.childNodes[2].textContent = text.slice(end);
pnode.replaceChild(cnode, pnode.childNodes[2]);
return pnode;
};
/******************************************************************************/
var nodeFromURL = function(url) {
// Emphasize hostname in URL.
var emphasizeHostname = function(url) {
var hnbeg = url.indexOf('://'); var hnbeg = url.indexOf('://');
if ( hnbeg === -1 ) { if ( hnbeg === -1 ) {
return document.createTextNode(url); return document.createTextNode(url);
@ -90,14 +118,14 @@ var nodeFromURL = function(url) {
} }
} }
var node = renderedURLTemplate.cloneNode(true);
var node = emphasizeTemplate.cloneNode(true);
node.childNodes[0].textContent = url.slice(0, hnbeg); node.childNodes[0].textContent = url.slice(0, hnbeg);
node.childNodes[1].textContent = url.slice(hnbeg, hnend); node.childNodes[1].textContent = url.slice(hnbeg, hnend);
node.childNodes[2].textContent = url.slice(hnend); node.childNodes[2].textContent = url.slice(hnend);
return node; return node;
}; };
var renderedURLTemplate = document.querySelector('#renderedURLTemplate > span');
var emphasizeTemplate = document.querySelector('#emphasizeTemplate > span');
/******************************************************************************/ /******************************************************************************/
@ -180,7 +208,11 @@ var renderLogEntry = function(entry) {
case 'error': case 'error':
case 'info': case 'info':
tr = createRow('1'); tr = createRow('1');
if ( entry.d0 === 'cookie' ) {
tr.cells[fvdc].appendChild(emphasizeCookie(entry.d1));
} else {
tr.cells[fvdc].textContent = entry.d0; tr.cells[fvdc].textContent = entry.d0;
}
break; break;
case 'net': case 'net':
@ -198,7 +230,7 @@ var renderLogEntry = function(entry) {
tr.cells[fvdc].textContent = ''; tr.cells[fvdc].textContent = '';
} }
tr.cells[fvdc+1].textContent = (prettyRequestTypes[entry.d2] || entry.d2); tr.cells[fvdc+1].textContent = (prettyRequestTypes[entry.d2] || entry.d2);
tr.cells[fvdc+2].appendChild(nodeFromURL(entry.d1));
tr.cells[fvdc+2].appendChild(emphasizeHostname(entry.d1));
break; break;
default: default:

3
src/js/messaging.js

@ -164,9 +164,6 @@ var matrixSnapshot = function(pageStore, details) {
userSettings: { userSettings: {
colorBlindFriendly: µmuser.colorBlindFriendly, colorBlindFriendly: µmuser.colorBlindFriendly,
displayTextSize: µmuser.displayTextSize, displayTextSize: µmuser.displayTextSize,
popupCollapseDomains: µmuser.popupCollapseDomains,
popupCollapseSpecificDomains: µmuser.popupCollapseSpecificDomains,
popupHideBlacklisted: µmuser.popupHideBlacklisted,
popupScopeLevel: µmuser.popupScopeLevel popupScopeLevel: µmuser.popupScopeLevel
} }
}; };

43
src/js/popup.js

@ -87,6 +87,23 @@ function setUserSetting(setting, value) {
/******************************************************************************/ /******************************************************************************/
function getUISetting(setting) {
var r = vAPI.localStorage.getItem(setting);
if ( typeof r !== 'string' ) {
return undefined;
}
return JSON.parse(r);
}
function setUISetting(setting, value) {
vAPI.localStorage.setItem(
setting,
JSON.stringify(value)
);
}
/******************************************************************************/
function updateMatrixSnapshot() { function updateMatrixSnapshot() {
matrixSnapshotPoller.pollNow(); matrixSnapshotPoller.pollNow();
} }
@ -258,11 +275,11 @@ function getCellClass(hostname, type) {
// want to lose all his hardwork. // want to lose all his hardwork.
function getCollapseState(domain) { function getCollapseState(domain) {
var states = getUserSetting('popupCollapseSpecificDomains');
if ( states !== undefined && states[domain] !== undefined ) {
var states = getUISetting('popupCollapseSpecificDomains');
if ( typeof states === 'object' && states[domain] !== undefined ) {
return states[domain]; return states[domain];
} }
return getUserSetting('popupCollapseDomains');
return getUISetting('popupCollapseDomains');
} }
function toggleCollapseState(elem) { function toggleCollapseState(elem) {
@ -277,9 +294,9 @@ function toggleMainCollapseState(uelem) {
var matHead = uelem.ancestors('#matHead.collapsible').toggleClass('collapsed'); var matHead = uelem.ancestors('#matHead.collapsible').toggleClass('collapsed');
var collapsed = matHead.hasClass('collapsed'); var collapsed = matHead.hasClass('collapsed');
uDom('#matList .matSection.collapsible').toggleClass('collapsed', collapsed); uDom('#matList .matSection.collapsible').toggleClass('collapsed', collapsed);
setUserSetting('popupCollapseDomains', collapsed);
setUISetting('popupCollapseDomains', collapsed);
var specificCollapseStates = getUserSetting('popupCollapseSpecificDomains') || {};
var specificCollapseStates = getUISetting('popupCollapseSpecificDomains') || {};
var domains = Object.keys(specificCollapseStates); var domains = Object.keys(specificCollapseStates);
var i = domains.length; var i = domains.length;
var domain; var domain;
@ -289,7 +306,7 @@ function toggleMainCollapseState(uelem) {
delete specificCollapseStates[domain]; delete specificCollapseStates[domain];
} }
} }
setUserSetting('popupCollapseSpecificDomains', specificCollapseStates);
setUISetting('popupCollapseSpecificDomains', specificCollapseStates);
} }
function toggleSpecificCollapseState(uelem) { function toggleSpecificCollapseState(uelem) {
@ -298,14 +315,14 @@ function toggleSpecificCollapseState(uelem) {
var section = uelem.ancestors('.matSection.collapsible').toggleClass('collapsed'); var section = uelem.ancestors('.matSection.collapsible').toggleClass('collapsed');
var domain = section.prop('domain'); var domain = section.prop('domain');
var collapsed = section.hasClass('collapsed'); var collapsed = section.hasClass('collapsed');
var mainCollapseState = getUserSetting('popupCollapseDomains');
var specificCollapseStates = getUserSetting('popupCollapseSpecificDomains') || {};
var mainCollapseState = getUISetting('popupCollapseDomains');
var specificCollapseStates = getUISetting('popupCollapseSpecificDomains') || {};
if ( collapsed !== mainCollapseState ) { if ( collapsed !== mainCollapseState ) {
specificCollapseStates[domain] = collapsed; specificCollapseStates[domain] = collapsed;
setUserSetting('popupCollapseSpecificDomains', specificCollapseStates);
setUISetting('popupCollapseSpecificDomains', specificCollapseStates);
} else if ( specificCollapseStates[domain] !== undefined ) { } else if ( specificCollapseStates[domain] !== undefined ) {
delete specificCollapseStates[domain]; delete specificCollapseStates[domain];
setUserSetting('popupCollapseSpecificDomains', specificCollapseStates);
setUISetting('popupCollapseSpecificDomains', specificCollapseStates);
} }
} }
@ -495,7 +512,7 @@ var createMatrixRow = function() {
function renderMatrixHeaderRow() { function renderMatrixHeaderRow() {
var matHead = uDom('#matHead.collapsible'); var matHead = uDom('#matHead.collapsible');
matHead.toggleClass('collapsed', getUserSetting('popupCollapseDomains'));
matHead.toggleClass('collapsed', getUISetting('popupCollapseDomains'));
var cells = matHead.descendants('.matCell'); var cells = matHead.descendants('.matCell');
cells.at(0) cells.at(0)
.prop('reqType', '*') .prop('reqType', '*')
@ -902,7 +919,7 @@ function makeMatrixGroup4(group) {
var groupDiv = createMatrixGroup().addClass('g4'); var groupDiv = createMatrixGroup().addClass('g4');
createMatrixSection() createMatrixSection()
.addClass('g4Meta') .addClass('g4Meta')
.toggleClass('g4Collapsed', !!getUserSetting('popupHideBlacklisted'))
.toggleClass('g4Collapsed', !!getUISetting('popupHideBlacklisted'))
.appendTo(groupDiv); .appendTo(groupDiv);
makeMatrixMetaRow(computeMatrixGroupMetaStats(group), 'g4') makeMatrixMetaRow(computeMatrixGroupMetaStats(group), 'g4')
.appendTo(groupDiv); .appendTo(groupDiv);
@ -1332,7 +1349,7 @@ uDom('#matList').on('click', '.g4Meta', function() {
var collapsed = uDom(this) var collapsed = uDom(this)
.toggleClass('g4Collapsed') .toggleClass('g4Collapsed')
.hasClass('g4Collapsed'); .hasClass('g4Collapsed');
setUserSetting('popupHideBlacklisted', collapsed);
setUISetting('popupHideBlacklisted', collapsed);
}); });
/******************************************************************************/ /******************************************************************************/

9
src/js/start.js

@ -131,6 +131,15 @@ var onHostsFilesLoaded = function() {
}; };
var onUserSettingsLoaded = function() { var onUserSettingsLoaded = function() {
// Version 0.9.0.0
// Remove obsolete user settings which may have been loaded.
// These are now stored as local settings:
delete µm.userSettings.popupCollapseDomains;
delete µm.userSettings.popupCollapseSpecificDomains;
delete µm.userSettings.popupHideBlacklisted;
// These do not exist anymore:
delete µm.userSettings.statsFilters;
µm.loadHostsFiles(onHostsFilesLoaded); µm.loadHostsFiles(onHostsFilesLoaded);
}; };

2
src/js/tab.js

@ -47,7 +47,7 @@ var µm = µMatrix;
µm.normalizePageURL = function(tabId, pageURL) { µm.normalizePageURL = function(tabId, pageURL) {
if ( vAPI.isBehindTheSceneTabId(tabId) ) { if ( vAPI.isBehindTheSceneTabId(tabId) ) {
return 'http://behind-the-scene/';
return 'http://' + this.behindTheSceneScope + '/';
} }
// If the URL is that of our "blocked page" document, return the URL of // If the URL is that of our "blocked page" document, return the URL of

1
src/js/user-rules.js

@ -236,7 +236,6 @@ uDom.onLoad(function() {
uDom('#importButton').on('click', startImportFilePicker); uDom('#importButton').on('click', startImportFilePicker);
uDom('#importFilePicker').on('change', handleImportFilePicker); uDom('#importFilePicker').on('change', handleImportFilePicker);
uDom('#exportButton').on('click', exportUserRulesToFile); uDom('#exportButton').on('click', exportUserRulesToFile);
uDom('#revertButton').on('click', revertHandler); uDom('#revertButton').on('click', revertHandler);
uDom('#commitButton').on('click', commitHandler); uDom('#commitButton').on('click', commitHandler);
uDom('#editEnterButton').on('click', editStartHandler); uDom('#editEnterButton').on('click', editStartHandler);

2
src/logger-ui.html

@ -29,7 +29,7 @@
</div> </div>
<div style="display: none;"> <div style="display: none;">
<div id="renderedURLTemplate"><span><span></span><b></b><span></span></span></div>
<div id="emphasizeTemplate"><span><span></span><b></b><span></span></span></div>
</div> </div>
<script src="js/vapi-common.js"></script> <script src="js/vapi-common.js"></script>

Loading…
Cancel
Save