I am looking for simple solution of my problem.
Pressing ctrl+alt+lmb should fire my script and I can achieve that using content_scripts, but content_scripts does not have access to chrome.*.
Any suggestions?
manifest.json
{
"manifest_version": 2,
"version": "1.0",
"name" : "Incognito Shortcut",
"content_scripts" : [
{
"matches" : ["*://*/*"],
"js" : ["core.js"],
"run_at" : "document_end",
"all_frames" : true
}
],
"permissions" : [
"tabs"
]
}
core.js
document.addEventListener("click", function(e) {
if(e.altKey && e.ctrlKey) {
// do stuff
e.preventDefault();
}
}, false);
Standard approach in such situation, is that you'll have to pass messages from content script to the background page (which has access to the most of chrome api's). E.g. content script will send message to the background-page and background page will call some appropriate chrome api
If you will read articles Content-Scripts and Message Passing from official documentation, you should be able to implement it easily.
Related
I am trying to make a very simple Manifest v3 chrome extension that blocks an "automatic redirect" on a website.
I like to use an overseas online merchant ("foo.com") but when you click into a product's details, it sends me to sso.foo.com to login w/ a free account. I can tap the browser stop button when the product page shows up, but before the "redirect" executes (i think its more like a JS triggered window.location) - and i can work around it that way; but i'd rather just make a chrome extension for myself that blocks this.
I tried creating a declarative_new_request that blocks requests to sso.foo.com
[{
"id" : 1,
"priority": 1,
"action" : { "type" : "block" },
"condition" : {
"urlFilter" : "sso.foo.com",
"domains" : ["foo.com"],
"resourceTypes" : ["script", "main_frame", "sub_frame"]
}
}]
...And this almost works, however the browser blocks the "end" of the request to https://sso.foo.com - so instead of "stopping" on the Product page, i see a chrome page saying "sso.foo.com" was blocked by an extension. What i want, is to block the navigation to https://soo.foo.com/* but leave me on the current page ... is this possible? Thoughts on how i can do this?
For script resource type you can try redirecting to a data URL:
"action" : { "type" : "redirect", "url": "data:," },
The main_frame and sub_frame navigation cancelling is not implemented yet.
Please star https://crbug.com/943223 to raise its importance.
The workaround
Assuming the page is using an <a> link you can declare a content script with a click listener in the capturing phase that hides this event from the page.
manifest.json:
"content_scripts": [{
"matches": ["*://*.foo.com/*"],
"js": ["content.js"],
"run_at": "document_start"
}]
content.js:
window.addEventListener('click', ev => {
const a = ev.target.closest('a');
if (a?.hostname === 'sso.foo.com') {
ev.stopPropagation();
ev.preventDefault();
}
}, true);
I thought this would be a regularly asked question, but I can not find the answer for it.
Is there a way for a chrome extension to have it's browser action popup available only on content_scripts matching pages?
Eg: I make an extension for a site. I want the popup to only be available when browsing the site (on the current tab). How do I do?
For me the browser action is always enabled by default, whatever page I'm browsing, and page action never enabled.
Thanks for your help
EDIT: Here is part of the manifest, I changed with pageAction with No success. I want my icon to activate only on tabs browsing LDLC.
{
"content_scripts": [
{
"js": [ "jquery.min.js", "scrooge.js" ],
"matches": [ "*://www.ldlc.com/fiche/*", "*://ldlc.com/fiche/*" ]
}],
"manifest_version": 2,
"permissions": [ "storage", "activeTab","*://ldlc.com/*", "*://scroogealpha.esy.es/*" ],
"page_action": {
"default_title": "Worth buying?", // optional; shown in tooltip
"default_popup": "popup.html" // optional
},
"web_accessible_resources": [
],
"background":{
"scripts" : ["eventPage.js"],
"persistent": false
}
}
Use a page action and in your background script listen for tab changes. When a tab url changes, check if you want to show the page action.
In your background script do something like this:
let matches = ["://www.ldlc.com/fiche/", "://ldlc.com/fiche/" ]
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
for (let i in matches) {
if (tab.url.includes(matches[i])) {
chrome.pageAction.show(tabId)
break
}
}
})
Also add the tabs permission in your manifest.
We are new to chrome extensions - what we are trying to do is create an extension that can capture information from the current browser window. As an example, if we went to http://www.bbc.co.uk/sport/football/tables we would want to be able to pick out the teams name and details to be saved to our database for later use on our site.
Using jquery we initially added a button to the top of the page and when clicked, it would fire our jquery script and this worked fine but we would prefer to have an icon in the toolbar so when clicked we could navigate to the page and gather the info rather than adding a button to the page.
Manifest is set up like so:
{
"manifest_version": 2,
"name": "registration",
"version": "1",
"permissions": [
"http://*/*",
"https://*/*"
],
"content_scripts": [{
"matches": ["http://*/*"],
"js": ["jquery.min.js","app.js"]
}]
}
content_script
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
if (request.method == "getHTML")
sendResponse({data: document.getElementById('header').innerHTML});
else
sendResponse({}); // snub them.
});
Any pointers would really be appreciated.
Thanks
Rich
I've tried browsing through similar questions posted here, but none seems to work
Manifest.json
{
"manifest_version": 2,
"name" : "A simple Found Text Demo",
"description" : "Bla",
"version" : "1.0",
"background" : {
"pages" : "background.html"
},
"page_action" : {
"default_icon" : "icon.png"
},
"content_scripts" : [{
"matches" : ["*://*/*"],
"js" : ["contentscript.js"]
}]
}
Background.html
<html>
<script>
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse){
alert(request);
//chrome.pageAction.show(sender.tab.id);
sendResponse('Found!');
}
)
</script>
</html>
Contentscript.js
chrome.extension.sendMessage({"name" : "hola"}, function(res){
console.log(res); })
However I repeatedly get the same error :
Port error: Could not establish connection. Receiving end does not exist.
Any ideas?
Since things changed over to manifest 2, you are actually no longer allowed to use in-line scripts (such as what you have in your background.html in the <script> tags above. See here). I'm not sure of your use case, but in most cases simple cases (read: the stuff I've done :) ), you don't actually need to populate background.html with anything. Instead, you can directly pass in a background.js file that will contain the same script you have above. Therefore you can try changing your manifest.json to this:
{
"manifest_version": 2,
"name" : "A simple Found Text Demo",
"description" : "Bla",
"version" : "1.0",
"background" : {
"scripts" : ["background.js"]
},
"page_action" : {
"default_icon" : "icon.png"
},
"content_scripts" : [{
"matches" : ["*://*/*"],
"js" : ["contentscript.js"],
"run_at": "document_end"
}]
}
Note we did two things here - changed pages to scripts inside of background and pointed it to ["background.js"], and then added "run_at": "document_end" to the end of the content_scripts section. This is something that can definitely cause issues if left out (issues similar to what you are seeing now) - you are now telling the content script to run after the page has loaded. If it runs immediately, you run the risk of the background page not having loaded, which means it isn't yet ready to receive messages and gives you the connection error. Below is background.js, which is identical to the script you had in between your <script> tags before:
chrome.extension.onMessage.addListener(
function(request, sender, sendResponse){
alert(request);
//chrome.pageAction.show(sender.tab.id);
sendResponse('Found!');
}
)
I want to check some values in the content of chrome browser page when it completely loaded
like that
if(document.body.innerText.indexOf("Cat") !=-1)
Where and when can I make my check? please give me an clear example
I read some thing about "Background.html" and "Content script" but I can't do
Register a content script in the manifest file at "run_at": "document_idle" (which is the default) and put your code in the content script file. Then the script will be run when the page is ready.
If you want to detect from the background page whether a page is completely loaded, use the chrome.webNavigation.onCompleted event and do whatever you want, such as calling chrome.tabs.executeScript to execute a content script. This method could be useful over the previous method if the list of URLs is dynamic or if the URL patterns cannot be described using the match pattern syntax.
chrome.webNavigation.onCompleted.addListener(function(details) {
chrome.tabs.executeScript(details.tabId, {
code: ' if (document.body.innerText.indexOf("Cat") !=-1) {' +
' alert("Cat not found!");' +
' }'
});
}, {
url: [{
// Runs on example.com, example.net, but also example.foo.com
hostContains: '.example.'
}],
});
The webNavigation and host permissions have to be set in manifest.json, e.g.:
{
"name": "Test",
"version": "1.0",
"background": { "scripts": ["background.js"] },
"permissions": [ "webNavigation", "*://*/*" ],
"manifest_version": 2
}