Project

General

Profile

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

haketilo / content / activity_info_server.js @ c12b9ee3

1
/**
2
 * part of Hachette
3
 * Informing about activities performed by content script (script injection,
4
 * script blocking).
5
 *
6
 * Copyright (C) 2021 Wojtek Kosior
7
 * Redistribution terms are gathered in the `copyright' file.
8
 */
9

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

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

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

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

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

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

    
42
function report_settings(settings)
43
{
44
    report_activity("settings", settings);
45
}
46

    
47
function report_content_type(content_type)
48
{
49
    report_activity("content_type", content_type);
50
}
51

    
52
function report_repo_query_action(update, port)
53
{
54
    report_activity_oneshot("repo_query_action", update, port);
55
}
56

    
57
function trigger_repo_query(query_specifier)
58
{
59
    repo_query(...query_specifier);
60
}
61

    
62
function handle_disconnect(port, report_action)
63
{
64
    ports.delete(port)
65
    unsubscribe_repo_query_results(report_action);
66
}
67

    
68
function new_connection(port)
69
{
70
    console.log("new activity info connection!");
71

    
72
    ports.add(port);
73

    
74
    for (const activity of activities)
75
	port.postMessage(activity);
76

    
77
    const report_action = u => report_repo_query_action(u, port);
78
    subscribe_repo_query_results(report_action);
79

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

    
86
    port.onDisconnect.addListener(() => handle_disconnect(port, report_action));
87
}
88

    
89
function start_activity_info_server()
90
{
91
    listen_for_connection(CONNECTION_TYPE.ACTIVITY_INFO, new_connection);
92
}
93

    
94
/*
95
 * EXPORTS_START
96
 * EXPORT start_activity_info_server
97
 * EXPORT report_script
98
 * EXPORT report_settings
99
 * EXPORT report_content_type
100
 * EXPORTS_END
101
 */
(1-1/6)