One link for Firefox and Chrome add-ons? - javascript

I have add-on for Chrome and Firefox, they are same just for different browsers. Is it possible to have one link/button for the add-on, once user clicks it, it will be recognized which browser user is on and opens Chrome vs Firefox add-on.
It would be possible to have in html 2 buttons and hide one for each browser, but I would like to have just one button.
Thanks!

You can extract browser name from navigator.userAgent then set needed link
Add-on Link
<script>
var link = document.querySelector("#link");
var userAgent = navigator.userAgent;
if (userAgent.indexOf("Firefox") > 0) {
link.setAttribute("href", "https://www.mozilla.org");
} else if (userAgent.indexOf("Chrome") > 0) {
link.setAttribute("href", "https://google.com");
}
</script>

Related

window.open() does not open when clicked more than once in internet explorer

I am using window.open to open my PDF files. Following is my code.
Reactions of various browsers:
In chrome: First time when I click PDF icon, in new window opens the PDF file. I am not closing the opened PDF. Each time when I click on the same PDF icon again it focuses the same file again
In firefox: First time when I click , new window opens with PDF file(almost same as chrome), but on consequent clicks of the same file without closing the existing PDF, it opens the same file again and again and focus it.
In IE: Here is the major problem. On first click the samething happens as with other browsers but on consequent clicks the opened file is not getting focused and nothing happens at all.
Is there anyway to make IE focus on the same PDF that is opened in new window on consequent clicks? Also is there anyway to make firefox stop opening same file again and again and just focus on existing like in Chrome?
function open_page(url, name, features) {
var win = window.open(url,
(name == null ? 'page' : 'test'),
(features == null ? 'menubar=1,toolbar=1,scrollbars=1,width=1024,height=768,resizable=1' : features));
if (win == null) {
alert('Not open');
} else {
document.isUnloading = false;
console.log("#2");
}
}

Safari - data export/html download attribute not working

My app allows users to export GeoJSONs as .json files... the download works just fine in Chrome and Firefox, but in Safari, the user is directed to a url with data:text/ + GEOJSON STRING and the text of the GeoJSON is rendered on the page - no download at all.
$('#export_table > tbody > tr > td').each(function(){
geoObject = JSON.parse($(this).html());
layerName = geoObject.name;
exportRowToGeoJSON($(this).html(), layerName);
});
function exportRowToGeoJSON(storageObj, fileName){
dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(storageObj);
link = document.createElement('a');
link = document.body.appendChild(link); //FOR FIREFOX
link.setAttribute("href", dataStr);
link.setAttribute("download", fileName + ".json");
link.click();
};
So rather than triggering a download of the href datasStr as it does in the other browsers, Safari treats the href attribute as a url to link to.
Any way that I can get this functioning properly across Chrome, Firefox, and Safari?
Please look at w3schools.com
As you can see, you must be using a version of Safari that is under 10.1, correct? Is so, I do recommend updating your browser, or switch to Chrome, Firefox, or Opera.
Any version lower than 10.1 in safari has no support for HTML5 attributes/tags, which is why some websites require and updated browser.

Redirect Safari To Chrome

I'm trying to redirect user's of my mobile webapp to use Chrome rather than Safari. I tried using the following:
<script>
javascript:location.href="googlechrome"+location.href.substring(4);
</script>
However this constantly opens tabs in Chrome in a loop. Do you know how I can successfully do this?
Cheers,
Dara
This will cause the page to open every time the webpage is loaded, regardless if you are in Safari or Chrome. This is also very poor User Experience to just forward the user to another browser without their input.
It would be better to have some way for the user to open your site in Chrome and also to have an explanation why it is needed.
There are other schemes for https and callbacks: https://developer.chrome.com/multidevice/ios/links#uri_schemes
<p>This webapp is best viewed in Google Chrome</p>
<button type="button" onclick="openInChrome()">Open in Chrome</button>
<script>
var openInChrome = function() {
if (/^((?!Chrome).)*(Safari)+.*$/.test(navigator.userAgent)) {
var protocol = location.href.substring(0,location.href.indexOf(':'));
var url = location.href.substring(location.href.indexOf(':'));
if (protocol === "http") {
location.href = "googlechrome" + url;
}
else {
location.href = "googlechromes" + url;
}
}
}
</script>
Edit:
Added a check to verify they are in Safari.
Well, the reason is pretty obvious; Chrome is instructed to open Chrome too. You just want a userAgent conditional.
if (navigator.userAgent.indexOf("CriOS") == -1) {
location.href="googlechrome"+location.href.substring(4);
}
I would go on my standard rant about user agent checking being bad, but I trust what you're saying about this being a private webapp. Since iOS doesn't let you change your default browser, I guess this is a fair workaround.

How to programmatically add list of web-pages to browser favourites on click?

I currently have a list of useful webpages that I am adding to my web-page. I would like to add a link next to each URL that will add it as a bookmark in the users browser. How can I do this?
Further to this, how can I add a "Bookmark all links" button to my page ?
Bookmarking for the user isn't supported by some major browsers. You might want to just let the users do it themselves. If you insist, however, this post has some code Bookmark on click using jQuery
<script language="javascript" type="text/javascript">
$(document).ready(function(){
$("a.jQueryBookmark").click(function(e){
e.preventDefault(); // this will prevent the anchor tag from going the user off to the link
var bookmarkUrl = this.href;
var bookmarkTitle = this.title;
if (window.sidebar) { // For Mozilla Firefox Bookmark
window.sidebar.addPanel(bookmarkTitle, bookmarkUrl,"");
} else if(( window.external && window.external.AddFavorite) || document.all) { // For IE Favorite
window.external.AddFavorite( bookmarkUrl, bookmarkTitle);
} else if(window.opera) { // For Opera Browsers
$("a.jQueryBookmark").attr("href",bookmarkUrl);
$("a.jQueryBookmark").attr("title",bookmarkTitle);
$("a.jQueryBookmark").attr("rel","sidebar");
} else { // for other browsers which does not support
alert('Your browser does not support this bookmark action');
return false;
}
});
});
</script>
This Code is taken from Developersnippets!
Chrome does not support such actions, since the security level could be broken.

Bookmarking with javascript

I want to make a bookmark of my webpage by using javascript code. I want a javascript code to make a bookmark that should be opened in a new window.
I am using firefox.
Use this function:
function bookmark_us(url, title){
if (window.sidebar) // firefox
window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
var elem = document.createElement('a');
elem.setAttribute('href',url);
elem.setAttribute('title',title);
elem.setAttribute('rel','sidebar');
elem.click();
}
else if(document.all)// ie
window.external.AddFavorite(url, title);
}
In Firefox there is currently no way to do this (and there are bugs filed in bugzilla tracking this defect)
By "no way to do this" I mean that you can use the function Dreas gave but you will be limited to adding a bookmark that will default to the sidebar. The end user will have to manually un-check the "open in sidebar" option.
Ask on one of the Mozilla user groups (probably mozilla.dev.tech.xul or mozilla.dev.tech.xpcom). You could conceivably create the right XPCOM object to access the browser's bookmark manager -- this requires enabling privileges (the user will have to explicitly approve this). Not portable to other browsers (obviously) and perhaps not a great way of doing this.
Most websites I've seen that facilitate bookmarking (Jesse Ruderman's bookmarklet page is a great example) just ask the user to drag the appropriate bookmark to the bookmark folder of their choice, and this seems so much more straightforward/simpler than trying to automatically add something to the bookmarks.
function bookmark_us(url, title){
if (window.sidebar) // firefox
window.sidebar.addPanel(title, url, "");
else if(window.opera && window.print){ // opera
var elem = document.createElement('a');
elem.setAttribute('href',url);
elem.setAttribute('title',title);
elem.setAttribute('rel','sidebar');
elem.click();
} else if (document.all) // ie
window.external.AddFavorite(url, title);
}
When used with:
Bookmark Us
The above function no longer works as desired in Opera 9.x. It simply navigates to www.yahoo.com.
To make a link that pops up the Add Bookmark dialog in Opera 9.x, use the following:
function bookmark_us(url, title) {
if (window.sidebar && window.sidebar.addPanel) // firefox
window.sidebar.addPanel(title, url, "");
else if (window.external && 'undefined' != typeof window.external.AddFavorite) // ie
window.external.AddFavorite(url, title);
}
Bookmark Us

Categories