home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   alt.os.linux      Getting to be as bloated as Windows!      107,822 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 107,818 of 107,822   
   Maria Sophia to All   
   =?utf-8?Q?Tutorial=3A_Firefox_right=E2=8   
   23 Feb 26 21:48:15   
   
   XPost: alt.comp.software.firefox, alt.comp.os.windows-10   
   From: mariasophia@comprehension.com   
      
   Tutorial:   
   Firefox right-click extension + native host on Windows (GUI launch mystery)   
      
   Today I invested a couple of hours learning how to add a right-click   
   context menu to Firefox to call a Windows $EDITOR to edit source HTML.   
      
   I didn't *need* to write the extension, but since it was my very first   
   Firefox extension, I learned a ton I'd like to discuss with those who are   
   interested in writing their own Firefox extensions on their given platform.   
      
   This tutorial can be rightly titled something like:   
    *Adding a right-click Firefox menu item that calls a native program*   
      
   This tutorial shows how to write a simple Firefox extension on Windows that   
   adds a right-click context menu item, extracts the HTML of the current page   
   & sends it to a native Windows program. The native program receives the   
      
   HTML, writes it to a temporary file, and attempts to launch an editor such   
   as Notepad++ or gVim.   
      
   Everything in this tutorial worked end-to-end on my Windows 10 box,   
   except for one final detail, where Windows does not (yet) allow GUI   
   applications to appear on the user's desktop when launched from a Firefox   
   native messaging host. The editor launch command executes successfully, the   
   temp file is created, everything important is logged in great detail, and   
   the native host returns success, but the editor window never appears.   
      
   This might be a Windows session isolation behavior.   
   Linux users may not encounter this limitation.   
      
   We know Firefox, even on Windows, *can* launch any desired $EDITOR.   
    about:config > view_source.editor   
    view_source.editor.external = true   
    view_source.editor.path = C:\app\editor\txt\vi\gvim.exe   
      
   So why can't we get an extension to do the same thing native in Firefox?   
      
   Dunno yet. So this tutorial is only 99% complete but it's still useful   
   because it demonstrates the entire Firefox-extension pipeline, the native   
   messaging host mechanism, and the Windows registry integration.   
      
   Only the final GUI launch behavior remains an open question.   
   Note that all paths are mine, as they're copied from my working setup.   
   The paths don't matter as long as they fit what your system filespecs are.   
      
   1. Overview   
      
     A. What the extension does   
        a. Adds a right-click context menu item to Firefox.   
        b. Extracts the HTML of the current page.   
        c. Sends the HTML to a native Windows program.   
        d. The native program writes the HTML to a temp file.   
        e. The native program attempts to launch an editor.   
      
     B. What works   
        a. The extension loads.   
        b. The context menu appears.   
        c. The content script extracts HTML.   
        d. The background script sends the HTML to the native host.   
        e. The native host receives JSON correctly.   
        f. The Python script writes the temp file.   
        g. The Python script executes the editor launch command.   
        h. The native host returns success.   
        i. Every important step is logged to debug files.   
      
     C. What remains open   
        a. On Windows, GUI editors launched from a native host do not appear.   
        b. No process appears in tasklist.   
        c. No error is thrown.   
        d. Yet, the same editor launches normally when invoked by   
           Firefox's built-in view_source.editor.external mechanism.   
        e. Linux users may not have this limitation. Dunno.   
      
   2. Directory Setup (my filespecs are used here, change as desired)   
      
     A. Create the directory:   
        C:\app\browser\firefox\openwithgvim   
      
     B. Place the following files inside it:   
        a. manifest.json   
        b. background.js   
        c. content.js   
        d. gvim_host.json   
        e. gvim_host.bat   
        f. gvim_host.py   
        g. install_host.reg   
      
   3. Installing the Native Host   
      
     A. Double-click install_host.reg to register the native host.   
     B. Ensure Python is on your PATH.   
     C. Restart Firefox after making changes.   
      
   4. Testing the Extension   
      
     A. Load the extension as a temporary add-on in Firefox.   
     B. This adds a new Firefox context menu "Open page source in gVim".   
     B. Right-click any page & choose "Open page source in gVim".   
     C. Observe the logs in the Browser Console.   
     D. Observe the debug logs in host_debug.log & host_stderr.log.   
     E. Confirm that:   
        a. The temp file is created.   
        b. The native host returns success.   
        c. The editor launch command executes.   
      
     F. On Windows, the editor window did not appear but there   
        were no errors in the debug logs. This is the open question.   
      
   5. The Open Question for Windows and Linux Users   
      
     A. Why do GUI applications launched from a Firefox native messaging   
        host not appear on the user's desktop in Windows?   
      
        Do they appear on other operating systems?   
      
     B. Observations:   
        a. The editor launch command executes successfully.   
        b. No error is thrown.   
        c. The temp file is created.   
        d. The native host returns success.   
        e. No editor process appears in tasklist.   
        f. The same editor launches normally when invoked by   
           Firefox's built-in external editor mechanism.   
      
     C. Hypothesis:   
        a. Firefox native hosts run in a restricted background session.   
        b. Windows prevents GUI applications from appearing in the interactive   
           desktop session when launched from that context.   
      
     D. Invitation:   
        a. Linux users may be able to run the same code successfully.   
        b. Windows experts may know a workaround that does not require   
           writing a helper executable.   
      
   6. Files (each indented one space for Usenet safety)   
      
    manifest.json   
     {   
       "manifest_version": 2,   
       "name": "Open Page Source in gVim",   
       "version": "1.0",   
       "description": "Right-click a page to open its HTML source in gVim.",   
       "permissions": [   
         "contextMenus",   
         "tabs",   
         "nativeMessaging",   
         "activeTab",   
         ""   
       ],   
       "background": {   
         "scripts": ["background.js"],   
         "persistent": true   
       },   
       "content_scripts": [   
         {   
           "matches": [""],   
           "js": ["content.js"],   
           "run_at": "document_end"   
         }   
       ],   
       "browser_specific_settings": {   
         "gecko": {   
           "id": "gvim-opener@example"   
         }   
       }   
     }   
      
    background.js   
     console.log("BACKGROUND SCRIPT LOADED");   
      
     browser.runtime.onInstalled.addListener(() => {   
       console.log("onInstalled fired: creating context menu");   
       browser.contextMenus.create({   
         id: "open-page-source-gvim",   
         title: "Open page source in gVim",   
         contexts: ["page"]   
       });   
     });   
      
      
   [continued in next message]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca