This repository was archived by the owner on Oct 24, 2019. It is now read-only.
forked from adblockplus/adblockplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomposer.js
More file actions
122 lines (109 loc) · 3.41 KB
/
composer.js
File metadata and controls
122 lines (109 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* This file is part of Adblock Plus <https://adblockplus.org/>,
* Copyright (C) 2006-present eyeo GmbH
*
* Adblock Plus is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* Adblock Plus is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
/* globals getErrorMessage */
"use strict";
let targetPageId = null;
const {stripTagsUnsafe} = ext.i18n;
function onKeyDown(event)
{
if (event.keyCode == 27)
{
event.preventDefault();
closeDialog();
}
else if (event.keyCode == 13 && !event.shiftKey && !event.ctrlKey)
{
event.preventDefault();
addFilters();
}
}
function addFilters()
{
browser.runtime.sendMessage({
type: "filters.importRaw",
text: document.getElementById("filters").value
}).then((errors) =>
{
if (errors.length > 0)
{
errors = errors.map(getErrorMessage);
alert(stripTagsUnsafe(errors.join("\n")));
}
else
closeDialog(true);
});
}
// We'd rather just call window.close, but that isn't working consistently with
// Firefox 57, even when allowScriptsToClose is passed to browser.windows.create
// See https://bugzilla.mozilla.org/show_bug.cgi?id=1418394
function closeMe()
{
browser.runtime.sendMessage({
type: "app.get",
what: "senderId"
}).then(tabId => browser.tabs.remove(tabId));
}
function closeDialog(success)
{
browser.runtime.sendMessage({
type: "composer.forward",
targetPageId,
payload:
{
type: "composer.content.finished",
popupAlreadyClosed: true,
remove: (typeof success == "boolean" ? success : false)
}
});
closeMe();
}
function init()
{
// Attach event listeners
window.addEventListener("keydown", onKeyDown, false);
document.getElementById("addButton").addEventListener("click", addFilters);
document.getElementById("cancelButton").addEventListener(
"click", closeDialog.bind(null, false)
);
document.getElementById("filters").focus();
ext.onMessage.addListener((msg, sender, sendResponse) =>
{
switch (msg.type)
{
case "composer.dialog.init":
targetPageId = msg.sender;
const filtersTextArea = document.getElementById("filters");
filtersTextArea.value = msg.filters.join("\n");
filtersTextArea.disabled = false;
document.getElementById("addButton").disabled = false;
// Firefox sometimes tells us this window had loaded before it has[1],
// to work around that we send the "composer.dialog.init" message again
// when sending failed. Unfortunately sometimes sending is reported as
// successful when it's not, but with the response of `undefined`. We
// therefore send a response here, and check for it to see if the
// message really was sent successfully.
// [1] - https://bugzilla.mozilla.org/show_bug.cgi?id=1418655
sendResponse(true);
break;
case "composer.dialog.close":
closeMe();
break;
}
});
window.removeEventListener("load", init);
}
window.addEventListener("load", init, false);