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"]
}
Related
So I'm in the midst of creating my first Chrome Extension (Trying)
I feel like I'm close... But I genuinely don't know what to google to get the answers I need. So I'm sorry if this is a silly question.
Essentially what I'm trying to do is on click of extension - Append HTML & CSS to body and run a jQuery function. But from the looks of it, I need to call in jQuery in the manifest? Which I think I've done and it's still not working.
My Code:
manifest.json
{
"name": "Title",
"description": "Description",
"version": "1.0",
"browser_action": {
"default_title": "Hover Title",
"default_icon": "icon.png"
},
"content_scripts": [ {
"js": [ "jquery-1.7.2.min.js", "background.js" ],
"matches": [ "http://*/*", "https://*/*"]
}],
"manifest_version": 2
}
background.js
chrome.browserAction.onClicked.addListener(function(tab) {
(function ($) {
$('body').append("Hello");
alert("Hello");
console.log("Hello");
}(jQuery));
});
Any insight into where I'm going wrong would be massively helpful!
Thank you!!
Chrome extension architecture is simple but it doesn't mean one can write code without studying it.
There are two methods of injecting content scripts:
Unconditionally on all specified urls, in which case "content_script" key is used in the manifest and the content scripts communicate with the background page via runtime.sendMessage.
Only when some event occurs like e.g. the user clicks our toolbar icon, in which case we only need the permission to access the active tab.
So in the given case we'll attach the icon click handler and inject the code afterwards:
manifest.json:
{
"name": "Title",
"description": "Description",
"version": "1.0",
"browser_action": {
"default_title": "Icon Title",
"default_icon": "icon.png"
},
"background": {
"scripts": ["background.js"]
},
"permissions": ["activeTab"],
"manifest_version": 2
}
background.js (this is an event page because we didn't use "persistent": true in the manifest, so be advised that the [global] variables will be lost after a few seconds of inactivity; instead you should use chrome.storage API or HTML5 localStorage/sessionStorage/and so on):
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript({file: "jquery-1.7.2.min.js"}, function(result) {
chrome.tabs.executeScript({file: "content.js"}, function(result) {
});
});
});
content.js (the code runs in a sandbox so there's no need to hide global variables using IIFE)
$('body').append("Hello");
alert("Hello");
console.log("Hello");
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.
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");
I created an extension for Google Chrome with this background script background.js:
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(tab.id, {code:"document.body.style.background='red !important';"}); // doesn't work
chrome.tabs.executeScript(tab.id, {code:"alert('hello');"}); // runs alert
});
I want to run document.body.style.background='red !important'; in the context of the web page.
How can I do it?
manifest.json:
{
"name": "Test",
"version": "1.0",
"manifest_version": 2,
"description": "Test",
"browser_action": { "default_icon": "icon.png" },
"background": { "scripts": ["background.js"] },
"permissions": ["tabs", "*://*/*"]
}
Plain and straight. Add https://*/* to permissions.
background actually expects everything, if you want to change the color use backgroundColor and need not give !important as you are injecting after everything is loaded.
So the below change may work. Please try that.
chrome.tabs.executeScript(tab.id, {code:"document.body.style.backgroundColor='red';"});
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"]
}