I'm pretty new at chrome extensions and am trying to make a simple one that automatically launches links in my emails. I am going to modify it a bit later on, but for now, this is all I am trying to do. How do I have a chrome extension automatically read the text of the current tab that I am on, or when I open emails if I can get that specific? I have a manifest file set up and currently can make the extension button launch a link, but I'd rather have this happen automatically, as I don't want to hit a button to launch a link when I could just click the link itself.
manifest.json
{
"manifest_version": 2,
"name": "MT task launcher",
"description": "This extension launches Task Links in emails",
"version": "1.0",
"background": {
"scripts": ["task.js"]
},
"browser_action": {
"default_icon": "icon.png",
"default_title": "Email Task Launcher"
},
"permissions": [
"activeTab"
]
}
task.js
chrome.browserAction.onClicked.addListener(function(tab) {
var action_url = "http://www.reddit.com";
chrome.tabs.create({ url: action_url });
});
Take a look at Official Guide, for your purpose, I think you should use content scripts ( which are injected into current web page), then read the DOM and get all the links. To open the links, you can either call window.open() or by passing message then open them via chrome.tabs.create
There are two options to do that, it's either edit the local copy of the extension or to inject the call to the extension.
Inject code as a Content script, use the matching rules as defines in the manifest file
A background page file use the 'chrome.tabs.onUpdated' event. Also, use the 'chrome.tabs.executeScript' method to inject script.
Related
How do i display an Alert message instead of the pop up.
My goal is to launch a script when the user clicks the extensions icon. The pop up is unnecessary, in fact i would like to avoid letting the pop up to initiate all together. I want my extension to behave like the Gmail hyper link, but instead of opening a link, I want it to execute my script.
Take a look at the api.
You will want to remove the popup in your manifest and have a onClicked handler instead. It will look like this:
chrome.browserAction.onClicked.addListener(function(){
alert("stuff");
});
The important part is that you don't define a popup in your manifest as it prevents the event from firing.
Rather than a popup, I'm assuming you want to run a background script to listen for a browserAction event.
First, make sure you remove "default_popup": "popup.html" from your manifest.json. Then include the background script in "background".
Your manifest json should be something like this:
"browser_action": {
"default_icon": "image.png",
"default_title": "My Extension"
},
"background": {
"scripts": ["background.js"]
},
This is probably a simple question, but I'm trying to create a Google extension without an icon, one that just runs in the background without a popup or anything. What would I put into the manifest to get this to happen?
Simply remove this from the manifest.json:
...
"browser_action": {
"default_icon": "icon.png"
},
...
Now the app will not appear in the top bar.
I'm completely new to Chrome Extensions. I want the creation of a bookmark to trigger a xmlhttprequest. Right now, I'm just trying to get a new bookmark event to do a console.log and can't see what I'm missing.
Here is my manifest.json:
{
"manifest_version": 2,
"name": "Booky Desktop Integration",
"description": "Sends New Chrome Bookmarks To Your Booky Desktop.",
"version": "1.0",
"background": {
"scripts": ["background.js"],
"persistent": false
},
"permissions": [
"bookmarks",
"http://bookydesktop.com/"
]
}
Here is my js:
chrome.bookmarks.onCreated.addListener(function(id, bookmark) {
console.log("bookmark created");
});
What am I missing?
Your code works perfectly as written. You're probably not viewing the console for your background page. You need to:
Open chrome://extensions/ (or click "Extensions" in Chrome's "Settings" menu)
Ensure "Developer mode" is ticked in the top right
Open the console by clicking "_generated_background_page.html (Inactive)" in the "Inspect views" list under your extension
Each page in Chrome has its own console instance. You were looking at the consoles of ordinary web pages, instead of looking at the console for your background page.
I'm in the process of writing a Google Chrome extension, specifically a lightweight EPUB reader, with the goal of it being just about as slick as the built in PDF reader in Chrome.
What I'm trying to figure out is if you can somehow trigger the extension automagically when you click a link that would normally force you to save an epub file to disk.
In a perfect world every link to an epub file would point to a file.epub and you'd be able to use a content script to point that to the extension, but unfortunately more often than not that's not the case. So what I'm thinking is that it would have to be triggered after the HTTP get request has returned the headers at least, and trigger off of the mime type via a chrome.webRequest or something.
Can you invoke a Google Chrome extension from the mime-type of a file being served to you?
Assuming you are having browser action for all the functionality, you can detect and enable\disable extension with chrome.webRequest.onHeadersReceived, chrome.browserAction.enable() and chrome.browserAction.disable()
Pseudo Code
manifest.json
{
"name": "Detect Mime type",
"version": "1.0",
"description": "Detecting Mime type and enabling browser action",
"permissions": ["webRequest", "webRequestBlocking",
"<all_urls>"],
"background": {
"scripts": ["background.js"]
},
"browser_action":{
"default_popup":"popup.html", // Use you related code here
"default_icon":"icon.jpg"
},
"manifest_version": 2
}
background.js
chrome.webRequest.onHeadersReceived.addListener(function (object){
// Do your code for related header(s)
//Invoke chrome.browserAction.enable(integer tabId) or chrome.browserAction.disable(integer tabId)
},{urls: ["<all_urls>"]});
chrome.tabs.query({"status":"complete"}, function(tabs){
//Disable all browser actions using chrome.browserAction.disable(integer tabId)
});
Let me know if you need more information.
I am testing my Chrome extension by using the following codes:
chrome.tabs.create({url: "login_popup.html"}, function(tab){
chrome.extension.sendRequest({oid: tab.openerTabId});
});
However, a new tab was created only when Chrome started, but no tabs were created when I opened open a new page. Shouldn't the chrome.tabs.create be executed whenever a new page is loaded?
The manifest.json is:
{
"name": "minus_test",
"version": "1.0",
"background_page": "minus_test.html",
"permissions": [ "*://*/", "tabs" ]
}
Thanks!
No. background_page gets executed once when Chrome starts at which point it creates a new tab. If you want to create a new tab ever time a new window is opened you need to use the chrome.windows.onCreated listener.