To execute a function on the button click in chrome extension - google-chrome

In the above screenshot you will see a panel and there are three buttons namely hide, close and clear.
I want to perform a particular action on click of those buttons.
Can you please suggest me some approach to achieve it?
Actually the panel created below is nothing but a div container which I have appended to the body of the current webpage.
So I had tried with one approach to inject my own script in that particular webpage and perform a particular action on those button clicks.
But that didn't work for me.
Can you please suggest me some other approach?
This is my manifest file :-
{
"name": "Demo Extension",
"version": "1.0.0",
"manifest_version": 2,
"browser_action": {
"default_icon": "icon.png"
},
"background": {
"scripts": ["jquery-1.7.2.js","code.js"]
},
"content_scripts": [
{
"matches": ["*://*/*"],
"js": ["jquery-1.7.2.js","content.js"],
"css": ["panel.css"],
"run_at": "document_end"
}
],
"permissions": [
"webRequest",
"tabs",
"http://*/*",
"https://*/*",
]
}
And my content.js file is as follows:
var data_div = $("<div class='panel'></div>");
data_div.css("position","fixed");
data_div.css("bottom","0px");
data_div.css("height","300px");
data_div.css("display","block");
data_div.css("width","100%");
data_div.css("overflow","scroll");
data_div.css("z-index","1004");
data_div.css("background-color","#C0C0C0");
$("body").append(data_div);
data_div.html("One big data set here");

Related

content script not being injected into extension html page

I have an extension that gets JSON data from a site for a list of characters and their resources when chosen from a menu. I'm using a single html page and trying to update it with javscript to modify the code for the menu. The only thing is that I can't get the content script injected/loaded into the html page.
When my extension is run, the path for the rem.htm file is shown as 'moz-extension://a02e52b1-e41d-4d28-844b-a8466a1dd67b/rem.htm' which gives an invalid error for the 'matches' key in the console.
What else can I use?
manifest.json:
{
"description": "Resource Manager for Path of Exile",
"manifest_version": 2,
"name": "Path of Exile Resource Manager",
"version": "1.0",
"content_scripts":
[
{
"matches": ["file:///rem.htm"],
"js": ["modify-page.js"]
}
],
"background":
{
"scripts": ["background.js"]
},
"browser_action":
{
"default_icon":
{
"16": "icons/rem_16.png",
"32": "icons/rem_32.png",
"64": "icons/rem_64.png"
}
},
"permissions":
[
"*://www.pathofexile.com/",
"webRequest",
"tabs",
"activeTab"
]
}
I figured it out. I didn't realize I had modify-page in a separate directory. I was then able to add '' to matches and it finally worked!

Chrome extension to work on all the sites and perform different action just for few sites

I'm trying to make an extension to work on all the site.And also for some site specific sites(Google and LinkedIn) I want some others action to be performed. I have managed to work it on Google search google.com/search and LinkedIn search pages linkedin.com/vsearch/ and perform action A and B respectively. But i'm stuggling to make it work on all others sites to perform action C.
{
"manifest_version": 2,
"name": "extname",
"description": "Welcome to my ext",
"icons": {
"48": "images/ext.png"
},
"version": "2.6",
"background": {
"scripts": [ "js/jquery-2.1.4.min.js", "js/background.js","js/select2.min.js","js/lodash.js","js/bootstrap.min.js","js/bootstrap-select.min.js" ]
},
"content_scripts": [{
"css": [ "css/select2.css" ,"css/bootstrap.min.css","css/bootstrap-select.min.css" ],
"js": [ "js/jquery-2.1.4.min.js", "js/extension_google_result.js" ,"js/select2.min.js" ,"js/bootstrap.min.js","js/bootstrap-select.min.js"],
"matches": [ "*://*.google.com/search*" ] //for Google Search page
},
{
"css": [ "css/extension_linkedin_search_page.css", "css/bootstrap.min.css","css/bootstrap-select.min.css" ],
"js": [ "js/jquery-2.1.4.min.js","js/select2.min.js" ,"js/bootstrap.min.js","js/bootstrap-select.min.js"],
"matches": [ "*://*.linkedin.com/vsearch/*"] //for Linkedin Search page
},
{
"css": [ "css/extension_linkedin_search_page.css" ,"css/bootstrap.min.css","css/bootstrap-select.min.css","css/select2.css" ],
"js": ["js/lodash.js", "js/jquery-2.1.4.min.js", "js/extension_linkedin_sales_navigator_search_page.js" ,"js/select2.min.js" ,"js/bootstrap.min.js","js/bootstrap-select.min.js", "js/akash.js"],
"matches": [ "<all_urls>"] //for all other sites
}
],
"content_security_policy": "script-src 'self' 'unsafe-eval' https://d37gvrvc0wt4s1.cloudfront.net https://*.pusher.com; object-src 'self'",
"browser_action": {
"default_icon": "images/found128.png",
"default_popup": "html/extension-login-popup.html",
"default_title": "Search with Found"
},
"permissions": ["cookies","tabs", "http://*/*", "https://*/*","contextMenus", "tabs", "storage", "\u003Call_urls>", "notifications", "webRequest" ]
}
But here the first two cases for google and linked in search works perfectly and i'm getting callbacks(Action A and B) on only those pages as expected.I'm also able to call action c on other sites.But actin c is also been calling on google and linkedIn search pages also on every new tab action which i don't want.How can i fix that? Any idea?
Take a look at Content Scripts, you will find you can use exclude_matches in your manifest.json to exclude pages that your content script would not be injected into.

Matching content script with last keyword match

I have a page in block.html in extension directory. I want match content script with the page name.
Check my manifest file below, there is content_script match:
{
"manifest_version": 2,
"name": "Cobra Security ATD",
"version": "1.0",
"description": "Cobra Security Advance Threat Defence.",
"icons": {
"48": "img/48-48.png",
"128": "img/128-128.png"
},
"browser_action": {
"default_icon": "img/16-16.png",
"default_title": "Cobra Security Advance Threat Defence",
"default_popup": "popup.html"
},
"background": {
"scripts": ["js/jquery-1.11.1.js","js/event.js"],
"persistent": false
},
"content_scripts": [
{
"matches": ["http://*/*block.html"],
"js": ["js/jquery-1.11.1.js","js/block.js"]
}
],
"permissions": [
"<all_urls>",
"tabs",
"webNavigation"
]
}
One does not inject content scripts into chrome-extension:// pages. You cannot indicate this schema in the manifest, and you can't indicate a host permission that would allow you to inject it.
Since it's an extension that you control, you don't need to inject scripts for anything!
If you want to put code in it, you.. put code in it, in a <script
src="..."> fashion.
If you want to inform the page of some event, you use Messaging.
If you want to pass some information when you open the page, you can
use this trick, sending a request back for data.

close pop-up - chrome extensions

Im trying to create a quite simple chrome extention, that close a pop-up after the pop-up is loaded, and by its title.
But for some reason the the title keeps returnen blank.
This is what i came up with.
{
"name": "ReportCloser",
"version": "0.1",
"permissions": [
"tabs","<all_urls>"
],
"browser_action": {
"default_icon": "icon.png"
},
"content_scripts": [
{
"matches": [
"http://*/*",
"https://*/*"
],
"run_at": "document_end" ,
"js": ["script.js"] // pay attention to this line
}
],
"manifest_version":2
}
script.js
var x = document.title;
var title = "customtitle";
if(x==title){
close();
}
else{ }
I tried to move run_at: document_end before the content script, but the problem i still there.
Any tips?

Chrome Extensions - How can i perform an action, when chrome.browserAction.onClicked has fired?

I want to make a browserAction extension, with an icon and a listener on it.
I have a manifest file, and a background script, the script is the following:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(null,{code:'some code here'});
});
The code works on the page, i tried it on a different way (popup and a button what fires the action). But if i try it with a browserAction onclick method, nothing happens:(
The manifest:
{
"name": "somename",
"version": "1.0",
"manifest_version": 2,
"description": "sometext",
"browser_action": {
"default_icon": "images/icon.png",
"default_title": "MyStyle"
},
"background": {
"scripts": ["js/code.js"]
},
"permissions": [
"tabs",
"https://www.examplesite.ex/*",
"http://www.examplesite.ex/*",
"http://*.ex/*"
]
}
Can anybody help me?:/
Since the original question has been solved in the comments, I'll answer the follow-up question:
"Next step to make it automatic, without any click".
This can be done easily using Content scripts. When you don't have to access global variables, the following code is sufficient. Otherwise, inject the script using the techniques as mentioned here:
js/code.js
document.title = "newtitle";
manifest.json
{
"name": "somename",
"version": "1.0",
"manifest_version": 2,
"description": "sometext",
"content_scripts": {
"js": ["js/code.js"],
"matches": [ "*://www.examplesite.ex/*", "http://*.ex/*" ]
},
"permissions": [ "*://www.examplesite.ex/*", "http://*.ex/*" ]
}