extension not loading on url change - javascript

My content.js is not running when I click on a link. The url changes (content.js runs when hitting F5). The content that gets updated is inside of a frame.
How do I trigger my content.js to run without press F5?
{
"name":"My name",
"description":"My description!",
"version":"1",
"manifest_version":2,
"browser_action": {
"default_icon": "icon.png"
},
"content_scripts": [
{
"matches": ["http://www.somePage.com/article*"],
"js": ["content.js"]
}]}

Could you try to set the "all_frames" to true in order that when a frame in a page is updated the content script will be injected again like in the following:
"content_scripts": [
{
"matches": [ "http://www.somePage.com/article*" ],
"all_frames": true,
"js": [ "content.js" ]
}

Related

document.body returning iframe body

I am developing a chrome extension that injects html to Linkedin. When I do $(document.body).append($("<div>")); from my Js file, it also appends for an Iframe component that's on the page.How ever when I type $(document.body) on the console it only returns main body.
How do I restrict it to only main body from my js file.?
My manifest file:
{
"manifest_version":2,
"name": "Hawk! Beta",
"description": "Hawk",
"version": "1.0",
"permissions": [
"activeTab",
"tabs",
"storage",
"https://ajax.googleapis.com/",
"https://*.ngrok.io/"
],
"icons": {
"16": "img/icon.png",
"48": "img/icon.png",
"128": "img/icon.png"
},
"browser_action": {
"default_icon": {
"19": "img/icon.png",
"38": "img/icon.png"
}
},
"background": {
"scripts": [
"js/lib/jquery.min.js",
"js/lib/asteroid/ddp.js",
"js/lib/asteroid/q/q.js",
"js/lib/asteroid/asteroid/dist/asteroid.chrome.js",
"js/lib/asteroid/asteroid/dist/plugins/facebook-login.js",
"js/lib/asteroid/asteroid/dist/plugins/google-login.js",
"js/lib/asteroid/asteroid/dist/plugins/github-login.js",
"js/lib/bootstrap.min.js",
"js/background.js"
]
},
"content_scripts": [
{
"matches": [
"https://www.linkedin.com/*"
],
"all_frames" : true,
"match_about_blank": true,
"js": [
"js/lib/jquery.min.js",
"js/lib/handlebars.js",
"js/content_scripts/getPagesSource.js"
],
"css":[
"css/custom.css"
]
}
],
"web_accessible_resources": [
"img/*",
"js/*",
"js/lib/*",
"css/custom.css",
"html/*"
]
}
The code I used to inject html
function injectHtml(){
console.log(document.body); //this returns 2 bodies
$(document.body).append($("<div>", {
"class": "myapp-container"
}));
}
I only want to inject into main body and not into iframe body.
As suggested by Patrick and wOxxOm , I edited the manifest file's content_scripts to look like below.
"content_scripts": [
{
"matches": [
"https://www.linkedin.com/*"
],
"all_frames" : false, //this had to be changed
"match_about_blank": true,
"js": [
"js/lib/jquery.min.js",
"js/lib/handlebars.js",
"js/content_scripts/getPagesSource.js"
],
"css":[
"css/custom.css"
]
}
]
It now works fine. Thanks guys.
Edit:
all_frames:
Controls whether the content script runs in all frames of the matching
page, or only the top frame.
For more we have,
this link to know more

Chrome extension Content Script not working

I built a Chrome Extension script that is supposed to run on Reddit.
My script:
console.log("hello world");
My manifest.json
{
"manifest_version": 2,
"name": "Name",
"description": "Desc",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png"
},
"content_scripts": [
{
"matches": [
"*://reddit.com/*"
],
"js": [
"contentscript.js"
],
"run_at": "document_end"
}
],
"permissions": [
"tabs", "*://reddit.com/*", "activeTab"
]
}
The script doesn't show up in the "Content Script" section in the chrome dev tools. Does anyone have an idea why my extension is not running?
"*://reddit.com/*" doesn't match a valid url, you should use "*://*.reddit.com/*"

chrome.tabs.executeScript event don't work anymore?

I have this code below that until last week was working, but now when I will go execute again, this not works.
Based in this question and tested by me, using chrome.tabs.onUpdated.addListener event, works fine, but I have some strings in msgBox.js file, that cause errors relative to encoding (UTF-8/ANSI) and because this, all script inside of msgBox.js file is necessary execute using chrome.tabs.executeScript.
msgBox.js
function msg()
{
alert("hello!");
}
msg();
event.js
chrome.webRequest.onCompleted.addListener(
function onWindowLoad() {
chrome.tabs.executeScript(null, {
file: "msgBox.js"
}, function() {
});
}
,
{
urls: ["<all_urls>"],
types: ["main_frame"]
},
["responseHeaders"]
);
manifest.json
{
"background": {
// "page": "popup.html"
"scripts": ["event.js"]
},
"description": "Media Player for Flash",
"manifest_version": 2,
"name": "Media Player",
"icons": {
"128" : "picture/flash128.png" ,
"48" : "picture/flash48.png"
},
"web_accessible_resources": [
"event.js"
],
"content_scripts": [
{
"matches": ["<all_urls>", "*://*/*", "http://*/*", "https://*/*"],
"js": ["event.js"],
"run_at": "document_end",
"all_frames": true
}
],
"permissions": [ "tabs", "background", "activeTab", "<all_urls>", "webNavigation", "webRequest", "http://*/*", "https://*/*", "*://*/*" ],
"version": "1.0"
}
What can is wrong?
Any help will welcome.
According to https://developer.chrome.com/extensions/tabs#method-executeScript
null is not a valid option for object in "executeScript", Try using "string" in it's place. It worked for me at least when trying to reproduce and fix
Edit: although that does not explain why it worked before but stopped

Content script on chrome error page

How can I run my extension content script on Google Chrome error page?
For example on the "This webpage is not available" error page? Here is my manifest.json config:
{
"manifest_version": 2,
"name": "injectbox",
"version": "1.1",
"background": {
"scripts": ["jquery-1.11.1.min.js", "bg.js" ]
},
"content_scripts": [
{
"js": [ "jquery-1.11.1.min.js" ],
"matches": [ "<all_urls>" ],
"match_about_blank": true,
"run_at": "document_end"
},
{
"js": [ "content.js" ],
"css":["styles.css"],
"matches": [ "<all_urls>" ],
"match_about_blank": true,
"run_at": "document_end"
}
],
"permissions": [
"tabs", "http://*/*", "https://*/*", "file://*/*", "ftp://*/*",
"webRequest",
"storage"
]
}
I don't think it's possible, most probably this is an internal chrome:// page, which are excluded from page matches.
An alternative solution would be to listen to error events in webRequest/webNavigation APIs and replace the error page with your own.

Chrome Extension - Simple Content Script for running js on any page

How can I write a simple Chrome Extension content script that will execute JavaScript (for example alert("hello");) on every page load?
So when I navigate to a page or reload a page, the JavaScript should run.
This is my manifest.json file so far:
{
"name": "Highlight some phrases",
"description": "Hightlight some pre defined text from websql database after page loads",
"version": "0.1",
"permissions": [
"tabs","<all_urls>"
],
"browser_action": {
"default_icon": "icon.png",
"default_popup": "popup.html"
},
"content_scripts": [
{
"matches": [
"http://*/*",
"https://*/*"
],
"js": ["content.js"]
}
],
"background": {
"page": "background.html"
},
"manifest_version": 2
}
If all you need is to alert hello on every page load or reload, below is a simple demo:
Manifest.json:
{
"name": "Highlight some phrases",
"description": "Hightlight some pre defined text after page loads",
"version": "0.1",
"permissions": [
"tabs","<all_urls>"
],
"browser_action": {
"default_icon": "icon.png"
},
"content_scripts": [
{
"matches": [
"http://*/*",
"https://*/*"
],
"js": ["content.js"],
"run_at": "document_end" // Pay attention to this line
}
],
"manifest_version": 2
}
and content.js:
// alert("hello");
document.body.style.background = 'yellow';
Yes, that's enough.
And of course, don't forget to add an icon named icon.png at the same directory with these two files, then test it in Google Chrome.

Categories

Resources