setUninstallURL - in Safari and Firefox? - javascript

We created extensions for Chrome, Firefox and Safari. We want to open a new tab when uninstalling our extension. I found this code for Chrome, but is it possible to do it in Safari and Firefox?
switch (platform) {
case 'chrome':
try {
chrome.runtime.setUninstallURL(uninstall_url);
} catch(e) {
console.error(e);
}
break;
}

In place of manifest.json Safari extensions use Info.plist which is typically generated with ExtensionBuilder. As you can see from the provided screenshot, no 'uninstallURL' is available. I suggest testing a beforeUnload listener in the global page but this will probably also be triggered during other events like updates, browser closing etc.
As for Firefox, it all depends on the actual API your add-on is based on. You maybe out of luck if you have used the Addon SDK or created a legacy XUL overlay extension. However, Restartless bootstrapped extensions use low-level APIs and the uninstall function in bootstrap.js receives a reason parameter:
function uninstall(data, reason) {
if (reason === ADDON_UNINSTALL) {
let win = Services.wm.getMostRecentWindow('navigator:browser');
win.gBrowser.selectedTab = win.gBrowser.addTab(url);
}
}
Good news is a new WebExtensions API is coming to Firefox. It is basically identical to the Chrome API. See the relevant bug

From addon sdk you would use this https://developer.mozilla.org/en-US/Add-ons/SDK/Tutorials/Listening_for_load_and_unload
That shows you how to listen for load and unload and detect the reason for load/unload solution is from here - https://stackoverflow.com/a/31497334/1828637

Related

How to check if a Firefox WebExtension is installed or not with page JavaScript?

I have developed a WebExtension for Firefox and my website works with the extension as a prerequisite. I need to check programmatically whether the extension is installed or not and if not ask the user to install it.
I am not able to find a way how to check this operation of whether my extension is already installed in the user's browser.
Editor note: Methods available in Firefox differ from those available in Chrome, so this question is not a duplicate.
Important note to begin with: A page can't query if an extension is installed without explicit help from the extension. This is done to prevent browser fingerprinting and/or preventing sites from denying content if certain extensions are installed.
WebExtensions are largely built upon the same principles as Chrome extensions. As such, this question is relevant: Check whether user has a Chrome extension installed.
However, some of the best methods available in Chrome are currently unavailable in Firefox:
You can't use external messaging from a webpage (through externally_connectable) as it's not available in FF.
You can't use web-accessible resources for checking presence since Firefox intentionally shields them from fingerprinting:
The files will then be available using a URL like:
moz-extension://<random-UUID>/<path/to/resource>
This UUID is randomly generated for every browser instance and is not your extension's ID. This prevents websites from fingerprinting the extensions a user has installed.
As such, what are your options? The page can't talk directly to the extension context (background), and the background can't directly affect the page; you need a Content script to interact with the page content.
How can page code and a content script communicate? They are isolated from each other unless content script does something about it.
First off, generic tricks that work in both FF and Chrome:
You can create or modify a DOM element on the page from a content script and look for those modifications in the page.
// Content script
let beacon = document.createElement("div");
beacon.classname = browser.runtime.id;
document.body.appendChild(beacon);
// Page script
// Make sure this runs after the extension code
if (document.getElementsByClassName("expected-extension-id").length) {
// Installed
} else {
// Not installed
}
You can use postMessage to communicate between contexts, though it's clunky to use as a bidirectional channel.
Here's documentation and sample WebExtension.
// Content script code
window.postMessage({
direction: "from-content-script",
message: "Message from extension"
}, "*");
// Page code
window.addEventListener("message", function(event) {
if (event.source == window &&
event.data.direction &&
event.data.direction == "from-content-script") {
// Assume extension is now installed
}
});
You can use custom DOM events in a similar way.
There are interesting Firefox-specific approaches as well:
You can share code with the page using exportFunction or cloneInto:
// Content script
function usefulFunction() {
/* ... */
}
const extensionInterface = {
usefulFunction
}
window.wrappedJSObject.extensionInterface =
cloneInto(extensionInterface, window, {cloneFunctions: true});
// Page code
if (typeof window.extensionInterface !== "undefined") {
// Installed
window.extensionInterface.usefulFunction();
} else {
// Not installed
}

How to detect that browser is running in Safari on iOS 11 via JavaScript?

I want to detect that my JS code in a webpage is running in Safari on iOS 11, which supports some new features.
I can do something like
if (window.navigator.userAgent.includes('OS 11_0')) {
// iOS 11
}
but I believe this is considered unreliable.
Is there some feature or a hack that works only on iOS 11 and not on other OS and can be used to detect that version without inspecting the userAgent?
Update: I am talking about getUserMedia so I am not sure if ther is a way to test it presence without triggering the microphone permission request.
Check out this solution, and then you could do something like this:
ver = iOSversion();
if (ver[0]==11) {
// do something
}
The shared snippet can also be used to detect any specific iOS version, >iOS 2.

Detect if another Chrome app is running

I've got a Chrome extension and a Chrome app, which can communicate with each other over messaging. There are certain actions I want the extension to take only when the Chrome app isn't running. Given the Chrome app's Id, how can I detect from the Chrome extension if it is running?
I've tried using closed and suspend lifecycle events from the chrome app to assist, but that route isn't seeming possible due to various reasons. Is there any other way to detect?
Well, you are already using Messaging, so presumably App's event page will wake up to answer if you call it, even if the app is not launched.
You define "running" as having windows open. From the event page, you can check that:
chrome.runtime.onMessageExternal.addListener(function(message, sender, sendResponse) {
/* ... */
if (chrome.app.window.getAll().length) {
// Launched
} else {
// Not launched
}
});
Something like:
chrome.management.get(appId,function(theapp){
if(theapp.enabled){
// it is running
} else {
// it is not running
}
});

Starting Android app from browser: How to feature-detect "Intent" support?

We are trying to build an parameterized entry webpage for our app in which about 50% of users will have our app installed, 50% not.
Originally, we had been using a specialized URL scheme that our app had registered; ie,
myapp://dothing?specialParameters=5
However, we have difficulty detecting cases where users either don't have our app installed, or have an earlier version of our app that doesn't support URL schemes. In Chrome, or the Android Browser, the user is navigated to a browser-generated Error page since it couldn't locate the server.
In iOS Safari, and Android Firefox, we can resolve this through the use of a setTimeout before navigation;
function timeoutFn() {
var timeout = setTimeout(function() {
window.location = //iOS Appstore location;
}, 1000);
window.addEventListener("pagehide", function(evt) {
clearTimeout(timeout);
});
window.location = "myApp://dothing?specialParameters=5";
}
My solution for Chrome/Android Browser is to use Google's recommended system of Intents, described here:
https://developers.google.com/chrome/mobile/docs/intents
I've tested this out, and it actually seems to work well - however, Firefox does not know how to handle links beginning with "intent://". We can still use the above JavaScript function for Firefox, but I'm very reluctant to start writing user-agent-sniffing code for it, especially since I'd think it likely any other web browsers on Android will similarly lack Intent support.
So, back to the title question: Chrome, and the Android browser, are capable of opening apps through "intent://" links - is there any way, using JavaScript or similar methods, to use feature detection to find this support?
This is what we are currently using, however, it's far from ideal ...
function isItentsSupported() {
var isChrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;
if (isChrome) {
var version = parseInt(window.navigator.appVersion.match(/Chrome\/(\d+)\./)[1], 10);
return version >= 25;
} else {
return false;
}
}
Chrome version number based on Google documentation

How to prevent script error in C# webbrowser?

I am getting script error in loading some web sites.
Please help me how to prevent script error in C# WebBrowser.
This is my code:
try
{
webBrowser1.Navigate(textBox1.Text);
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
MessageBox.Show("Loaded");
}
catch(Exception)
{
MessageBox.Show("failed");
}
Write this in Your Code
webBrowser1.ScriptErrorsSuppressed = true;
To disable the script error pop up, you need to go to (in Internet Explorer) Tools->Internet Options, there go to the Advanced tab, and in Browsing select Disable Script Debugging (Other), but, the problem may be related to the fact that every site loaded in the WebBrowser control is rendered in IE7 compatibility mode, to solve this the only thing you can do is a registry hack like this: WebBrowser control to use IE9
change your registry to 2af8 which is IE 11 for devenv.exe
software/Microsoft/internet explorer/main/featurecontrol/feature_Browser_emulation
If your working with a GUI (like in Visual Studio) just go to the Webbrowser Properties and set "ScriptErrorsSuppressed" = true
It is easy to see how this error has been eliminated. But Visual Studio uses Internet Explorer and another Web Browser coding is hard. If you look at the versions, the last Visual Studio one uses IE 11 for WebBrowser tool.
The C++ GUI is recommended for one of each encoding:
https://msdn.microsoft.com/en-us/library/60k1461a.aspx

Categories