I am trying to create a chrome extension to redirect a tab url via a custom url. I browsed many already answered questions and tested many but did not find the answer.
my manifest:
{
"name": "Price saver",
"version": "1.0",
"manifest_version": 2,
"description": "Price saving during online shopping",
"background": {
"scripts": ["background.js"]
},
"manifest_version": 2,
"permissions": ["tabs", "webRequest", "webRequestBlocking", "http://*/*", "https://*/*"]
}
my background.js
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
var currentUrl = tabs[0].url;
var newUrl = "www.dealtikka.com/out.php?url=" + currentUrl
return { redirectUrl: newUrl};
},
{
urls: [
'*://*.*/*'
],
types: ['main_frame']
},
['blocking']);
Nothing happens when I click the extension.
Related
Duplicate of Display current URL in a chrome extension
The solution in my 'background.js':
$(document).ready(function(){
chrome.tabs.query({'active': true, 'lastFocusedWindow': true, 'currentWindow': true}, function (tabs) {
var url = tabs[0].url;
console.log(url);
alert(url)
})
})
did not work for me.
I also gave 'tabs' permission on manifest.json
{
"manifest_version": 2,
"name": "YtDl",
"version": "0.1",
"permissions": ["tabs"],
"background": {
"persistent": false,
"scripts": ["jquery.min.js", "background.js"]
},
"content_scripts":[
{
"matches": ["<all_urls>"],
"js": ["jquery.min.js", "content.js"],
}
]
}
is there any other solution to get current tab url?
NOTE: I am trying to get URL from background script. when i reload the extension it works for one time in 'chrome://extensions/' and does not work at any other site.
I have a chrome extension.
What I want to is :
When an <a> tag is clicked in a page (named A) , I use chrome.webRequest.onBeforeRequest to block the request.
The question is:
I tried both {redirectUrl: 'javascript:'} and {cancel: true}, but the page A still redirect.
How can I prevent the redirect in chrome extension.
manifest.json:
{
"background": {
"page": "background.html"
},
"browser_action": {
"default_icon": "img/19.png",
"default_popup": "popup.html",
"default_title": "test"
},
"icons": {
"128": "img/128.png",
"48": "img/48.png"
},
"is_account_related": false,
"manifest_version": 2,
"minimum_chrome_version": "38.0.0.0",
"permissions": ["webRequest", "webRequestBlocking", "notifications", "tabs", "storage", "cookies", "http://*/*", "https://*/*", "*://*/*"],
"homepage_url": "xxxx",
"name": "test",
"description": "test",
"version": "10.3.6",
"content_security_policy": "script-src 'self' 'unsafe-eval' xxx 'unsafe-inline'; object-src 'self'",
"key": "xxx"
}
background.html
chrome.webRequest.onBeforeRequest.addListener(
function (details) {
console.log('========start============');
console.log('block', details, details.url);
console.log('========end============');
// return { redirectUrl: 'javascript:' };
return { cancel: true };
},
{ urls: ["*://developer.mozilla.org/*"] },
['blocking']
);
In the background.html, the log is correct. But the page still navigate
=== update
I find that the page use history API to navigate. So is there any way to prevent history api, like history.push, history.replace ?
You could also return false; every time <a> clicked like this:
for(elem of document.getElementsByTagName("a")){
elem.onclick = () => false;
}
Make sure you have appropriate permissions in your manifest, as it is mentioned in the docs
As this function uses a blocking event handler, it requires the "webRequest" as well as the "webRequestBlocking" permission in the manifest file.
Also you can try something like
chrome.webRequest.onBeforeRequest.addListener(
function(details) { return {cancel: true}; },
{urls: ["<all_urls>"]},
["blocking"]);
With the following code I try to open a mailto: link in a tab if the user clicks on the Chrome extension icon. However, upon click on the icon, no action is being performed.
manifest.json
{
"manifest_version": 2,
"name": "Extension",
"description": "Description",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png"
},
"permissions": [
"tabs"
]
}
popup.js
chrome.browserAction.onClicked.addListener(function tab) {
var emailUrl = "mailto:address#domain.com";
chrome.tabs.create({ url: emailUrl });
};
There are multible issues with your code:
You never require you popup.js.
You JavaScript code is invalid.
I recommend using an eventPage to trigger the E-Mail link:
manifest.json
{
"manifest_version": 2,
"name": "Extension",
"description": "Description",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png"
},
"background": {
"scripts": ["eventPage.js"],
"persistent": false
}
}
eventPage.js
chrome.browserAction.onClicked.addListener(tab => {
chrome.tabs.create({ url: 'mailto:address#domain.com' })
})
I'm looking to create a "new tab" extension like Panda or the Product Hunt Extension: the user can open a new tab with my website inside, with an hidden url.
I've generated my package with the awesome Extensionizr and here is my manifest.json :
manifest.json
{
"name": "My app",
"version": "0.0.1",
"manifest_version": 2,
"description": "My awesome app",
"homepage_url": "http://myapp.com",
"icons": {
"16": "icons/icon16.png",
"48": "icons/icon48.png",
"128": "icons/icon128.png"
},
"default_locale": "en",
"background": {
"scripts": [
"src/bg/background.js"
],
"persistent": false
},
"permissions": [
"tabs",
"http://myapp.com/*"
]
}
My background.js come from this answer, the problem seems really similar.
background.js
chrome.browserAction.onClicked.addListener(function(activeTab) {
var newURL = "http://myapp.com";
chrome.tabs.create({ url: newURL });
});
I'm still getting this error when I try to run the background page from the extension settings : Uncaught TypeError: Cannot read property 'onClicked' of undefined
And when I open a new tab, Google Chrome took the advantage and display me a google search page.
I do it wrong, and I don't know how/where/why
You're completely off-track. You don't want to open a (simple) new tab, you want to replace the "New Tab page".
Daniel's answer correctly explains why your code does not work, but it won't do what you wanted.
To replace Chrome's New Tab page, you need to use Override Pages:
"chrome_url_overrides" : {
"newtab": "myPage.html"
},
Inside that myPage.html, you can add an <iframe> for your remote content.
That's because you need to register the browser action in the manifest. Here is your manifest with the browser action added at the bottom.
{
"name": "My app",
"version": "0.0.1",
"manifest_version": 2,
"description": "My awesome app",
"homepage_url": "http://myapp.com",
"icons": {
"16": "icons/icon16.png",
"48": "icons/icon48.png",
"128": "icons/icon128.png"
},
"default_locale": "en",
"background": {
"scripts": [
"src/bg/background.js"
],
"persistent": false
},
"permissions": [
"tabs",
"http://myapp.com/*"
],
"browser_action": {}
}
Here are the browser action docs: https://developer.chrome.com/extensions/browserAction
I have written a sample extension to just add new tab to chrome in background page. It's not working. I am new to chrome extension, can you please help me?
manifest.json
{
"manifest_version": 2,
"name": "My extension",
"description": "Test Extension.",
"version": "1.0",
"background": {
"page": "bg.html"
},
"permissions": [
"history",
"tabs"
],
"browser_action": {
"default_icon": "icon.png"
}
}
bg.html
chrome.browserAction.onClicked.addListener(function(activeTab)
{
var newURL = "www.google.com";
chrome.tabs.create({ url: newURL });
});
Try to rename bg.html to bg.js and change manifest background declaration like this
manifest.json:
...
"background": {
"scripts": ["bg.js"]
},
...
bg.js:
chrome.browserAction.onClicked.addListener(function(activeTab)
{
var newURL = "www.google.com";
chrome.tabs.create({ url: newURL });
});