How does this JavaScript open Windows Settings in Firefox?

寵の児 提交于 2019-12-10 18:23:22

问题


After a new install of Firefox 45 Developer Edition, I saw this page. It has a button ("Let's do it") that when clicked, somehow opens up the Choose default apps settings page in Windows 10.

https://www.mozilla.org/en-US/firefox/windows-10/welcome/?utm_source=firefox-browser&utm_medium=firefox-browser

How is this done? I couldn't find anything through the Developer Console in the labyrinthine code on that page. Besides, I would have thought browsers don't allow JavaScript to open something as sensitive as the Settings app.


回答1:


The page fires a custom event of type mozUITour on the document. This event is handled in the browser by content-UITour.js, which shovels out most of the actual processing to UITour.jsm. The unobfuscated client-side code can be viewed in UITour-lib.js.

Cutting through all the client-side abstraction, this is what’s happening:

document.dispatchEvent(new CustomEvent('mozUITour', {
    bubbles: true,
    detail: {
        action: 'setConfiguration',
        data: {
            configuration: 'defaultBrowser'
        }
    }
}));

Then in the browser, it handles the event, dispatches the event in another internal event queue, where it will be processed by calling into nsIShellService::setDefaultBrowser, implemented by nsWindowsShellService.cpp. On what’s currently line 943, we have:

if (IsWin10OrLater()) {
  rv = LaunchModernSettingsDialogDefaultApps();
} else {
  rv = LaunchControlPanelDefaultsSelectionUI();
}

And LaunchModernSettingsDialogDefaultApps, I think, is a pretty descriptive function name.

Now, from your comment, “in a way that one could use it on their own page, for example”? Not so likely. content-UITour.js checks that the page has the uitour permission. From browser/app/permissions, we have:

# UITour
origin  uitour  1   https://www.mozilla.org
origin  uitour  1   https://self-repair.mozilla.org
origin  uitour  1   https://support.mozilla.org
origin  uitour  1   about:home

So unless you’re www.mozilla.org, self-repair.mozilla.org, support.mozilla.org, or about:home, you can’t do it, at least not by default. Before Firefox 15 (17 with a manual settings change, see this bug for more information), you might be able to use netscape.security.PrivilegeManager.enablePrivilege to request extra permissions from the browser, but that’s not around any more, and I’m not sure that even touches the same permission mechanism.



来源:https://stackoverflow.com/questions/34539718/how-does-this-javascript-open-windows-settings-in-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!