Skip to content

Commit

Permalink
Round trip global excludes through options.js.
Browse files Browse the repository at this point in the history
Saved in local storage, passed from bg -> monkey menu -> bg.

Refs greasemonkey#2843
  • Loading branch information
arantius committed Jun 29, 2018
1 parent 3ae55ca commit a9debd5
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 30 deletions.
10 changes: 9 additions & 1 deletion doc/Messages.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,15 @@ Response data:

* An array of `.details` objects from installed `RunnableUserScript`s.

# SaveOptions
# OptionsLoad
Sent by: `browser/monkey-menu.js`
Received by: `bg/options.js`

Returns previously saved options data. Result is the same format as `OptionsSave`'s request.

* `excludes` A string, one `@exclude` pattern per line.

# OptionsSave
Sent by: `browser/monkey-menu.js`
Received by: `bg/options.js`

Expand Down
57 changes: 44 additions & 13 deletions src/bg/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,45 @@ of Greasemonkey.
// Private implementation.
(function() {

let isEnabled = true;
let gIsEnabled = true;
chrome.storage.local.get('globalEnabled', v => {
isEnabled = v['globalEnabled'];
if ('undefined' == typeof isEnabled) isEnabled = true;
gIsEnabled = v['globalEnabled'];
if ('undefined' == typeof gIsEnabled) gIsEnabled = true;
setIcon();
});

let gGlobalExcludes = [];
chrome.storage.local.get('globalExcludes', v => {
let str = v['globalExcludes'];
if ('undefined' != typeof str) {
gGlobalExcludes = str.split('\n');
}
});


function getGlobalEnabled() {
return !!isEnabled;
return !!gIsEnabled;
}
window.getGlobalEnabled = getGlobalEnabled;


function getGlobalExcludes() {
return gGlobalExcludes.slice();
}
window.getGlobalExcludes = getGlobalExcludes;


function onEnabledQuery(message, sender, sendResponse) {
sendResponse(isEnabled);
sendResponse(gIsEnabled);
}
window.onEnabledQuery = onEnabledQuery;


function setGlobalEnabled(enabled) {
isEnabled = !!enabled;
gIsEnabled = !!enabled;
chrome.runtime.sendMessage({
'name': 'EnabledChanged',
'enabled': isEnabled,
'enabled': gIsEnabled,
}, logUnhandledError);
setIcon();
chrome.storage.local.set({'globalEnabled': enabled});
Expand All @@ -50,7 +64,7 @@ function setIcon() {
return;
}
let iconPath = chrome.extension.getURL('skin/icon.svg');
if (isEnabled) {
if (gIsEnabled) {
chrome.browserAction.setIcon({'path': iconPath});
} else {
let img = document.createElement('img');
Expand All @@ -69,17 +83,34 @@ function setIcon() {


function toggleGlobalEnabled() {
setGlobalEnabled(!isEnabled);
setGlobalEnabled(!gIsEnabled);
}
window.toggleGlobalEnabled = toggleGlobalEnabled;

/*****************************************************************************/

function onEnabledToggle(message, sender, sendResponse) {
try {
toggleGlobalEnabled();
sendResponse(isEnabled);
} catch (e) { console.error(e); }
toggleGlobalEnabled();
sendResponse(gIsEnabled);
}
window.onEnabledToggle = onEnabledToggle;


function onOptionsLoad(message, sender, sendResponse) {
let options = {
'excludes': gGlobalExcludes.join('\n'),
};
sendResponse(options);
}
window.onOptionsLoad = onOptionsLoad;


function onOptionsSave(message, sender, sendResponse) {
chrome.storage.local.set(
{'globalExcludes': message.excludes},
logUnhandledError);
gGlobalExcludes = message.excludes.split('\n');
}
window.onOptionsSave = onOptionsSave;

})();
1 change: 1 addition & 0 deletions src/browser/monkey-menu.css
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,6 @@ section.options .explain a {

section.options textarea {
font-family: monospace;
font-size: 90%;
white-space: pre;
}
2 changes: 1 addition & 1 deletion src/browser/monkey-menu.html
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@
</a>
{'global_excludes_explain'|i18n}
</p>
<p><textarea rows="5"></textarea></p>
<p><textarea rows="5" rv-value="options.globalExcludesStr"></textarea></p>
</section>


Expand Down
77 changes: 63 additions & 14 deletions src/browser/monkey-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,31 @@
let gTplData = {
'activeScript': {},
'enabled': undefined,
'pendingUninstall': 0,
'options': {
'globalExcludesStr': '',
},
'userScripts': {
'active': [],
'inactive': [],
},
'pendingUninstall': 0,
};
let gMainFocusedItem = null;
let gPendingTicker = null;
let gScriptTemplates = {};

///////////////////////////////////////////////////////////////////////////////

if ('undefined' == typeof window.getGlobalExcludes) {
// Equivalent of the version provided by `user-script-registry`, but here.
// Undefined check because of shared test scope collision.
window.getGlobalExcludes = function() {
return gTplData.options.globalExcludesStr.split('\n');
}
}

///////////////////////////////////////////////////////////////////////////////

function onContextMenu(event) {
event.preventDefault();
event.stopPropagation();
Expand Down Expand Up @@ -45,24 +58,57 @@ function onKeyDown(event) {

function onLoad() {
gPendingTicker = setInterval(pendingUninstallTicker, 1000);

let tabs = null;
let userScripts = null;
function finish() {
numPending--;
if (numPending > 0) return;

let url = tabs.length && new URL(tabs[0].url) || null;
loadScripts(userScripts, url);

tinybind.formatters.bothArraysEmpty
= (a, b) => !(!!a.length || !!b.length);
tinybind.bind(document.body, gTplData);

document.body.id = 'main-menu';

setTimeout(window.focus, 0);
}

let numPending = 0;

numPending++;
chrome.runtime.sendMessage(
{'name': 'EnabledQuery'},
enabled => gTplData.enabled = enabled);
enabled => {
gTplData.enabled = enabled;
finish();
});

numPending++;
chrome.runtime.sendMessage(
{'name': 'ListUserScripts', 'includeDisabled': true},
function(userScripts) {
chrome.tabs.query({'active': true, 'currentWindow': true}, tabs => {
let url = tabs.length && new URL(tabs[0].url) || null;
loadScripts(userScripts, url);

tinybind.formatters.bothArraysEmpty
= (a, b) => !(!!a.length || !!b.length);
tinybind.bind(document.body, gTplData);
userScripts_ => {
userScripts = userScripts_;
finish();
});

document.body.id = 'main-menu';
numPending++;
chrome.runtime.sendMessage(
{'name': 'OptionsLoad'},
options => {
gTplData.options.globalExcludesStr = options.excludes;
finish();
});

setTimeout(window.focus, 0);
});
numPending++;
chrome.tabs.query(
{'active': true, 'currentWindow': true},
tabs_ => {
tabs = tabs_;
finish();
});
}

Expand Down Expand Up @@ -187,7 +233,10 @@ function loadScripts(userScriptsDetail, url) {
function navigateToMainMenu() {
switch (document.body.id) {
case 'options':
console.log('TODO: Save options.');
chrome.runtime.sendMessage({
'name': 'OptionsSave',
'excludes': gTplData.options.globalExcludesStr,
}, logUnhandledError);
break;
case 'user-script':
if (gTplData.pendingUninstall > 0) {
Expand Down
4 changes: 3 additions & 1 deletion src/user-script-obj.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,11 @@ window.RemoteUserScript = class RemoteUserScript {
}

// TODO: Profile cost of pattern generation, cache if justified.
// TODO: User global excludes.
// TODO: User includes/excludes/matches.

for (let glob of getGlobalExcludes()) {
if (_testClude(glob, url)) return false;
}
for (let glob of this._excludes) {
if (_testClude(glob, url)) return false;
}
Expand Down
16 changes: 16 additions & 0 deletions test/bg/options.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,20 @@ describe('bg/options', () => {
assert.equal(getGlobalEnabled(), false);
});
});

describe('global excludes', () => {
it('prevents script matching', () => {
window.onOptionsSave({'excludes': ''});
let userScript
= new EditableUserScript({'includes': ['http://example.net/*']});

assert.isTrue(userScript.runsAt(new URL('http://example.net/ruined')));
assert.isTrue(userScript.runsAt(new URL('http://example.net/weaved')));

window.onOptionsSave({'excludes': 'http://example.net/r*'});

assert.isFalse(userScript.runsAt(new URL('http://example.net/ruined')));
assert.isTrue(userScript.runsAt(new URL('http://example.net/weaved')));
});
});
});

0 comments on commit a9debd5

Please sign in to comment.