I need to know if the browser running my page is Firefox. I came across the code below:
var isGecko = (navigator.product == 'Gecko');
but this is true for Firefox and Safari.
Only Firefox has the string "Firefox" in the user agent, so it is as easy as
var isFirefox = (navigator.userAgent.indexOf('Firefox') !== -1);
Edit: yes, Mozilla discourages it
Related
I've been searching for a while about the recommended and current way of creating Watir::Browser.new for :chrome and set it to accept a certain language. However, this seems to be a topic with little interest, seems to be ever changing, etc.
So, please, can someone explain what the current and recommended way of setting Chrome's language is?
I'm not talking about Chrome's user interface but the Accept-Language HTTP header that later gets sent with the HTTP request so that an application can render it's web UI using the correct locale and language.
For Firefox this works but I have no luck for Chrome:
profile = Selenium::WebDriver::Firefox::Profile.new
profile["intl.accept_languages"] = "ES"
caps = Selenium::WebDriver::Remote::Capabilities.firefox(firefox_profile: profile)
caps.platform = "Linux"
caps.version = 20
browser = Watir::Browser.new :firefox, :desired_capabilities => caps
Thanks.
The "intl.accept_languages" is a Chrome preference, so can be passed in as:
Watir::Browser.new :chrome, options: {prefs: {'intl' => {'accept_languages' => 'ES'}}}
I am using the chrome namespace for both Chrome and Firefox, but would like to know which browser is running the web extension.
Links to extension resources have different schemes in Chrome and Firefox.
const isFirefox = chrome.runtime.getURL('').startsWith('moz-extension://');
const isChrome = chrome.runtime.getURL('').startsWith('chrome-extension://');
Check chrome.app which is absent in Firefox:
const isFirefox = !chrome.app;
Check for browser which is absent in Chrome:
const isFirefox = window.browser && browser.runtime;
(the additional check is to avoid false positives on pages that have an element with id="browser" that creates a named property on window object for this element)
Use the asynchronous browser.runtime.getBrowserInfo.
P.S. navigator.userAgent may be changed during debugging in devtools when switching to device mode or via about:config option in Firefox so it's an unreliable source.
This is what I do in my own extensions to check for Firefox (FF) vs Chrome:
const FF = typeof browser !== 'undefined';
Update: (1)
Here is an explanation .....
I am using the chrome namespace for both Chrome and Firefox, but would
like to know which browser is running the web extension.
AFA I understand, the question relates to extension code and not content code. I use above code in background script in "firefox-webextensions" or "google-chrome-extension" background script.
From then on then code would be:
if (FF) {...}
else { .... }
Once established, content script has no bearing on it.
In case of a developer who somehow decides to use id="browser" then a further step could be added which returns a boolean true|false e.g.
const FF = typeof browser !== 'undefined' && !!browser.runtime;
Worth nothing that the following returns an object or undefined and not a boolean
const isFirefox = window.browser && browser.runtime;
While it works fine in if() conditionals, it wont work in other situations where a boolean would be required (e.g. switch)
(1) Note: Marking down answers, discourages people from spending time and effort in answering questions in future.
I'm trying to write a Chrome/Firefox extension in inject MathJax 3.0.1 into arbitrary web pages, Green Pi. It's working well for Chrome, but I'm having trouble with Firefox.
The content_script.js is
MathJax = {
chtml: {
fontURL: chrome.runtime.getURL("fonts"),
},
};
require("mathjax-full/components/src/tex-chtml/tex-chtml.js");
// This paints pages green
// require("green.js");
// // cat green.js == document.body.style.backgroundColor = "green";
(It's getting a little more involved when the user opts in/out of certain pages, but this isn't relevant here.)
Now, as noted in the code, the above works fine in Chrome, but fails in Firefox with the MathJax error
MathJax(?): window.MathJax is undefined
I don't see any other warning or error. Any hint on what might be going wrong here?
This turned out to be a MathJax bug after all, cf. https://github.com/mathjax/MathJax/issues/2399.
Idea is to be able to modify document.referrer variable just before any JavaScript execution on website loaded by headless browser. What browser to use is not relevant, I have tried PhantomJS and Zombie without any luck.
My researches indicates that currently it is like so:
PhantomJS - no, since it is const somewhat taken from Referrer header, but even providing this header .referrer is still an empty string in result.
zombie - unknown.
you may try this in phantomJS
var webPage = require('webpage');
var dpage = webPage.create();
dpage.customHeaders = {
"Referer": "https://www.facebook.com"
};
How can I detect if the browser is an internet explorer or firefox or chrome? Is there an easy way like just using jquery. Because I want to limit the jquery calls if my user agent is internet explorer.
Please advise.
Many thanks.
jQuery.browser is deprecated in jQuery since 1.9.
There is a plugin for jQuery that adds this "back" to jquery. You will fid it (and documentation) at https://github.com/gabceb/jquery-browser-plugin
Install it by adding <script src="/path/to/jquery.browser.js"></script> after where you are adding jQuery.
then you can use the following.
$.browser.msie; //returns true if ie
$.browser.webkit; //returns true if webkit-browser (Safari, Chrome)
$.browser.mozilla; //returns true if firefox
Try jQuery.browser
Check if IE
$.browser.msie ? alert('Internet Explorer') : alert('Not Internet Explorer');
Or info for the browser that is accessing page
$.each($.browser, function(i, val) {
$("<div>" + i + " : <span>" + val + "</span>").appendTo(document.body);
});
Working example here