Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect MATLAB Help Browser

Tags:

matlab

I'd like to create some HTML documentation for a large MATLAB application, to be viewed mainly within the MATLAB Help Browser, in any version of MATLAB from 11b on. This would have some custom CSS (but nothing very complicated).

However, I'd also like the same documentation to be viewable within other browsers. I care about Chrome and IE9 and 10, although support for other browsers would be welcome. I'd like the documentation to

  1. Look essentially the same across all the browsers including the MATLAB Help Browser, but
  2. In a few ways look different when presented in the MATLAB Help Browser - in particular, I want matlab: links to be displayed and behave differently, as they are not supported outside the MATLAB Help Browser.

Question 1: I'm running into difficulty even with 1, as despite the simplicity of my CSS the MATLAB Help Browser seems unpredictable in what parts of CSS it supports, and it seems to vary a great deal across MATLAB versions. I'm guessing that it's not documented at all. Does anyone have any experience/tips about using CSS in the MATLAB Help Browser?

Question 2: I was hoping to be able to auto-detect somehow whether the matlab: protocol was supported in the viewing browser, but it seems from browsing other StackOverflow answers that this is not possible. Given that I need to support Chrome, it's not even possible to try following a matlab: link and detect if it fails, as apparently Chrome fails silently. Boo. So my next thought is to use JavaScript to detect the UserAgent of the browser. MATLAB 11b reports itself as Mozilla4.0 with ICEbrowser, whereas 13a gives Mozilla4.0 with Trident6. I'm not entirely sure what these are - I'm guessing they're third-party Java-based browser components that behave like Mozilla, and MathWorks have changed the component between 11b and 13a.

If I detect the UserAgent and assume that anything containing the strings ICEBrowser or Trident, is that likely to be sufficient to distinguish the MATLAB Help Browser for most purposes? Obviously I'll have to keep up to date with UserAgent strings for post-13a releases.

Thanks for your suggestions!

like image 859
Sam Roberts Avatar asked May 04 '13 00:05

Sam Roberts


3 Answers

The capabilities of the MATLAB help browser differ greatly, not only between different MATLAB versions, but also between different operating systems and even between different architectures (the recent 64bit versions often have a particularly bad browser).

We're using the user agent to deliver MATLAB-specific web pages. Here's a list of some of them:

R2010b/R2011b, 32bit, WinXP:
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8) Gecko/20060705

R2010b/R2011b, 32bit, Win7:
Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.8) Gecko/20060705

R2010b, 64bit, Win7:
Mozilla/5.0 (Java 1.6.0_17; Windows 7 6.1 amd64; de_DE) ICEbrowser/v6_0_2

R2011b, 64bit, Win7:
Mozilla/5.0 (Java 1.6.0_17; Windows 7 6.1 amd64; de_DE) ICEbrowser/v6_0_2

R2012a, 32bit, Win7:
Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko

R2012a, 32bit, WinXP:
Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2) Gecko

R2012a, 64bit, Win7:
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2;     .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Tablet PC 2.0)

R2012b, 32bit, Win7:
Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2) Gecko

R2012b, 64bit, Win7:
Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Win64; x64; Trident/4.0; .NET CLR 2.0.50727; SLCC2;     .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Tablet PC 2.0)

R2010b, 32bit, Linux:
Mozilla/5.0 (Java 1.6.0_17; Linux 2.6.32-33-generic i386; en_US) ICEbrowser/v6_0_2

Note that you cannot simply do a string comparison against these, since some parts of the strings change depending on the client machine (e.g. the language code).

Also note that other capabilities besides rendering HTML and CSS differ greatly. In particular, support for JavaScript and HTTP authentication.

like image 154
Florian Brucker Avatar answered Oct 17 '22 13:10

Florian Brucker


icebrowser is a java html renderer that supported a random subset of html/javascript. From 2012b on for the main help and from 2013a on for the "supplemental software" help they have switched to a client which is a thin jni wrapper around a native browser so you get trident (=internet explorer) on windows, webkit (=safari) on mac and gecko (=firefox) on linux. Unfortunately they haven't changed the user agent to have anything obvious like matlab. This means that from 2013a onwards we can use css/javascript rendering with a real browser capability but get all the cross-browser issues that you have on the open web. Wheras pre 2013a the browser capabilities were far more limited but you only had to worry about one browser.

I was looking myself for a reliable way to detect if the matlab: protocol is supported but all I found was your question:-)

like image 24
David Carlisle Avatar answered Oct 17 '22 13:10

David Carlisle


I'm aware of the age of the question, but we had a similar desire to detect the MATLAB internal web browser. In our case we wanted to be able to show matlab: protocol edit/opentoline links only when using the MATLAB internal browser (as they won't work in web browsers).

As we have seen the user agen string is completely useless - in the older versions MATLAB used their own home-brew browser with a useless useragent, and then in later versions switched to a Chromium based browser (jxbrowser).

In hunting for a way to identify it, I accidently stubled across the browsers failing to handle the URLSearchParams java call - much like IE did, it triggers an exception. Looking here it seems that it is supported by the vast majority of web browsers, with notable exceptions being Opera Mini and IE (though we can identify it's not those in other ways). This is of course not foolproof. It may give false positives on obscure browsers (possible), or may stop working if MATLAB implement support for it (unlikely). However thus far it seems to work in MATLAB versions R2013a to R2019a (don't have newer one installed to test).

Update: Somewhere between R2019a and R2022a, they changed from jxbrowser to JCEF. I've modified the detection code to include a check for JCEF that if present assumes it is in MATLAB. This makes it work in R2022a (not sure about the versions in between). Again this opens up the possibility of false positives but still better than nothing.

function isMatlabBrowser() {
    try {
        if(window.operamini) {
            return false;
        }
    } catch(err) {}
    try {
        if (window.document.documentMode) {
            return false;
        }
    } catch(err) {}
    try {
        if(typeof window.cefQuery === 'function') {
            return true;
        }
    } catch (err) {}
    try {
        var hasSearchParamsFcn = new URLSearchParams(window.location.search);
    }
    catch(err) {
        return true;
    }
    return false;
}

In order to get our matlab links to show only in the internal browser, I premake documentation pages with the links (e.g. <a href="matlab: edit('myfile.m')">open</a>) present in the HTML code. Each link then has class="matlaburi" with everything tagged with that class being hidden by default using CSS.

We then have the following additional javascript, which checks if in the MATLAB browser, and if so makes all the links visibble by changing the display style of anything that is tagged with the class:

function showMatlabOpenLinks() {
    if (isMatlabBrowser()) {
        var cols = document.getElementsByClassName('matlaburi');
        for(i = 0; i < cols.length; i++) {
            cols[i].style.display = "inline";
        }
    }
}

var onLoadFcn = function() {
    showMatlabOpenLinks();
};

if(window.attachEvent) {
    window.attachEvent('onload', onLoadFcn);
} else {
    window.onload = onLoadFcn;
}

like image 45
Tom Carpenter Avatar answered Oct 17 '22 14:10

Tom Carpenter