I just started learning JS a few weeks ago and am trying to build a Chrome Extension. I've build a manifest.json, popup.html, popup.js, and content.js file.
When I try running the background page console on Chrome to see if the popup.js is working I keep getting the error:
Uncaught TypeError: Cannot read property 'addEventListener' of null
at popup.js:8
I've tried moving the script in the html to the bottom of the body, but it still ends in an error and have tried implementing:
document.addEventListener('DOMContentLoaded', function () {
// My code here.. ( Your code here )
});
in my popup.js and the windows.onload method, but still haven't been able to get rid of the error.
If someone could please point out any errors in my code and tell me what is wrong I would really appreciate it. Thanks
POPUP HTML
<!DOCTYPE html>
<html>
<head>
<title>Like IG Links</title>
</head>
<body>
<h3><center>Paste Links Below</center></h3>
<hr>
<textarea rows="10" cols="60" id="urls"></textarea>
<br>
<textarea rows="1" cols="5" id="counter" disabled></textarea>
<br>
<br>
<button type="submit" align="center" style="width:60px;"
id="start">Start</button>
<br>
<br>
<button type="submit" align="right" style="width:60px;"
id="stop">Stop</button>
<script type="text/javascript" src="popup.js"></script>
</body>
</html>
CONTENT JS
console.log('CHROME');
POPUP JS
console.log('background running');
function loadUrls() {
console.log("123");
}
var startbutton = document.getElementById("start");
startbutton.addEventListener('click', loadUrls);
MANIFEST JSON
{
"manifest_version": 2,
"name": "Like IG Links",
"version": "1.0",
"description": "Like IG Links",
"content_scripts": [
{
"matches": [
"<all_urls>"
],
"js": ["content.js"]
}
],
"background": {
"scripts": ["popup.js"]
},
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"permissions": ["tabs", "storage", "activeTab"]
}
Remove the background script portion of the manifest. After updating the extension, the errors should disappear. In an extension I am working on, I had jquery.js and popup.js in the HTML and in the manifest. The background script tag for the manifest is for logic that tracks browser events and responds to them persistently, not for the js behind the extension HTML.
Related
I've been looking everywhere for an answer to what seems like a simple problem but can't find anything that works. Making a chrome extension and all I want it to do right now is display how many DOM items are listed on the current page.
masnifest.json:
{
"manifest_version": 3,
"name": "Tag Manager",
"description": "",
"version": "1.0",
"permissions": ["scripting",
"tabs",
"notifications"],
"host_permissions": ["https://*/*"],
"action": {
"default_popup": "popup.html"
}
}
popup.html:
<html>
<head>
<title></title>
</head>
<body>
<script src='popup.js'></script>
</body>
</html>
popup.js:
function hello() {
var name = document.getElementsByClassName('wd-tag-row').length;
alert("Tags " +name);
}
hello();
When I run the popup.js script from the developer console on the page I'm testing it works. An alert pops up that says "Tags: 9" but whenever I run the extension it says "Tags: 0" and I can't figure out why.
Any help would be appreciated, thank you!
Tried moving around where the script is called on, and tried making it run when the page is finished loading.
I am writing a chrome extension and am trying to bind an event listener to an HTML button via an external JS file.
The event listener works, however Chrome throws the following error:
This is manifest.json, background.js, and popup.html file in that order:
{
"name": "DRIP",
"version": "1.0",
"manifest_version": 2,
"description": "DRIP is an automation tool for purchasing limted items",
"permissions": ["storage", "tabs"],
"browser_action": {
"default_popup": "popup.html"
},
"background": {
"scripts": ["background.js"]
}
}
function myAlert(){
alert('hello world')
}
document.addEventListener('DOMContentLoaded', function () {
document.getElementById('alertButton').addEventListener('click', myAlert);
});
__
<html>
<head>
<title>Test</title>
</head>
<body>
<form name='testForm'>
<input type='button' id='alertButton' value='click me'>
</form>
<script language='javascript' src='background.js' defer></script>
</body>
I'm sorry if my formatting isn't great. Input is greatly appreciated!
Remove
"background": {
"scripts": ["background.js"]
}
That's for running tasks in the background and loads a background page. It is running on a blank page _generated_background_page.html (that it generated for you). Note that you can set the background page's html if you needed to.
Popup is a separate background page with the html set to the default_popup value. That is the page you get when you click the icon and get a popup.
Official docs for reference: https://developer.chrome.com/extensions/background_pages
I'm trying to write a Chrome extension that highlights certain words that were entered in extension's textfield
For example: I enter "web" in extension's area, press the button. On active tab all "web" words are should be highlighted.
I've found a nice web-pagethat has all the functions I need.
In my code, when I try to use them in my Extension, nothing works.
manifest file
{
"manifest_version": 2,
"name": "Word search & highlight",
"version": "1.0",
"icons": {
"16": "16x16.png",
"32": "32x32.png",
"48": "48x48.png",
"128": "128x128.png"
},
"content_scripts": [
{
"matches": [ "*://*/*" ],
"js": [ "popup.js"],
"run_at": "document_end"
}
],
"permissions": [
"tabs", "activeTab"
],
"browser_action": {
"default_title": "Start searching",
"default_icon": "48x48.png",
"default_popup": "popup.html"
}
}
popup.html
<!DOCTYPE html>
<html>
<head>
<script src="popup.js"></script>
<title></title>
</head>
<body>
<input type="text" id="searchtextfield"><br>
<input id="btn_save" value="Save" type="button">
<input id="btn_search" value="Search" type="button"><br>
</body>
</html>
And here's popup.js file: it's too long, so I made a pastebin document:
http://pastebin.com/g5ZenE48
I can't get innerHTML of webpage from these functions and I don't really understand how to make this all work.
sean-adams is correct, your browser action (popup.html) cannot communicate directly with your content script (popup.js). You can think of a content script as being an addition to whatever page the user is visiting, whereas the browser action is directly integrated as part of your chrome extension.
So, you will indeed need to use message passing to communicate. It's simple though. First, I recommend renaming your browser action popup to something like browser_popup.html. I'll use a stripped down example for brevity:
//browser_popup.html
<input type="button" id="my_button" value="Click Me">
<script src='browser_popup.js'></script>
You'll also want another file browser_popup.js for handling events.
//browser_popup.js
var button = document.querySelector('#my_button');
button.addEventListener('click', function() {
// Send message to active tab
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.sendMessage(tabs[0].id, 'button_clicked');
});
});
In your content script (let's call it content.js) you need to listen for the message:
//content.js
chrome.runtime.onMessage.addListener(function(message) {
if (message == 'button_clicked') {
// code to modify page...
}
});
iam trying to build an chrome extension that do actions on site,
the site have his API , for example if you click on edit link in site, in backgroung its send an API command like this App.Cases.edit(Casenumber)
when iam calling this line via chrome extension send script API i get an error, the error said that its anonymous function.
So what i did is i found the path that lead to the element that trigger the click.
as you can see in code,
and now i get an Uncaught Error: NotFoundError: DOM Exception 8
Any one have an idea how to handle this issue? or maybe even more simple how to get acess to the site API.
here is the code , this is the JS file
$(document).ready(function()
{
$('#btn1').click(function(){
chrome.tabs.executeScript(null,{file:"jquery-1.10.1.min.js"},function() {
chrome.tabs.executeScript(null,{code:" $('#row-37416').children().eq(3).children().eq(1).click(); "});
});
});
this is the html file:
<html>
<head>
<link href="style.css" rel="stylesheet" type="text/css" />
<script src='jquery-1.10.1.min.js'></script>
<script src='alert.js'></script>
<script src='contentscript.js'></script>
</head>
<body>
<h1 id = "title">Extensions</h1>
<input type = "button" value ="Find And Replace" id="btn1" />
<div id="content">
</div>
</body>
</html>
this is the manifest file:
{
"name": "TEST",
"version": "1.0",
"manifest_version": 2,
"description": "jonathan",
"content_scripts": [
{
"matches": [ "http://*/*", "https://*/*"],
"js": ["jquery-1.10.1.min.js", "alert.js","contentscript.js"]
}
],
"permissions": [
"tabs", "http://*/*" , "https://*/*"
],
"browser_action":{
"default_icon": "icon.png",
"default_popup": "popup.html"
}
}
Use "trigger()" to trigger the click instead of ".click()". See here:
http://api.jquery.com/trigger/
I am trying to make basically an element highlighter chrome extension.
Workflow:
- click on browser icon
- click on the page
- hightlight the element clicked
I am having troubles in running content scripts upon browser action using manifest_version:2
When I inspect the popup that appears it says:
Refused to execute inline script because it violates the following
Content Security Policy directive: "script-src 'self'
chrome-extension-resource:" (popup.html:5).
Which is where the inline script in popup.html is and the script does not work
I have:
manifest.json:
{
"browser_action": {
"default_icon": "images/icon.gif",
"default_popup": "popup.html"
},
"manifest_version": 2,
"description": "MEH!",
"name": "My First Extension",
"permissions": [
"tabs", "http://*/*", "https://*/*"
],
"version": "0.1"
}
popup.html:
<html>
<head>
</head>
<body>
<script>
chrome.tabs.executeScript(null,{
code:"document.body.style.backgroundColor='red'"
});
</script>
<div id='msg' style="width:300px">...</div>
</body>
</html>
Any help would be very much appreciated
Turns out I could not read the error properly until I saw it in here
Apparently manifest v2 does not allow you to have inline scripts, so you just need to
src="path_to_the_file.js"
In extension to #tak3r's answer and #Doug's comment:
Inline scripts need to be changed to external scripts.
Move:
<script>
chrome.tabs.executeScript(null,{
code:"document.body.style.backgroundColor='red'"
});
</script>
To a new file called main.js and remove the <script></script> tags
Include the following in the <head></head> of your HTML
<script type="text/javascript" src="main.js"></script>