Project

General

Profile

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

haketilo / content / activity_info_server.js @ 96068ada

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
 * Redistribution terms are gathered in the `copyright' file.
9
 */
10

    
11
/*
12
 * IMPORTS_START
13
 * IMPORT listen_for_connection
14
 * IMPORT CONNECTION_TYPE
15
 * IMPORT repo_query
16
 * IMPORT subscribe_repo_query_results
17
 * IMPORT unsubscribe_repo_query_results
18
 * IMPORTS_END
19
 */
20

    
21
var activities = [];
22
var ports = new Set();
23

    
24
function report_activity_oneshot(name, data, port)
25
{
26
    port.postMessage([name, data]);
27
}
28

    
29
function report_activity(name, data)
30
{
31
    const activity = [name, data];
32
    activities.push(activity);
33

    
34
    for (const port of ports)
35
	port.postMessage(activity);
36
}
37

    
38
function report_script(script_data)
39
{
40
    report_activity("script", script_data);
41
}
42

    
43
function report_settings(settings)
44
{
45
    const settings_clone = {};
46
    Object.assign(settings_clone, settings)
47
    report_activity("settings", settings_clone);
48
}
49

    
50
function report_document_type(is_html)
51
{
52
    report_activity("is_html", is_html);
53
}
54

    
55
function report_repo_query_action(update, port)
56
{
57
    report_activity_oneshot("repo_query_action", update, port);
58
}
59

    
60
function trigger_repo_query(query_specifier)
61
{
62
    repo_query(...query_specifier);
63
}
64

    
65
function handle_disconnect(port, report_action)
66
{
67
    ports.delete(port)
68
    unsubscribe_repo_query_results(report_action);
69
}
70

    
71
function new_connection(port)
72
{
73
    console.log("new activity info connection!");
74

    
75
    ports.add(port);
76

    
77
    for (const activity of activities)
78
	port.postMessage(activity);
79

    
80
    const report_action = u => report_repo_query_action(u, port);
81
    subscribe_repo_query_results(report_action);
82

    
83
    /*
84
     * So far the only thing we expect to receive is repo query order. Once more
85
     * possibilities arrive, we will need to complicate this listener.
86
     */
87
    port.onMessage.addListener(trigger_repo_query);
88

    
89
    port.onDisconnect.addListener(() => handle_disconnect(port, report_action));
90
}
91

    
92
function start_activity_info_server()
93
{
94
    listen_for_connection(CONNECTION_TYPE.ACTIVITY_INFO, new_connection);
95
}
96

    
97
/*
98
 * EXPORTS_START
99
 * EXPORT start_activity_info_server
100
 * EXPORT report_script
101
 * EXPORT report_settings
102
 * EXPORT report_document_type
103
 * EXPORTS_END
104
 */
(1-1/4)