Pass info,tab through Shortkey/command listener like with a context menu listener

Sometimes the error reporting in Firefox really sucks. Try it with a try ... catch:

code with logging and catch
browser.commands.onCommand.addListener(async command => { try { switch (command) {
    case '1 Host Connect': {
        const { id: windowId, } = (await browser.windows.getLastFocused());
        console.log(`windowId`, windowId);
        const { id: tabId, } = (await (browser.tabs.query({ active: true, windowId, })))[0];
        console.log(`tabId`, tabId);
        const pageSelection = (await Promise.all(
            (await browser.webNavigation.getAllFrames({ tabId, }))
            .map(async ({ frameId, }) => { try {
                console.log(`Checking frame ${frameId} in tab ${tabId} ...`);
                const selected = (await browser.tabs.executeScript(tabId, { frameId, code: `window.getSelection().toString()`, }));
                console.log(`Selection in frame ${frameId} in tab ${tabId}:`, selection);
                return selection;
            } catch (_) { console.error(_); return ''; } }) // in production, avoid weird errors about uninitialized frames and stuff like that, but when debugging empty selection texts again, comment this line
        )).reduce((a, b) => a || b, '') || '';
        console.info(`Selection in some frame in the currently active tab ${tabId}`: pageSelection);
        // use `pageSelection`
    } break;
    // other commands
} catch (error) { console.error(`onCommand error`, error); } });
1 Like

Well… that’s frustrating - for you probably more than for me (thx again btw)… I’ll get the following:

windowId 5 icinga_addon.js:699:9
tabId 15 icinga_addon.js:701:9
onCommand error <unavailable> icinga_addon.js:715:20 

If you aren’t using the extension debugger already (about:debugging), see if that displays more than just the <unavailable>.
If it doesn’t, set a breakpoint in the line with the console.error (you may have to put it on its own line for that to work in Firefox).

1 Like

Thx for the hint… had no idea that I need to turn on debugging specifically. Thought that the console would give me that output already -.-

I made progress however…
My manifest.json missed two permission, webNavigation and Host. Altered selection variable to selected. This is what the code looks like now:

browser.commands.onCommand.addListener(async command => { try { switch (command) {
    case '1 Host Connect': {
        const { id: windowId, } = (await browser.windows.getLastFocused());
        console.log(`windowId`, windowId);
        const { id: tabId, } = (await (browser.tabs.query({ active: true, windowId, })))[0];
        console.log(`tabId`, tabId);
        const pageSelection = (await Promise.all(
            (await browser.webNavigation.getAllFrames({ tabId, }))
            .map(async ({ frameId, }) => { try {
                console.log(`Checking frame ${frameId} in tab ${tabId} ...`);
                const selected = (await browser.tabs.executeScript(tabId, { frameId, code: `window.getSelection().toString()`, }));
                console.log(`Selection in frame ${frameId} in tab ${tabId}:`, selected);
                return selected;
            } catch (_) { console.error(_); return ''; } }) // in production, avoid weird errors about uninitialized frames and stuff like that, but when debugging empty selection texts again, comment this line
        )).reduce((a, b) => a || b, '') || '';
        console.info(`Selection in some frame in the currently active tab ${tabId}:`, pageSelection);
        // use `pageSelection`
    } break;
    // other commands
}} catch (error) { console.error(`onCommand error`, error); }
	});

This gives me the following output:

windowId 5
Webconsole context has changed
tabId 1 
Checking frame 0 in tab 1 ... 
Checking frame 8589934596 in tab 1 ... 
Checking frame 8589934597 in tab 1 ... 
Checking frame 8589934598 in tab 1 ... 
Selection in frame 0 in tab 1: Array [ "" ]
Selection in frame 8589934596 in tab 1:Array [ "" ]
Selection in frame 8589934597 in tab 1: Array [ "" ]
Selection in frame 8589934598 in tab 1: Array [ "\r\n\t\r\n\t\r\nSONE_SONEPINT" ]
Selection in some frame in the currently active tab 1: Array [ "" ]

So it seems to work now… I’m just not getting the selection of the frame I want to as it seems…

If I select a string on a regular html page, not the icinga page, it works though, since it only has 1 frame:

tabId 3 
Checking frame 0 in tab 3 ... 
Selection in frame 0 in tab 3: Array [ "wikipedia " ]
Selection in some frame in the currently active tab 3: Array [ "wikipedia " ]

:frowning:

Ok, so I forgot that tabs.executeScript() actually returns arrays with the results, because there is an allFrames option which leads to multiple results at once. Using that should also simplify the code quite a bit (but this simpler version doesn’t allow the catch per frame, which may be a problem). You wouldn’t even really need the tabId anymore:

browser.commands.onCommand.addListener(async command => { try { switch (command) {
    case '1 Host Connect': {
        const { id: windowId, } = (await browser.windows.getLastFocused());
        console.log(`windowId`, windowId);
        const { id: tabId, } = (await (browser.tabs.query({ active: true, windowId, })))[0];
        console.log(`tabId`, tabId);
        const pageSelection = (await browser.tabs.executeScript(tabId, {
            allFrames: true, code: `window.getSelection().toString()`,
        })).reduce((a, b) => a || b, '') || '';
        console.info(`Selection in some frame in the currently active tab ${tabId}:`, pageSelection);
        // use `pageSelection`
    } break;
    // other commands
}} catch (error) {
    console.error(`onCommand error`, error); }
});
1 Like

You my friend… are awesome!!!
Works perfectly now!
I’ll implement this now in the rest of my code, but that should do the trick…

Thanks a lot again!!

1 Like