Running jquery script triggered by context menu in chrome extension - google-chrome-extension

I am new to chrome extension development. The sample code I have is not running properly.
Requirement: Executing any jquery script(say $("body").hide();) on click of context menu button.
From the code, only jquery part is not working.
I have following files:
manifest.json
{
"manifest_version": 2,
"name": "jQuery DOM",
"version": "1",
"permissions": [
"contextMenus","tabs","activeTab"
],
"background": {
"scripts": ["jquery.min.js","sample.js"]
},
"description": "Manipulate the DOM when the page is done loading",
"browser_action": {
"name": "Manipulate DOM",
"icons": ["icon.png"],
"default_icon": "icon.png"
},
"content_scripts": [ {
"js": [ "jquery.min.js", "background.js" ],
"matches": [ "http://*/*", "https://*/*"],
"run_at": "document_end"
}]
}
background.js
$("body").append('Test');
I have icon.png in folder, and it gets loaded well.
jquery.min.js in same folder
sample.js
alert("Extension loaded");
function genericOnClick(info, tab) {
alert("Tab "+tab.id);
chrome.tabs.executeScript(tab.id, {
file: "jquery.min.js",
allFrames: true
},function(){
alert("callback");
$("body").hide();
});
alert("Completed");
$("body").hide();
}
var contexts = ["page"];
for (var i = 0; i < contexts.length; i++) {
var context = contexts[i];
var title = "Test Page menu item";
var id = chrome.contextMenus.create({"title": title, "contexts":[context],
"onclick": genericOnClick});
console.log("'" + context + "' item:" + id);
}
background.js works!
All the alerts work file, but .hide function from genericOnClick doesn't work.
Even if I move the code from sample.js to backgroud.js, it won't work.
Can you please tell me where did i go wrong ?

As I mentioned, a background script isn't allowed to interact with the DOM, (while a content script isn't allowed to use chrome.contextMenus). You need to combine both, and use message passing to tell the content script when to execute. Some other adjustments I made:
I renamed background.js and content.js so that their names now
reflect what they do, and made background.js into an event page.
I removed the browser action (the
extension would need browserAction.html or background.js would
need chrome.browserAction.onClicked.addListener to do anything
other than show the icon).
Programmatically injecting jquery means that always loading it as a
content script is unnecessary (although skipping programmatic injection allows you to omit the last three permissions).
background.js doesn't need jquery
anymore, so it isn't loaded there either.
The default executeScript
tab is the active tab, so we don't need it's id.
Here's the finished product:
manifest.json
{
"manifest_version": 2,
"name": "jQuery DOM",
"version": "1",
"permissions": [
"contextMenus", "activeTab", "tabs", "http://*/", "https://*/"
],
"background": {
"scripts": [ "background.js" ],
"persistent": false
},
"content_scripts": [ {
"js": [ "content.js" ],
"matches": [ "<all_urls>" ]
}],
"description": "Manipulate the DOM when the page is done loading"
}
background.js
function genericOnClick(info, tab) {
chrome.tabs.executeScript(null,
{"file": "jquery.min.js"},
function() {
chrome.tabs.sendMessage(tab.id,{"message":"hide"});
});
}
chrome.contextMenus.create({"title": "Test Page menu item",
"contexts":["page"],
"id":"contextId"});
chrome.contextMenus.onClicked.addListener(genericOnClick);
content.js
chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
if (msg.message == 'hide') {
$("body").hide();
}
sendResponse();
});
Your content.js is probably much larger than it is here (+1 for the SSCCE). But if it is small, another option would be to omit the content script entirely, and replace the sendMessage with chrome.tabs.executeScript(null,{code:"$('body').hide();"});.

Related

browser extension: dynamically disable content script

I have a browser extension with a content script that runs on all urls.
{
"name": "foobar",
"version": "0.1.0",
"permissions": [],
"manifest_version": 2,
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["content.js"]
}
]
}
I would now like to allow the user the disable the content script on certain sites, e.g., by having her click on a button in the popup. How would I go about this?
From the background script (with "activeTab" and "https://*/" permissions), one can inject any script using chrome.tabs.executeScript:
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
const inject = /.*:\/\/.*stackoverflow.com\/.*/.test(request.url);
if (inject) {
chrome.tabs.executeScript({
file: 'foobar.js'
});
}
// Send an empty response to avoid warning
sendResponse({});
});

Chrome Extension message listener not working

I am trying to send a message from my chrome extension popup window to the content script of the page, but the listener is not picking up the sent messages.
popup.js
chrome.runtime.sendMessage({ greeting: "hello" })
main.js (content script, part of the DOM)
The Listener is not picking up the sent messages:
chrome.runtime.onMessage.addListener(function (request) {
console.log("message received")
if (request.greeting == "hello") {
console.log("hello message")
}
})
manifest.json
{
"manifest_version": 3,
"name": "Go Go Ninja Computers",
"version": "0.54",
"description": "This extension is trying to send a message.",
"background": {
"scripts": [
"background.js"
],
"persistent": false
},
"permissions": [
"storage"
],
"content_scripts":
[
{
"matches": ["*://*/*"],
"js": ["main.js"],
"css" : ["style.css"],
"run_at": "document_end"
}
],
"browser_action": {
"default_title": "Ninja Pirate",
"default_popup": "popup.html"
}
}
I am just following these basic instructions, so I'm quite baffled why this isn't working? I'm also not sure if 'background.js' needs to be included, but without it, I cannot use console.log in popup.js.

tabs.executeScript: Cannot access contents of url

Well, this code was working and now, not anymore, WHY? I'm just trying to inject code via content script. (base code)
manifest.json
{
"name": "Test",
"permissions": [
"activeTab"
],
"background": {
"scripts": [
"background.js"
],
"persistent": false
},
"content_scripts": [{
"matches": ["http://*/*", "https://*/*"],
"js": [
"bower_components/jquery/dist/jquery.min.js",
]
}],
"browser_action": {
"default_icon": {
"19": "icon_19.png"
}
},
"manifest_version": 2
}
background.js
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(null, {
file: "content_script.js"
});
});
I can suppose it's a permission bug. But, what should I add for this work ?
You have to add to your permissions the url of the page wich you want to inject your code.
If I well understand, you want to inject the code in the active tab, so you can use the activeTab permission that temporally give permission to your extension for the curent tab when the user invokes your extension (by clicking the browser action for example). You can read more about it here.

Jquery not running completely in chrome extension

I have written a chrome extension
The manifest file is below
{
"name": "MWE",
"version": "2.0",
"permissions": [
"activeTab"
],
"content_scripts": [
{
"matches": ["https://*/*"],
"js": ["jquery.js", "content_script.js"]
} ],
"browser_action": {
"default_icon": "icon.png",
"default_title": "Make this widget bigger"
},
"manifest_version": 2
}
The jquery file that i am using is given below
$(".dijitAccordionTitle.dijitAccordionTitle-selected").parent().siblings().css("display","none")
$('#containerDiv').show().parentsUntil('body').andSelf().siblings().hide();
$("#dijit_layout_AccordionContainer_0").height($(document).height())
I want the jquery to execute after page is loaded successfully.The first line is not executing while it runs successfully in developer tools console.
What could be the issue?
Try wrapping the code in:
document.addEventListener('DOMContentLoaded', function () {
// Your code here
});

chrome content script onclick event

I'm trying to write a chrome extension and cannot seem to understand how to implement the following scenario:
user is on page X
user clicks on the extension's button
something happens (specifically, user is redirected to some url)
here's the manifest.json:
{
"name": "My First Extension",
"version": "1.0",
"description": "The first extension that I made.",
"browser_action": {
"default_icon": "icon.png",
"default_title": "my title"
},
"content_scripts": [
{
"matches": ["http://*/*", "https://*/*"],
"js": ["myscript.js"]
}
],
"permissions": [
"tabs", "https://*/*"
]
}
and here's myscript.js:
alert('entered myscript.js..');
function doMagic()
{
alert('extension button clicked!!');
}
chrome.extension.onClicked.addListener(doMagic);
i know im missing something really obvious, but cant seem to figure it out from the docs, other sites, etc.!!
Don't use a content_script, you really only need those if have to have access to the HTML of the tab.
Use a background_page for the onClicked listener and chrome.tabs.update to redirect the page.
function doMagic(tab) {
chrome.tabs.update(tab.id, { url: 'http://www.google.com' });
}
chrome.browserAction.onClicked.addListener(doMagic);

Resources