Trying to make a Chrome extension to fade a webpage - google-chrome

I'm trying to make my first Chrome extension. Basically, the idea is to make a coloured filter that covers every page. (I have a monitor that won't get any darker, so I want to put a translucent black filter over every site I visit.)
From my understanding, I can run javascript by using a "Content script," with the script executing on every page. This is my manifest.json:
{
"name": "My First Extension",
"version": "1.0",
"description": "The first extension that I made.",
"browser_action": {
"default_icon": "icon.png",
"popup": "popup.html"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"css": ["mystyles.css"],
"js": ["contentscript.js"]
}
]
}
I want to do something like this: http://jsfiddle.net/GM2Z6/14/
Great, so I've gotten that far, by making mystyles.css like this:
#cover {
height:100%;
width:100%;
background-color:#000;
display:none;
position:absolute;
top:0;
left:0;
z-index:99999999;
}​
And I've got my contentscript.js looking like this:
$(function(){
$('#cover').fadeTo("fast",0.5);
});​
So now how do I make my #cover div overlay on top of the page?

A couple of things...As rvmook said, you didnt include jquery and you also never create the div in the page.
Following is some code that works. Notice in the css I changed a couple of things...Position is now "fixed", otherwise your div scrolls with the page and pointer-events:none; allows you to click through the div to the elements below it. Also now rely on the content scripts "run_at" option to decide when to run the script and not jquery, because I dont know what document_end is in jquery.
manifest.json
{
"name": "My First Extension",
"version": "1.0",
"description": "The first extension that I made.",
"browser_action": {
"default_icon": "icon.png",
"popup": "popup.html"
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"css": ["mystyles.css"],
"js": ["jquery-1.7.1.min.js","contentscript.js"] ,
"run_at" : "document_end"
}
]
}
mystyles.css
#cover {
height:100%;
width:100%;
background-color:#000;
display:none;
position:fixed;
top:0;
left:0;
z-index:99999999;
pointer-events:none;
}​
contentscript.js
(function(){
$('<div/>', {
id: 'cover'
}).appendTo(document.documentElement);
$('#cover').fadeTo("fast",0.5);
})();

It seems that you haven't inlcuded jQuery in your content_scripts. Even if the website you're injecting your code in has jQuery itself, you can't access it with your Extension, so that's why it should also be included in your content_scripts.
"content_scripts": [
{
"matches": ["<all_urls>"],
"css": ["mystyles.css"],
"js": ["contentscript.js", "your/path/to/jquery-1.7.1.min.js"]
}

Related

Chrome Extension: Show Images Only (No Text) on Page

Is it possible to write a Chrome extension that shows images ONLY on a page (hides all text), when the plugin is enabled?
There are plugins available that hide all images. I need the opposite: a page with just the images, and all text hidden.
Ideally, this behavior should also be restricted to some location on the page (e.g., inside a particular DIV), but that's not a requirement.
If you want to hide all html tags but img then, yes, it's possible and is very easy to do. You can do this using a content style that overwrites the CSS of the target page and hide everything except images tags.
Here is how you can do it:
manifest.json:
{
"name": "Test extension",
"version": "0.0.1",
"manifest_version": 2,
"description": "Some description",
"icons": {
"16": "images/icon-16.png",
"128": "images/icon-128.png"
},
"default_locale": "en",
"content_scripts": [
{
"matches": [
"http://*/*",
"https://*/*"
],
"css" : [
"styles/contentstyle.css"
],
"run_at": "document_end"
}
]
}
scripts/contentstyle.css
body {
visibility: hidden;
}
* img {
visibility: visible;
}
Note that this CRX won't show images set as a background-image through CSS, only the img tags.

To execute a function on the button click in chrome extension

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");

Background page function not triggered in google chrome:

I am using the following code to access the background page function in google chrome
popup.html
function sendRequest(ea,eb)
{
console.log("Inside");
chrome.extension.sendRequest({ea:ea,eb:eb},
function(response)
{
alert(response.farewell);
});
}
background.html
<html>
<body>
<script>
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
sendResponse({farewell: "goodbye"});
})
</html>
</body>
</script>
manifest.json
{
"name": "My First Extension",
"version": "1.0",
"manifest_version": 2,
"background": {
"page": "background.html"
},
"content_scripts": [
{
"matches": ["http://*/"],
"js": ["popup.js"]
}
],
"description": "The first extension that I made.",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "mine.html"
},
"permissions": [
"tabs","http://*/","background"
],
"web_accessible_resources": ["loading.html","bu.png"]
}
However it does not print the alert. Can anyone tell me what i am doing wrong here?
Your HTML for background.html is extremely malformed and should be fixed;
<html>
<body>
<script>
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
sendResponse({farewell: "goodbye"});
})
</script>
</body>
</html>
Tags should be closed in the reverse order of them being open to maintain a correct hierarchy. Since you had not done so, the <script> element was malformed and contained invalid syntax </html></body> so would not be executed correctly.
Since you're using version 2 of the manifest you may want to consider abstracting the contents of this script element (ignoring all HTML) to its own file (e.g. background.js) and change your manifest to the following;
{
"name": "My First Extension",
"version": "1.0",
"manifest_version": 2,
"minimum_chrome_version": "18",
"background": {
"scripts": ["background.js"]
},
"content_scripts": [
{
"matches": ["http://*/"],
"js": ["popup.js"]
}
],
"description": "The first extension that I made.",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "mine.html"
},
"permissions": [
"tabs","http://*/","background"
],
"web_accessible_resources": ["loading.html","bu.png"]
}
Notice that the background property now contains an array of strings representing JavaScript files to be loaded (in the order specified) in to a dynamically generated background page.
I've also set the minimum_chrome_version property to 18 as manifest version 2 should only be used when targeting this version of Chrome and above.
Developers should now only really need to use background pages instead of scripts when they need to support older versions of Chrome.
Edit
It just clicked that you're attempting to execute embedded JavaScript within your background page. Manifest version 2 introduces Content Security Policies which prohibit the execution of inline (e.g. onclick="showDialog();" and href="javascript:void(0);") and embedded JavaScript. This is why your background.html won't work and why background.js will. You will also want to ensure your popup.html doesn't contain any embedded JavaScript. The best workaround (and generally best practice anyway) is to abstract all JavaScript into its own file (e.g. popup.js) which is referenced by the HTML file. For example;
<script src="/popup.js"></script>

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/*" ]
}

DIV Drag On Webpage (Chrome Extension)

What I'm working on is I have a div with a black background and I wanna be able to click, and drag it. I'm using JQuery UI, and well I can't seem to figure out how to do it.
When the icon for the div is clicked it toggles to show/hide, and I wanna be able to click, and drag it over a web page. Later on this will be helpful for calculators, stopwatches, etc: but I can't seem to figure out how to have the div show/hide nor how to be able to drag it.
Any help would be much appreciated.
manifest:
{
"name": "DIV Drag Test",
"version": "0.0.1",
"description": "DIV Drag Test",
"background_page": "background.html",
"permissions": ["http://*/*", "tabs"],
"icons": {
"48": "logo.png"
},
"browser_action": {
"default_icon": "logo.png",
"default_title": "DIV Drag Test"
},
"content_scripts": [
{
"matches": ["http://*/*"],
"css": ["style.css"],
"js": ["js/jquery.js", "js/jquery-ui.js"]
}
]
}
css:
div#test8935 {
cursor:pointer;
width:320px;
height:240px;
background-color:#000;}
background.html
<script>
$(document).ready(function() {
$('div#test8935').draggable();
chrome.browserAction.onClicked.addListener(function(tab) {
$('div#test8935').toggle(350);
});
});
It has been a little while since I have done Chrome extension development, but these are my initial thoughts based on what I remember.
It looks like you are trying to manipulate a div on a website through background.html. Are you sure you don't want to be targeting a content script instead? Background has no interface and will never interact directly with a page.
So what I think you want to do is move your script from background.html into a new js file and include it with your contentscript js params.
"content_scripts": [
{
"matches": ["http://*/*"],
"css": ["style.css"],
"js": ["js/jquery.js", "js/jquery-ui.js", "contentscript.js"]
}