chrome.storage not working - javascript

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!

Related

When I try to load an (unpacked) extension into Edge, why does it say that web_accessible_resources[0] is invalid?

I'm building a browser extension for Microsoft Edge in which I need to inject a script into certain pages. Whenever I try to load the unpacked extension, Edge reports, “Error Invalid value for 'web_accessible_resources[0]'.” However, I've looked at the documentation and some examples and it appears that I have a valid configuration. Below is a minimum working example to reproduce the error:
manifest.json
{
"manifest_version": 3,
"name": "MWE",
"version": "0.0.0",
"web_accessible_resources": [{
"resources": ["script.js"],
"matches": ["<all_urls>"]
}]
}
script.js
<empty>
I'm using Microsoft Edge Version 86.0.622.69 (Official build) (64-bit). Why am I getting this error? What can I do to resolve this? I'm able to use Manifest v2 with success, but I want to use Manifest v3.
I tried using the Manifest v2 format for web_accessible_resources. It loaded successfully, but it had a background error that said, “The maximum currently-supported manifest version is 2, but this is 3. Certain features may not work as expected.” I guess that solves that. Microsoft's documentation didn't make that clear.

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

Google Chrome App - check if app or regular site

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?

Google Chrome Other Bookmarks Toolbar Object Location

I'm trying to figure out a way to access the section of the bookmarks toolbar where 'Other Bookmarks' is stored. I would like to place something next to it.
Is this hard coded into Chrome or can I access it through the API?
Thanks
You must declare the "bookmarks" permission in the extension manifest to use the bookmarks API. For example:
manifest.json
{
"name": "My extension",
...
"permissions": [
"bookmarks"
],
...
}
Read complete documentation here.

Categories

Resources