Redirect to url Using Google chrome Extension -
Redirect to url Using Google chrome Extension -
i newly google chrome extention , have on button in extention , button want redirect site "www.example.com" have next code , redirection.i scared why not work me solve this question
manifest.json
{ "name": "popup ", "manifest_version": 2, "version": "0.1", "description": "run process on page activated click in extension popup", "browser_action": { "default_icon": "icon.png", "default_popup": "popup.html" }, "permissions": [ "tabs", "http://*/*", "https://*/*" ] }
popup.html
<html> <head> <script src="popup.js"></script> <style type="text/css" media="screen"> body { min-width:250px; text-align: center; } #click-me { font-size: 15px; } </style> </head> <body> <button id='click-me'>click me!</button> </body> </html>
background.js
chrome.extension.onrequest.addlistener(function(request, sender) { chrome.tabs.update(sender.tab.id, {url: request.redirect}); });
popup.js
function clickhandler(e) { chrome.extension.sendrequest({redirect: "https://www.google.co.in"}); alert("url"); this.close(); } document.addeventlistener('domcontentloaded', function () { document.getelementbyid('click-me').addeventlistener('click', clickhandler); })
thats it.. please help me , dont want stuck on starting
if utilize background pages, need declare background script (background.js in case) in manifest file:
class="lang-js prettyprint-override"> "background": { "scripts": [ "background.js" ] },
your illustration not work though, because sender.tab
defined if request came tab or content script, not popup.
in example, there no need background page @ all, can utilize chrome.tabs
api straight popup page:
function clickhandler(e) { chrome.tabs.update({url: "https://example.com"}); window.close(); // note: window.close(), not this.close() } document.addeventlistener('domcontentloaded', function() { document.getelementbyid('click-me').addeventlistener('click', clickhandler); });
google-chrome google-chrome-extension
Comments
Post a Comment