Home > Mobile >  Open ALL External links in Browser (NWjs)
Open ALL External links in Browser (NWjs)

Time:02-14

I was trying to make my own standalone copy of Google Chat using NWjs, but I ran into some problems.

So, when you open a link in the NWjs window, it opens in another NWjs window. However, I would like to open it in the default system browser.

I tried using Tampermonkey, which didn't work, since I figured out how to add extensions to NWjs, and making my own extension didn't work either, so I found out about NWjs's inject_js_end and tried to use that.

But, I keep getting Uncaught TypeError: Failed to set the 'innerHTML' property on 'Element': This document requires 'TrustedHTML' assignment. error.

I assumed this was because CSP was blocking my jQuery code, so I modded CSP-Blocker for chrome but it didn't work.

package.json:

{
    "name": "Chat",
    "main": "index.html",
    "chromium-args": "--enable-logging=stderr --load-extension='./AntiCSP'",
    "inject_js_end": "script.js",
    "window": {
        "title": "Chat",
        "icon": "ico.png",
        "width": 1280,
        "height": 720,
        "resizable": true
    }
}

index.html:

<!DOCTYPE html>
<html>
    <head>
        <title>Loading...</title>
    </head>
    
    <body>
        <script>
window.location.replace("https://chat.google.com")
        </script>
    </body>
</html>

script.js

//jQuery here
console.log("[DEBUG]: Replace links to browser...")

$('a').click(function() {
    nw.Shell.openExternal($(this).prop("href"));
})

How can I force all links in the webpage to open in system browser? I am mostly a Python dev and I haven't found any working solutions yet.

EDIT: As Jaredcheeda pointed out, inject_js_end does only run once. Does anyone have other possible solutions I could use? Maybe Tampermonkey...?

EDIT2: Anyone have any ideas? I still don't have any ideas...

CodePudding user response:

While I agree with Jared that you should figure out how to use the official API, I disagree that you would need to re-add click handlers whenever a new IM came in. You can add a click handler to the document, and check the element type that was clicked on for interception.

This guide's click handler should have most of what you're looking for: https://www.thepolyglotdeveloper.com/2018/08/using-nwjs-convert-website-into-desktop-application/

CodePudding user response:

Correct answer: Use the official API

Anything other than that will just be very hacky. The inject_js_end only runs once. What happens when two people are IMing and a new message is received containing a link, your function to hijack the click event and open in a default browser would need to be re-ran after every IM. You could try listening to navigation events and preventing them, but anything you do is just gonna be hacky at the end.

  • Related