Project

General

Profile

Download (3.93 KB) Statistics
| Branch: | Tag: | Revision:

haketilo / content / activity_info_server.js @ 263d03d5

1
/**
2
 * This file is part of Haketilo.
3
 *
4
 * Function: Informing the popup about what happens in the content script
5
 *     (script injection, script blocking, etc.).
6
 *
7
 * Copyright (C) 2021 Wojtek Kosior
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation, either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * As additional permission under GNU GPL version 3 section 7, you
20
 * may distribute forms of that code without the copy of the GNU
21
 * GPL normally required by section 4, provided you include this
22
 * license notice and, in case of non-source distribution, a URL
23
 * through which recipients can access the Corresponding Source.
24
 * If you modify file(s) with this exception, you may extend this
25
 * exception to your version of the file(s), but you are not
26
 * obligated to do so. If you do not wish to do so, delete this
27
 * exception statement from your version.
28
 *
29
 * As a special exception to the GPL, any HTML file which merely
30
 * makes function calls to this code, and for that purpose
31
 * includes it by reference shall be deemed a separate work for
32
 * copyright law purposes. If you modify this code, you may extend
33
 * this exception to your version of the code, but you are not
34
 * obligated to do so. If you do not wish to do so, delete this
35
 * exception statement from your version.
36
 *
37
 * You should have received a copy of the GNU General Public License
38
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
39
 *
40
 * I, Wojtek Kosior, thereby promise not to sue for violation of this file's
41
 * license. Although I request that you do not make use this code in a
42
 * proprietary program, I am not going to enforce this in court.
43
 */
44

    
45
/*
46
 * IMPORTS_START
47
 * IMPORT listen_for_connection
48
 * IMPORT CONNECTION_TYPE
49
 * IMPORT repo_query
50
 * IMPORT subscribe_repo_query_results
51
 * IMPORT unsubscribe_repo_query_results
52
 * IMPORTS_END
53
 */
54

    
55
var activities = [];
56
var ports = new Set();
57

    
58
function report_activity_oneshot(name, data, port)
59
{
60
    port.postMessage([name, data]);
61
}
62

    
63
function report_activity(name, data)
64
{
65
    const activity = [name, data];
66
    activities.push(activity);
67

    
68
    for (const port of ports)
69
	port.postMessage(activity);
70
}
71

    
72
function report_script(script_data)
73
{
74
    report_activity("script", script_data);
75
}
76

    
77
function report_settings(settings)
78
{
79
    report_activity("settings", settings);
80
}
81

    
82
function report_document_type(is_html)
83
{
84
    report_activity("is_html", is_html);
85
}
86

    
87
function report_repo_query_action(update, port)
88
{
89
    report_activity_oneshot("repo_query_action", update, port);
90
}
91

    
92
function trigger_repo_query(query_specifier)
93
{
94
    repo_query(...query_specifier);
95
}
96

    
97
function handle_disconnect(port, report_action)
98
{
99
    ports.delete(port)
100
    unsubscribe_repo_query_results(report_action);
101
}
102

    
103
function new_connection(port)
104
{
105
    console.log("new activity info connection!");
106

    
107
    ports.add(port);
108

    
109
    for (const activity of activities)
110
	port.postMessage(activity);
111

    
112
    const report_action = u => report_repo_query_action(u, port);
113
    subscribe_repo_query_results(report_action);
114

    
115
    /*
116
     * So far the only thing we expect to receive is repo query order. Once more
117
     * possibilities arrive, we will need to complicate this listener.
118
     */
119
    port.onMessage.addListener(trigger_repo_query);
120

    
121
    port.onDisconnect.addListener(() => handle_disconnect(port, report_action));
122
}
123

    
124
function start_activity_info_server()
125
{
126
    listen_for_connection(CONNECTION_TYPE.ACTIVITY_INFO, new_connection);
127
}
128

    
129
/*
130
 * EXPORTS_START
131
 * EXPORT start_activity_info_server
132
 * EXPORT report_script
133
 * EXPORT report_settings
134
 * EXPORT report_document_type
135
 * EXPORTS_END
136
 */
(1-1/4)