Skip to content

Commit 2ed9b8b

Browse files
committed
extend blacklist syntax to specify an element (#96)
1 parent e418dbf commit 2ed9b8b

File tree

4 files changed

+58
-24
lines changed

4 files changed

+58
-24
lines changed

src/chrome/_locales/en_US/messages.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
"description": "Exceptional URLs"
3535
},
3636
"option_qa_blacklist_tips": {
37-
"message": "* Put any URL to prevent from wasavi activation.\n* One URL per line.\n* A line starting with [c];[/c] or [c]#[/c] is treated as comment.\n* You can use wildcard [c]*[/c] and [c]?[/c] in URL.",
38-
"description": "* Put any URL to prevent from wasavi activation.\n* One URL per line.\n* A line starting with [c];[/c] or [c]#[/c] is treated as comment.\n* You can use wildcard [c]*[/c] and [c]?[/c] in URL."
37+
"message": "* Put any URL to prevent from wasavi activation.\n* One URL per line.\n* A line starting with [c];[/c] or [c]#[/c] is treated as comment.\n* You can use wildcard [c]*[/c] and [c]?[/c] in URL.\n* You can also put blank and CSS selector expression after URL, like [c]http://example.net/ [name=\"foo\"][/c]. The selector will changes a blacklisting target to an element from a page.",
38+
"description": "* Put any URL to prevent from wasavi activation.\n* One URL per line.\n* A line starting with [c];[/c] or [c]#[/c] is treated as comment.\n* You can use wildcard [c]*[/c] and [c]?[/c] in URL.\n* You can also put blank and CSS selector expression after URL, like [c]http://example.net/ [name=\"foo\"][/c]. The selector will changes a blacklisting target to an element from a page."
3939
},
4040
"option_target_elements_desc": {
4141
"message": "* Format is <(modifier-)* key>,...\nFor example, <c-enter>, <insert> means \"Launch wasavi when ctrl+enter or insert is pressed.\"\n* Following keys can be used:\n[c]backspace[/c] [c]bs[/c] [c]tab[/c] [c]enter[/c] [c]return[/c] [c]ret[/c] [c]pageup[/c] [c]pagedown[/c] [c]end[/c] [c]home[/c] [c]left[/c] [c]up[/c] [c]right[/c] [c]down[/c] [c]insert[/c] [c]ins[/c] [c]delete[/c] [c]del[/c] [c],[/c] [c]comma[/c] [c].[/c] [c]dot[/c] [c]period[/c] [c]/[/c] [c]slash[/c] [c]\\[[/c] [c]\\\\[/c] [c]backslash[/c] [c]\\][/c]\n[c]f1[/c] to [c]f12[/c], [c]a[/c] to [c]z[/c], [c]0[/c] to [c]9[/c]\n* Following keys can be used as modifier: [c]s[/c] (for SHIFT), [c]c[/c] (for CTRL).\n* If text field is left empty default shortcut <c-enter>, <insert> is used.",
@@ -988,4 +988,4 @@
988988
"message": "You are in the restricted region.",
989989
"description": "You are in the restricted region."
990990
}
991-
}
991+
}

src/chrome/_locales/ja/messages.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
"description": "Exceptional URLs"
3535
},
3636
"option_qa_blacklist_tips": {
37-
"message": "* wasavi の起動をさせない URL を記述してください。\n* 1 行につき 1 URL です。\n* [c];[/c] または [c]#[/c] で始まる行はコメントと見なされます。\n* URL の中ではワイルドカード [c]*[/c] および [c]?[/c] を使用できます。",
38-
"description": "* Put any URL to prevent from wasavi activation.\n* One URL per line.\n* A line starting with [c];[/c] or [c]#[/c] is treated as comment.\n* You can use wildcard [c]*[/c] and [c]?[/c] in URL."
37+
"message": "* wasavi の起動をさせない URL を記述してください。\n* 1 行につき 1 URL です。\n* [c];[/c] または [c]#[/c] で始まる行はコメントと見なされます。\n* URL の中ではワイルドカード [c]*[/c] および [c]?[/c] を使用できます。\n* URL に続けて、空白と CSS セレクタを [c]http://example.net/ [name=\"foo\"][/c] のように記述することもできます。このセレクタは、ブロッキングの対象をページから要素に狭めます。",
38+
"description": "* Put any URL to prevent from wasavi activation.\n* One URL per line.\n* A line starting with [c];[/c] or [c]#[/c] is treated as comment.\n* You can use wildcard [c]*[/c] and [c]?[/c] in URL.\n* You can also put blank and CSS selector expression after URL, like [c]http://example.net/ [name=\"foo\"][/c]. The selector will changes a blacklisting target to an element from a page."
3939
},
4040
"option_target_elements_desc": {
4141
"message": "* ショートカットの書式は <(修飾子-)*キー>,... です。\n例えば、<c-enter>,<insert> は「ctrl+enter か insert が押されたら、wasavi を起動する」ことを意味します。\n* 以下のキーをショートカットに使用できます:\n[c]backspace[/c] [c]bs[/c] [c]tab[/c] [c]enter[/c] [c]return[/c] [c]ret[/c] [c]pageup[/c] [c]pagedown[/c] [c]end[/c] [c]home[/c] [c]left[/c] [c]up[/c] [c]right[/c] [c]down[/c] [c]insert[/c] [c]ins[/c] [c]delete[/c] [c]del[/c] [c],[/c] [c]comma[/c] [c].[/c] [c]dot[/c] [c]period[/c] [c]/[/c] [c]slash[/c] [c]\\[[/c] [c]\\\\[/c] [c]backslash[/c] [c]\\][/c]\n[c]f1[/c] から [c]f12[/c], [c]a[/c] から [c]z[/c], [c]0[/c] から [c]9[/c]\n* 以下のキーを修飾子として使用できます: [c]s[/c] (シフトキー)、[c]c[/c] (コントロールキー)。\n* テキストフィールドを空にすると、既定のショートカット <c-enter>, <insert> が使用されます。",
@@ -988,4 +988,4 @@
988988
"message": "保護された領域内です。",
989989
"description": "You are in the restricted region."
990990
}
991-
}
991+
}

src/chrome/frontend/agent.js

+47-18
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ var fontFamily;
6363
var quickActivation;
6464
var devMode;
6565
var logMode;
66-
var canLaunch;
66+
var blacklist;
6767
var statusLineHeight;
6868

6969
var targetElement;
@@ -684,18 +684,49 @@ function toPlainText (input) {
684684
}
685685
}
686686

687-
function isInBlacklist (blacklist) {
688-
var result = (blacklist || '').split('\n').some(function (url) {
689-
url = url.replace(/^\s+|\s+$/g, '');
690-
if (url == '' || /^[#;]/.test(url)) return;
691-
687+
function parseBlacklist (blacklist) {
688+
var result = {
689+
fullBlocked: false,
690+
selectors: [],
691+
includes: function (element) {
692+
return this.fullBlocked || this.selectors.some(function (s) {
693+
try {
694+
return Array.prototype.indexOf.call(
695+
document.querySelectorAll(s), element) >= 0;
696+
}
697+
catch (e) {}
698+
});
699+
}
700+
};
701+
(blacklist || '').split('\n').some(function (line) {
702+
line = line.replace(/^\s+|\s+$/g, '');
703+
if (line == '' || /^[#;]/.test(url)) return;
704+
705+
var delimiter = /\s+/.exec(line);
706+
if (delimiter) {
707+
line = [
708+
line.substring(0, delimiter.index),
709+
line.substring(delimiter.index + delimiter[0].length)
710+
];
711+
}
712+
else {
713+
line = [line, ''];
714+
}
692715
try {
693-
url = new RegExp('^' + url
716+
var url = new RegExp('^' + line[0]
694717
.replace(/[\\^$+.()|{}]/g, function ($0) {return '\\' + $0})
695718
.replace(/\?/g, '.')
696719
.replace(/\*/g, '.+?'), 'i');
697720

698-
if (url.test(window.location.href)) return true;
721+
if (url.test(window.location.href)) {
722+
if (line[1] == '') {
723+
result.fullBlocked = true;
724+
return true;
725+
}
726+
else {
727+
result.selectors.push(line[1]);
728+
}
729+
}
699730
}
700731
catch (e) {}
701732
});
@@ -863,13 +894,13 @@ function createPageAgent (listenKeydown, hookKeyEvents) {
863894
*/
864895

865896
function handleKeydown (e) {
866-
if (!canLaunch || targetElement || !e || !e.target || !allowedElements) return;
897+
if (targetElement || !e || !e.target || !allowedElements) return;
867898
if (e.keyCode == 16 || e.keyCode == 17 || e.keyCode == 18) return;
899+
if (blacklist.includes(e.target)) return;
868900

869901
if (e.target.isContentEditable && allowedElements.enableContentEditable
870902
|| e.target.nodeName == 'BODY' && allowedElements.enablePage
871903
|| /^(?:TEXTAREA|INPUT)$/.test(e.target.nodeName) && isAcceptable(e.target.type)) {
872-
873904
/*
874905
* <textarea>
875906
* <textarea data-texteditor-extension="auto">
@@ -901,12 +932,11 @@ function handleKeydown (e) {
901932
*/
902933

903934
function handleTargetFocus (e) {
904-
if (!canLaunch || !quickActivation || targetElement || !e || !e.target || !allowedElements) return;
905-
935+
if (!quickActivation || targetElement || !e || !e.target || !allowedElements) return;
936+
if (blacklist.includes(e.target)) return;
906937
if (e.target.isContentEditable && allowedElements.enableContentEditable
907938
|| e.target.nodeName == 'BODY' && allowedElements.enablePage
908939
|| /^(?:TEXTAREA|INPUT)$/.test(e.target.nodeName) && isAcceptable(e.target.type)) {
909-
910940
var current = e.target.getAttribute(EXTENSION_CURRENT);
911941
var spec = e.target.getAttribute(EXTENSION_SPECIFIER);
912942
if (current !== null) return;
@@ -965,10 +995,11 @@ function handleAgentInitialized (req) {
965995
*/
966996

967997
function handleRequestLaunch () {
968-
if (!canLaunch || wasaviFrame || targetElement || !allowedElements) return;
998+
if (wasaviFrame || targetElement || !allowedElements) return;
969999
if (typeof document.hasFocus == 'function' && !document.hasFocus()) return;
9701000

9711001
var target = document.activeElement;
1002+
if (blacklist.includes(target)) return;
9721003
if (target.isContentEditable && allowedElements.enableContentEditable
9731004
|| (target.nodeName == 'TEXTAREA' || target.nodeName == 'INPUT')
9741005
&& target.type in ACCEPTABLE_TYPES
@@ -1237,9 +1268,7 @@ function handleBackendMessage (req) {
12371268
break;
12381269
}
12391270
}
1240-
if (qaBlacklist) {
1241-
canLaunch = !isInBlacklist(qaBlacklist);
1242-
}
1271+
blacklist = parseBlacklist(qaBlacklist);
12431272
logbuf.length && log(
12441273
'update-storage: consumed ', logbuf.join(', '));
12451274
break;
@@ -1374,7 +1403,7 @@ function handleConnect (req) {
13741403
quickActivation = req.quickActivation;
13751404
devMode = req.devMode;
13761405
logMode = req.logMode;
1377-
canLaunch = !isInBlacklist(req.qaBlacklist);
1406+
blacklist = parseBlacklist(req.qaBlacklist);
13781407
statusLineHeight = req.statusLineHeight;
13791408

13801409
extension.ensureRun(handleAgentInitialized, req);

src/chrome/scripts/options-core.js

+5
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ function setMarkup (node, text) {
7676
document.createElement(nodeName))
7777
});
7878
}
79+
else {
80+
stack[0].node.appendChild(document.createTextNode(
81+
s.replace(/\\(.)/g, '$1')
82+
));
83+
}
7984
}
8085
}
8186
else {

0 commit comments

Comments
 (0)