how to make a chrome extension clickable - google-chrome

after reading the tutorials I still can't make my extension to work after clicking on it, it starts working as soon as the page loaded.
Here is my manifest.
{
"manifest_version": 2,
"name": "My extension",
"description": "This extension bla bla.",
"version": "3.0",
"permissions": [
"tabs", "http://*/*", "https://*/*"
],
"browser_action": {
"default_icon": "icon.png"
},
"content_scripts": [
{
"matches": ["http://*/*"],
"js": ["make_changes.js"]
}
],
"background": {
"scripts": ["make_changes.js"],
"persistent": false
}
}
and here is my make_changes.js:
var oldSource = document.documentElement.innerHTML;
document.body.innerHTML = Make_change(oldSource);
function Make_change(source){
...
}
I've also got the background.HTML file, but it works without it. It seems that it is not correct:
chrome.tabs.executeScript(null, {file: "make_changes.js"});

You need to add a listener for the browser action onClicked event:
http://developer.chrome.com/extensions/browserAction.html#event-onClicked
chrome.browserAction.onClicked.addListener(function(tab) {
chrome.tabs.executeScript(tab.id, {file: "make_changes.js"});
});

Related

Chrome extension manifest permissions v3

Alright I'm making my first chrome extension and I've ran into a bit of a problem:
manifest.json
{
"name": "...",
"description": "...",
"version": "1.0",
"manifest_version": 3,
"background": {
"service_worker": "background.js"
},
"permissions": [
"tabs",
"scripting",
"activeTab"
],
"host_permissions": [
"tabs",
"scripting",
"activeTab"
]
}
background.js
chrome.tabs.onUpdated.addListener(function(tabId, info, tab) {
if (info.status == 'complete') changeTheme(tab);
});
function changeTheme(tab) {
chrome.scripting.insertCSS({
target: {tabId: tab.id},
css: "h1 { color: #212121; }"
})
}
The extension devtool console logs this:
Error: Cannot access contents of url "long ass url". Extension manifest must request permission to access this host.
How would I "Request" permission?
"host_permissions" is for matching urls not for permissions.
Change this:
"host_permissions": [
"tabs",
"scripting",
"activeTab"
]
Into this:
"host_permissions": [
"https://example.org/foo/bar.html"
]
Or any other urls you want the extension to be available on (expect chrome://)
If you want the extension to be available on all sites use:
"host_permissions": [
"<all_urls>"
]

js file not visible in Chrome Extension debugger

I have a simple app that I'm using to learn Chrome Extensions.
I cannot get break points to work in mainscript.js file in the debugger.
I couldn't see mainscript.js under Pages or Content Scripts.
I can see it when I load Filesystem and apply breakpoints but they are not breaking.
The alert is being displayed.
How do I set break points that work in mainscript.js?
Manifest:
{
"name": "Enable/Disable",
"version": "1.0",
"manifest_version": 2,
"description": "Enables/Disables",
"permissions":
[
"http://xxx.xxx.xxx.xxx/*",
"activeTab"
],
"browser_action":
{
"default_icon":
{
"16": "images/Icon_ENABLED.png"
}
},
"background":
{
"scripts": ["background.js"]
}
}
background.js:
chrome.browserAction.onClicked.addListener
(
function(tab)
{
chrome.tabs.executeScript(tab.id, {file: "mainscript.js"});
}
);
mainscript.js
alert('Mainscript');

chrome extension download API is slow to respond and doesn't change the filename

I'm using chrome messaging feature to send the filename and link to the background page
chrome.runtime.sendMessage({link: thelink, name:filename}, function(response) {});
The background.js
chrome.runtime.onMessage.addListener(function (message, sender, sendResponse) {
console.log(message.name);
chrome.downloads.download({
url: message.link,
filename: message.name,
conflictAction: 'prompt'
});
});
My manifest.json
{
"manifest_version": 2,
"name": "4ch",
"description": "This extension shows a Google Image search result for the current page",
"version": "1.0",
"icons": {
"16": "4ch-icon-16.png", "48": "4ch-icon-48.png", "128": "4ch-icon-48.png"
}
,
"permissions": ["downloads",
"<all_urls>",
"contextMenus"],
"content_scripts": [{
"js": ["script/jquery.js", "script/reddit.js"],
"matches": ["*://*.reddit.com/*"]
}
],
"background": {
"scripts": ["script/jquery.js","script/eventpage.js"], "persistent": false
}
}
the download only starts after a few seconds and doesn't change the file name
I have ofund a solution. The 'download master' extension was in conflict with my own,hence it doesn't replace the original filename.I have disabled it

close pop-up - chrome extensions

Im trying to create a quite simple chrome extention, that close a pop-up after the pop-up is loaded, and by its title.
But for some reason the the title keeps returnen blank.
This is what i came up with.
{
"name": "ReportCloser",
"version": "0.1",
"permissions": [
"tabs","<all_urls>"
],
"browser_action": {
"default_icon": "icon.png"
},
"content_scripts": [
{
"matches": [
"http://*/*",
"https://*/*"
],
"run_at": "document_end" ,
"js": ["script.js"] // pay attention to this line
}
],
"manifest_version":2
}
script.js
var x = document.title;
var title = "customtitle";
if(x==title){
close();
}
else{ }
I tried to move run_at: document_end before the content script, but the problem i still there.
Any tips?

An extension that adds '#top' to the URL

Okay, so I've worked through trying to get this to work and I just have no clue; here is my manifest file:
EDIT: Now updated:
{
"manifest_version": 2,
"name": "To Top Button",
"version": "0.1",
"description": "Adds '#top' to the URL.",
"permissions": [
"tabs", "https://forums.robertsspaceindustries.com/*"
],
"background": {
"scripts": ["adder.js"],
"persistent": false
},
"browser_action": {
"default_icon": "icon.png",
"default_title": "To the top!"
}
}
EDIT: changed to a js file:
function updateUrl(tab){
var currentURL = tab.url
var newurl = currentURL.replace(currentURL + "#top");
chrome.tabs.update(tab.id, {url: newurl});
}
chrome.browserAction.onClicked.addListener(function(tab) {updateUrl(tab);});
Now, what I want to do, is when I click the icon, if I'm on a page that is sub to this (https://forums.robertsspaceindustries.com/) it adds "#top" to the end of the URL and then refreshes the page and takes me to the top.
What have I messed up?
I think you have to move your script from HTML file to JS (according to Manifest V2 Security Changes: Inline Scripts) and register it in the manifest.
If your JS is named handler.js, the manifest should be something like this:
{
"manifest_version": 2,
"name": "To Top Button",
"version": "0.1",
"description": "Adds '#top' to the URL.",
"permissions": [
"tabs", "https://forums.robertsspaceindustries.com/*"
],
"background": {
"scripts": ["handler.js"],
"persistent": false
},
"browser_action": {
"default_icon": "icon.png",
"default_title": "To the top!"
}
}
As for the code, try
function updateUrl(tab){
   var newurl = tab.url.replace(/(#[^#]*)?$/, "#top");
   chrome.tabs.update(tab.id, {url: newurl});
}
chrome.browserAction.onClicked.addListener(function(tab) { updateUrl(tab); });