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.
Related
I am trying to make a Chrome extension, which will monitor GMail and do something when user starts to write a message. After some study of examples and documentation I have figured out that I should do it with declarativeContent, which reacts on page change.
This is what I have done by now.
manifest.json:
{
"manifest_version": 2,
"name": "Gmail helper",
"version": "0.1",
"permissions": [ "declarativeContent" ],
"background": {
"scripts": ["background.js"],
"persistent": false
}
}
background.js:
chrome.runtime.onInstalled.addListener (function (details) {
chrome.declarativeContent.onPageChanged.removeRules (undefined, function () {
chrome.declarativeContent.onPageChanged.addRules ([{
conditions: [
new chrome.declarativeContent.PageStateMatcher({
pageUrl: { hostEquals: 'mail.google.com', schemes: ['https'] },
css: ["div"]
// css: ["div[aria-label='Message Body']"]
})
],
actions: [ new chrome.declarativeContent.RequestContentScript({js: ["content.js"]}) ]
}]);
});
});
content.js:
alert ("Test");
My plan was to declare a content script that would trigger on page changes in GMail. I have added a declarative rule, which has pageURL, css and actions defined. According to my understanding content.js should be executed when pageUrl and css content match. However, content.js is not executed.
What am I doing wrong?
Thanks.
Running a content script on a site requires permissions for the site, which isn't stated explicitly in the documentation of declarativeContent API, but is deduced by the lack of "This action can be used without host permissions" note, which is present in other actions. The purpose of declarativeContent API is to install extensions from the WebStore without any permission confirmation warning so naturally this API can't grant you access to mail.google.com unless you add it explicitly in your manifest:
"permissions": ["declarativeContent", "https://mail.google.com/"]
From description of your task it looks like you don't need declarativeContent.
You need to add a content script to page if GMail page is open and in content script you need to add a listener to message editor DOM element (or whatever another element you need to track).
Assuming that you know how to do the second, to add content script to GMail page you need to add the following into manifest:
"content_scripts": [
{
"matches": [
"https://mail.google.com/*"
],
"js": ["content.js"]
}
You also don't need background script and permissions in this case.
Note:
Although you don't need to specify permissions, your extension will require to ask them from the user.
During installation, Chrome will warn the user that your extension will have access to all user data on the page to make the user able to cancel installation if he or she does not agree.
In the document description, RequestContentScript is experimental, but it is not. In my test, Chrome version >= 60 is available, but allFrames does not seem to work.
Before using "RequestContentScript", you need to declare host permissions in "permissions".
https://developer.chrome.com/extensions/declarativeContent#type-RequestContentScript
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!
I can't seem to make this work no matter what I do. Let me demonstrate with a very simple example extension. Here is the manifest.json:
{
"manifest_version": 2,
"name": "Sample Of Content Script",
"description": "Changes the background of a page pink",
"version": "1.0",
"content_scripts": [
{
"matches": [ "<all_urls>" ],
"js": [ "changer.js" ]
}
],
"permissions": [
"webNavigation"
],
"background": {
"scripts": [ "background.js" ]
}
}
Notice that my content_scripts entry matches all_urls, which (according to Google documentation) should match file:/// URLs.
The background.js:
(function (chrome) {
'use strict';
chrome.webNavigation.onCompleted.addListener(function (details) {
chrome.tabs.sendMessage(details.tabId, {
action: 'changeBackground',
color: 'pink'
});
});
})(chrome);
And the changer.js (content script):
(function (chrome) {
'use strict';
chrome.runtime.onMessage.addListener(function (request) {
if (request.action !== 'changeBackground') { return; }
document.body.style.background = request.color;
});
})(chrome);
This extension has been published on the Chrome Web Store so you can see the result in action:
https://chrome.google.com/webstore/detail/sample-of-content-script/bkekbfjgkkineeokljnobgcoadlhdckd
It's a pretty simple extension. Navigate to a page, and it turn's the page's background pink. Unfortunately, it doesn't work for any file:/// URLs. The changer.js script is not loaded into the page, and nothing happens.
Extra info
It actually seems to work just fine when running as an Unpacked extension in Developer mode. Pages loaded from the file system turn pink.
I tried using chrome.tabs.executeScript() instead of putting the script into the manifest. This failed in a more obvious way, saying that I didn't request permissions to modify file:/// URLs in the manifest.
I added "file:///*/*" to the permissions section of manifest.json. That seemed to workwell with chrome.tabs.executeScript(), but the Chrome Web Store rejected the extension, saying that file:/// permissions are not allowed.
I reverted to a content_script section in manifest.json and tried adding "file:///*/* to the matches section in the manifest. Again, this worked in a development build, but when I uploaded it to the Chrome Web Store and then installed it, it didn't work.
<all_urls> indeed covers file:// scheme, but it must be manually activated in the extensions list.
If an extension has permissions that cover file:// scheme, it will have a checkbox "Allow access to file URLs" next to "Allow in incognito". The user must enable that manually; you can help by creating a tab with preconfigured URL, after explaining the process:
chrome.tabs.create({url: "chrome://extensions/?id=" + chrome.runtime.id});
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.
I'm working on a chrome extension and I need to get the event when the tab is closed so I can fire of an post to a server. This is what I have atm.
chrome.tabs.onRemoved.addListener(function (tabId) {
alert(tabId);
});
But I can't get it to work. Anyone got any ideas?
Edit:
When I'm running it, it says
Uncaught TypeError: Cannot read property 'onRemoved' of undefined
Edit2: manifest.json
{
"name": "WebHistory Extension",
"version": "1.0",
"manifest_version": 2,
"description": "storing webhistory",
"content_scripts":[
{
"matches": ["http://*/*"],
"js": ["jquery-1.7.min.js","myscript.js"],
"run_at": "document_end"
}
],
"permissions" : ["tabs"]
}
You can't use chrome.tabs API in content scripts:
However, content scripts have some limitations. They cannot: Use
chrome.* APIs (except for parts of chrome.extension)
source
What you need to do is to establish communication between content script and background page. Background page has access to chrome.tabs API:
These limitations aren't as bad as they sound. Content scripts can
indirectly use the chrome.* APIs, get access to extension data, and
request extension actions by exchanging messages with their parent
extension.
source
Everything is in the first five paragraphs of content script documentation.