Project

General

Profile

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

haketilo / content / activity_info_server.js @ 2bd35bc4

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
    report_activity("settings", settings);
46
}
47

    
48
function report_document_type(is_html)
49
{
50
    report_activity("is_html", is_html);
51
}
52

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

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

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

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

    
73
    ports.add(port);
74

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

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

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

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

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

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