Reload content script on url change - google-chrome

I have a very simple chrome extension which basically add some text to specific pages, is working fine, but only on first loading and when i refresh the page.
The page have originally some real time filtering which update the results and the url, but because don't refresh the page, my script doesn't work.
I try to fix it using the background feature but maybe there are some simple thing i can do to force execute the script when the is some change on the page.
This is the current content of the manifest.json
{
"manifest_version": 2,
"name": "Extension name",
"version": "0.1.0",
"description": "Extension description",
"content_scripts": [
{
"js": ["contentList.js"],
"matches": ["https://www.test.com/*"]
}
]
}

Related

Chrome extension can not access custom window properties

Im trying to write a Chrome extension that has a dev tools panel. This extension needs to call functions defined on a property on window in a webpage that I also have made. In other words, the extension is only for my own web page and I control both. Example:
// This script is added in my own webpage application when it loads
window.myTest = () => { /* do something */ }
I want to be able to call the function window.myTest from my Chrome extension. I need to make similar functionality like https://github.com/zalmoxisus/redux-devtools-extension.
It seems that I need to do this by inject script/code from my backend page. I have all working, extension with backend page that gets invoked and I can see that the code that I inject gets called in the page context (testing by console.log gets written to the console output of the page).
Here is my code:
manifest.json
{
"manifest_version": 2,
"name": "MyTest",
"description": "MyTest",
"version": "0.0.1",
"minimum_chrome_version": "10.0",
"devtools_page": "devtools.html",
"background": {
"scripts": ["background.js"]
},
"content_scripts": [{
"matches": ["<all_urls>"],
"js": ["testscript.js"]
}],
"permissions": [
"tabs",
"<all_urls>"
]
}
testscript.js
window.myTest(); // myTest is undefined in the context of the injected script
background.js
// empty, but here to be able to open background page
I also have a pannel that sends a message to the background page when a button is clicked. I know that the panel and sending the message also work. But window.myTest is still undefined.
Edit
Removed the injection from background.js, because I did not use it and have same issue as described.
Finally, I got the specs on this. Mozilla and Chrome follow the same specs for extensions.
Content scripts get a "clean" view of the DOM. This means:
Content scripts cannot see JavaScript variables defined by page
scripts.
If a page script redefines a built-in DOM property, the
content script sees the original version of the property, not the
redefined version.
https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Content_scripts

Chrome Extension installation "Invalid manifest"

I have a Chrome extension that uploads to the Chrome store just fine. My current manifest file is posted below. It is saved in ANSI format as suggested in previous posts with this issue. This is the third variant and they all produce the same error message when I try to install: "Invalid manifest". No real information. I have tried waiting 24 hours for it to propagate properly as suggested in previous posts, but to no effect.
Has anyone encountered this issue before?
The extension is here: https://chrome.google.com/webstore/detail/bamboo-dialog-fixer/oelecpkhobhmbbdmehaoonkbkdodhdio?hl=en-US
{
"manifest_version": 2,
"name": "Bamboo dialog fixer",
"version": "1.3",
"description": "This extension makes bamboo popup dialogs such a the performance dialogs fit the width of the screen.",
"icons": {"128": "BambooHR_FullSize.png"},
"content_scripts":
{
"css": "styles.css",
"matches": "https://*.bamboohr.co.uk/*"
}
}
Thanks wOxxOm. I must have screwed up, even though I thought I had it right on a previous occasion. I rewrote the file as below and uploaded and it works now.
{
"manifest_version": 2,
"name": "Bamboo dialog fixer",
"version": "1.4",
"description": "This extension makes bamboo popup dialogs such a the performance dialogs fit the width of the screen.",
"icons": {"128": "BambooHR_FullSize.png"},
"content_scripts": [
{
"css": ["styles.css"],
"matches": ["https://*.bamboohr.co.uk/*"]
}
]
}

JSON - Make popup navigate to Google

I'm trying to write a Chrome extension where, when clicked, it will drop down Google. For now, it only loads a local .html file. Here is the code I currently have:
{
"manifest_version": 2,
"name": "theName",
"description": "TheDesc",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png",
"default_popup": "http://www.google.com",
"default_title": "Tooltip"
},
"permissions": [
"activeTab",
"https://ajax.googleapis.com/"
]
}
I have 4 files in my folder: icon.png, manifest.json, popup.html, & popup.js. I am using the "Getting Started" tutorial from https://developer.chrome.com/extensions/getstarted as a template, as I am extremely new to all of this (vb6 programmer for years).
Edit:
I finally got it to open a webpage in the little popup. Now, I need to grab the Title of the current tab's URL, remove the spaces from it, trim it down to 30 characters max, assign it to a string, then navigate to www.website.com/STRING. What I'm thinking is the extension needs to create a new popup.html each time, but that could be taxing on the server. Perhaps this can be done straight from the extension? I don't have a clue. Just guessing at this point.

Binding extension content scripts to Chrome's start page?

Is there any way to bind a content script to Chrome's start page?
I tried setting matches to "*", but it doesn't even run. With "*://*/*" it does not bind.
No, you cannot*. Technically, the start page is chrome://newtab/, and Chrome Extensions cannot access chrome:// pages for security reasons, not even with the widest "<all_urls>" permission.
Your only hope is to make your own New Tab page, though it would be hard to replicate all of the default functionality (e.g. thumbnails of top sites).
* One can enable this with Chrome Flags: chrome://flags/#extensions-on-chrome-urls But this is only applicable if the extension is for personal use and is a potential security risk.
Yes! Chrome's Start page (¿now?) has the hidden URL of the form:
https://www.google.com/_/chrome/newtab?espv=2&ie=UTF-8
And extensions with manifest.jsons like:
{
"manifest_version": 2,
"content_scripts": [ {
"js": [ "HelloWorld.js" ],
"matches": [ "*://*/_/chrome/newtab*" ]
} ],
"name": "Chrome start test",
"description": "Runs on the Chrome Start page",
"version": "1"
}
...run perfectly well on the Start page.

"exclude_matches" in manifest.json does nothing?

I'm having a problem controlling what pages my content scripts are injected into. The chrome extension developer guide specifies that I can use an "exclude_matches" directive in my manifest.json to exclude certain pages from injection.
However, this doesn't seem to have any effect. My content script still executes on pages that I have specified as ignored.
I have put the steps to reproduce in a Gist. The code is also available on Github.
Any ideas what I'm doing wrong?
manifest.json
{
"name": "Testing Extension",
"version": "1.0",
"description": "Test the chrome extensions exclude_matches.",
"content_scripts": [{
"matches": ["http://*/*", "https://*/*"],
"exclude_matches": ["http://news.ycombinator.com/"],
"js": ["content.js"]
}]
}
content.js
console.log("hello from the content script");
This is Bug #100106. exclude_matches do not function properly.
To solve the problem, use exclude_globs instead of exclude_matches.
Also, your exclude_matches rule does only match http://news.ycombinator.com/.
Suffice the pattern with an asterisk to match the whole site: http://news.ycombinator.com/*.
See also: Match patterns.