forked from adblockplus/adblockpluschrome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomposer.js
98 lines (86 loc) · 2.28 KB
/
composer.js
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
/*
* 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/>.
*/
/* eslint-env jquery */
"use strict";
let targetPageId = null;
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
},
errors =>
{
if (errors.length > 0)
alert(errors.join("\n"));
else
closeDialog(true);
});
}
function closeDialog(success)
{
browser.runtime.sendMessage({
type: "forward",
targetPageId,
payload:
{
type: "composer.content.finished",
remove: (typeof success == "boolean" ? success : false)
}
});
window.close();
}
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)
);
// Apply jQuery UI styles
$("button").button();
document.getElementById("filters").focus();
ext.onMessage.addListener((msg, sender, sendResponse) =>
{
switch (msg.type)
{
case "composer.dialog.init":
targetPageId = msg.sender;
document.getElementById("filters").value = msg.filters.join("\n");
break;
case "composer.dialog.close":
window.close();
break;
}
});
window.removeEventListener("load", init);
}
window.addEventListener("load", init, false);