Google Chrome App - check if app or regular site - javascript

I am working on project which will run as Chrome App & regular site.
How can I test/check in my JS if I am in an Chrome App? (i.e. some functionality will only work under chrome)
Just FYI, here is my Chrome App manifest, please note I am running this in the developer mode (directly from the source, not packaged yet)
{
"manifest_version": 2,
"name": "Example KIOSK APP",
"version": "1.1",
"icons": {
"16": "images/icon-16.png",
"48": "images/icon-48.png",
"128": "images/icon-128.png"
},
"app": {
"background": {
"scripts": ["background.js"],
"persistent": true
}
},
"kiosk_enabled": true,
"offline_enabled": true,
"permissions": [
"system.display",
"power",
"webview",
"fileSystem",
"alwaysOnTopWindows",
"system.storage",
"<all_urls>"
]
}
Any suggestions much appreciated.

Turns out the question meant to distinguish between identical code running in a webpage and inside a (regular) Chrome App window.
It is enough to test for Chrome App APIs that are never exposed to regular pages. An example of that would be to test for app.runtime:
if (window.chrome && chrome.app && chrome.app.runtime) {
// Running inside a Chrome App context
} else {
// Either not Chrome, or not as an app window
}

Edit: This answer turned out not to be relevant to this particular question, but I think I will leave this in case someone stumbles upon this question with a hosted app.
I assume that by "run as Chrome App" you mean a hosted Chrome App.
In this case, it is enough to check chrome.app.isInstalled from the website's code. It's not easy to find this in the documentation, as it was apparently left out as some point, but I will put this as a reference. I just checked and it still works.
So:
// Website code
if (window.chrome && chrome.app && chrome.app.isInstalled) {
// App is installed
} else if (chrome) {
// In Chrome, but app is not installed: offer inline install?
} else {
// Not in Chrome at all
}

I am searching for a way to know, if Chrome was started with --app=https://example.com (so in single page mode) or as full browser (with tabs, menu, etc.).
The answers above don't seem to apply in this case, as it is not an "installed" app. (Chrome app supported was discontinued, right?)
Is there a way to detect, if the page is was opened with --app?

Related

Can Firefox add-ons run for non-HTML requests such as images?

I am following the instructions on developing a WebExtension for Firefox. I have it working for HTML pages. But, I would also like my script to run when the requested "page" is an image. Is this possible?
Here is what I have:
manifest.json:
{
"manifest_version": 2,
"name": "Test",
"version": "1.0",
"applications": {
"gecko": {
"id": "alert#example.com",
"strict_min_version": "42.0",
"strict_max_version": "50.*",
"update_url": "https://example.com/updates.json"
}
},
"description": "Just does an alert.",
"icons": {
"48": "icons/border-48.png"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["alert.js"]
}
]
}
alert.js:
alert("ok");
After installing the plugin by following the instructions (by going to "about: debugging"), I get a JavaScript alert for all HTML pages. But if I navigate to an image (for example by right clicking on an image and choosing "View Image"), no alert occurs.
This is a bug in Firefox that is mostly, but not fully, resolved in Firefox 54 (currently Firefox Beta). Your script (mostly) works as expected (shows the alert when viewing an image from the context menu selection "View Image") in Firefox Beta and Firefox Developer Edition.
The issue I encountered, when testing with Beta and Developer Edition, was that the content script was intermittently not injected when using the forward and back buttons to navigate away from and back to the "View Image" page.
It is possible to be certain that your content script is loaded using tabs.executeScript(), and the events from the webNavigation, webRequest and tabs APIs. However, doing so generically for any content script on any page is...complex (a full generic implementation takes more code than the maximum characters permitted in a Stack Overflow answer).
WebExtensions is actively in development
The WebExtensions API is still in development. What is working improves with each version of Firefox. If you encounter problems, you should test your WebExtension add-on with Firefox Developer Edition, or Firefox Nightly. You should also make careful note of what version of Firefox is required for the functionality you desire to use. This information is contained in the "Browser compatibility" section of the MDN documentation pages.

chrome.storage not working

I have a google chrome app, and I tried to use the chrome.storage.sync.get, and it says, "cannot read property sync of undefined". Can someone please tell me what is going on? I have tried copying and pasting the exact line from the chrome developer website.
The code is literally:
$(".thing").click(function() {chrome.storage.sync.set(stuff)}
As far as I can tell, the mistake is just that I'm trying to use the chrome storage API in Google Chrome, not as an app.
Please follow these steps while you are using chrome.storage.sync
Declare storage permissions in your manifest.
manifest.json
{
"manifest_version": 2,
"name": "Storage",
"description": "This extension shows a Google Image search result for the current page",
"version": "1.0",
"icons":{
"256":"img/icon.png"
},
"permissions": [
"storage"
],
"app": {
"launch": {
"local_path": "options.html"
}
},
"background": {
"scripts": ["js/app/background.js"]
}
}
Reload your App in chrome://extensions so that your manifest.json gets updated in your browser.
Follow the exact syntax of chrome.storage.sync with its callback function.
Sample.js
$(".thing").click(function() {
chrome.storage.sync.set({"myKey": "testPrefs"},function(){
alert("object stored");
})
chrome.storage.sync.get("myKey",function(obj){
alert(obj.myKey);
})
});
This will work for you. I tested this code in my chrome app.
It turns out that I was trying to run this in google chrome (browser), not as a chrome app. Chrome doesn't know it should parse the code as an app, and ignores the line about chrome.storage. Something to watch out for, chrome developers!

Using content scripts/messaging with Chrome Apps (not extension) to send data

So I've been trying to send data from a web page to the Chrome Application (not Chrome Extension). Reading stuff around, according to me, this primarily would require the use of url_handlers for invoking Chrome App from a web page, content scripts to inject JS into the web page and/or Messaging for communication between Chrome Application and the web page.
Also, installed App from it's .crx, else loading unpacked directory leads to this error:
"content_scripts is only allowed for extensions and legacy packaged apps, but this is a packaged app."
Now, I tried injecting JS in the required site. However, on inspecting using Chrome Dev Tools, there's no such entry in the Sources->Content scripts section for that site. It simply didn't inject itself when using a Chrome App. It works perfectly with extensions but I want to use Chrome App for other functionalities.
As an alternative, I looked for Messaging examples where its usage is mentioned as:
"... your app or extension can receive and respond to messages from regular web pages."
But, somehow I couldn't get it working.
Any headers on either of the approaches? Other Suggestions?
manifest.json:
{
"name": "App",
"version": "1.0",
"manifest_version": 2,
"minimum_chrome_version": "31",
"app": {
"background": {
"scripts": ["background.js"]
}
},
"permissions": [
{"fileSystem": ["write", "retainEntries", "directory"]},
"storage",
"http://example.com/*"
],
"externally_connectable": {
"matches": ["http://example.com/*"]
},
"content_scripts": [{
"matches": ["http://example.com/*"],
"js": ["content.js"]
}]
}
Indeed, you can't have content scripts in an app.
However, using externally_connectable is valid.
You have already declared that you want to be externally connectable from example.com. (Note: be careful when defining match patterns, for instance this one does not cover www.example.com)
In example.com's own scripts, you can then include the following:
chrome.runtime.sendMessage("idOfYourAppHere", message, function(response) {
/* ... */
});
And in the app (probably its background script) you can catch that with
chrome.runtime.onMessageExternal.addListener(function(message, sender, sendResponse) {
/* ... */
sendResponse(response);
});
This does require you to know the ID in advance. You can pin it by packing your extension and extracting the "key" field from the manifest. See this question for some more details.

Write to a Google Doc from inside a Chrome Extension

I want to create a Chrome extension that runs through a forum and copies all the links to each post on a particular page to a Google Spreadsheet. I can't find much on using Google Apps Script in a Chrome Extension - does anyone have any resources or can you point me in the right direction? Thanks in advance!
You can absolutely launch an Apps Script published as a web app from a chrome extension. Here is a simple example. This extension will ask you to launch a script every time you open www.google.com. Annoying, but I tried to make it as simple as possible.
Background.js
// Handle requests for script launch
chrome.runtime.onMessage.addListener(function(request) {
if (request.type === 'launch_script') {
chrome.tabs.create({
url: 'https://script.google.com/a/macros/YOUR SCRIPT URL/exec',
active: false
}, function(tab) {
// After the tab has been created, open a window to inject the tab
chrome.windows.create({
tabId: tab.id,
type: 'popup',
focused: true
// incognito, top, left, ...
});
});
}
});
manifest.json
{
"name": "Script Launch Test",
"version": "1.0",
"manifest_version": 2,
"background": {
"scripts": ["background.js"],
"persistent": false
},
"content_scripts": [{
"matches": ["https://www.google.com/*"],
"js": ["open-dialog.js"]
}]
}
open-dialog.js
if (confirm('Launch Script?'))
chrome.runtime.sendMessage({type:'launch_script'});
The basic answer is yes, this is possible. This answer to a similar question includes a minimal working example.
However, given the restrictions GAS works under, I would instead consider skipping the Apps script part entirely, and instead using the Sheets APIs to access the content directly.

Can I disable the “You've gone full screen” notification in Chrome with chrome extension?

/How/ Can I disable the “You've gone full screen” notification in Chrome with chrome extension?
I have got this manifest.json:
{
"name": "Me",
"description": "Disable Fullscreen notification",
"version": "2.0",
"permissions": [
"activeTab"
],
"browser_action": {
"default_title": "Make this page red",
"default_icon": "icon.png"
},
"manifest_version": 2
}
No, you cannot remove the "You have gone full screen" notification, because this message is a security feature. It tells users how to exit full screen mode in case they do not know.
The only way to get in full screen without this notification is to start up Chrome with the --kiosk flag.
There's no extension for that.. but still you can start chrome in full screen mode without that message by typing in terminal
google-chrome --kiosk www.webpage.com
As others have pointed out, an extension cannot disable the notification. However, there are still a couple ways to go about disabling this "feature," such as by recompiling the source code or using the following launcher for the win32 version of chrome which automatically patches the executable on the fly, available at https://www.dropbox.com/s/5rn7fx94ts73jfb/chrome_v3.zip?dl=0. Source is included.
Edit: Note that chrome must not already be running for the launcher to work properly

Categories

Resources