File: popup.js

package info (click to toggle)
gnome-shell-extension-gsconnect 54-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 4,132 kB
  • sloc: javascript: 27,572; xml: 332; python: 117; sh: 97; makefile: 16
file content (183 lines) | stat: -rw-r--r-- 4,886 bytes parent folder | download
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
'use strict';


var CONNECTED = false;
var DEVICES = [];
var TARGET_URL = null;


// Suppress errors caused by Mozilla polyfill
// TODO: not sure if these are relevant anymore
const _MUTE = [
    'Could not establish connection. Receiving end does not exist.',
    'The message port closed before a response was received.',
];


// Simple error logging function
// eslint-disable-next-line no-redeclare
function logError(error) {
    if (!_MUTE.includes(error.message))
        console.error(error.message);
}


/**
 * Share a URL, either direct to the browser or by SMS
 *
 * @param {string} device - The deviceId
 * @param {string} action - Currently either 'share' or 'telephony'
 * @param {string} url - The URL to share
 */
async function sendUrl(device, action, url) {
    try {
        window.close();

        await browser.runtime.sendMessage({
            type: 'share',
            data: {
                device: device,
                url: url,
                action: action,
            },
        });
    } catch (e) {
        logError(e);
    }
}


/**
 * Create and return a device element for the popup menu
 *
 * @param {object} device - A JSON object describing a connected device
 * @return {HTMLElement} - A <div> element with icon, name and actions
 */
function getDeviceElement(device) {
    const deviceElement = document.createElement('div');
    deviceElement.className = 'device';

    const deviceIcon = document.createElement('img');
    deviceIcon.className = 'device-icon';
    deviceIcon.src = `images/${device.type}.svg`;
    deviceElement.appendChild(deviceIcon);

    const deviceName = document.createElement('span');
    deviceName.className = 'device-name';
    deviceName.textContent = device.name;
    deviceElement.appendChild(deviceName);

    if (device.share) {
        const shareButton = document.createElement('img');
        shareButton.className = 'plugin-button';
        shareButton.src = 'images/open-in-browser.svg';
        shareButton.title = browser.i18n.getMessage('shareMessage');
        shareButton.addEventListener(
            'click',
            () => sendUrl(device.id, 'share', URL)
        );
        deviceElement.appendChild(shareButton);
    }

    if (device.telephony) {
        const telephonyButton = document.createElement('img');
        telephonyButton.className = 'plugin-button';
        telephonyButton.src = 'images/message.svg';
        telephonyButton.title = browser.i18n.getMessage('smsMessage');
        telephonyButton.addEventListener(
            'click',
            () => sendUrl(device.id, 'telephony', URL)
        );
        deviceElement.appendChild(telephonyButton);
    }

    return deviceElement;
}


/**
 * Populate the browserAction popup
 */
function setPopup() {
    const devNode = document.getElementById('popup');

    while (devNode.hasChildNodes())
        devNode.removeChild(devNode.lastChild);

    if (CONNECTED && DEVICES.length) {
        for (const device of DEVICES) {
            const deviceElement = getDeviceElement(device);
            devNode.appendChild(deviceElement);
        }

        return;
    }

    // Disconnected or no devices
    const message = document.createElement('span');
    message.className = 'popup-menu-message';
    devNode.appendChild(message);

    // The native-messaging-host or service is disconnected
    if (!CONNECTED)
        message.textContent = browser.i18n.getMessage('popupMenuDisconnected');

    // There are no devices
    else
        message.textContent = browser.i18n.getMessage('popupMenuNoDevices');
}


/**
 * Callback for receiving a message forwarded by background.js
 *
 * @param {Object} message - A JSON message object
 * @param {runtime.MessageSender} sender - The sender of the message.
 */
function onPortMessage(message, sender) {
    try {
        // console.log(`WebExtension-popup RECV: ${JSON.stringify(message)}`);

        if (sender.url.includes('/background.html')) {
            if (message.type === 'connected') {
                CONNECTED = message.data;
            } else if (message.type === 'devices') {
                CONNECTED = true;
                DEVICES = message.data;
            }

            setPopup();
        }
    } catch (e) {
        logError(e);
    }
}


/**
 * Set the current URL and repopulate the popup, on-demand
 */
async function onPopup() {
    try {
        const tabs = await browser.tabs.query({
            active: true,
            currentWindow: true,
        });

        if (tabs.length)
            TARGET_URL = tabs[0].url;

        setPopup();
        await browser.runtime.sendMessage({type: 'devices'});
    } catch (e) {
        logError(e);
    }
}


/**
 * Startup: listen for forwarded messages and populate the popup on-demand
 */
browser.runtime.onMessage.addListener(onPortMessage);
document.addEventListener('DOMContentLoaded', onPopup);