Chrome extension need HELP!

60 views
Skip to first unread message

Leonardo Sedevcic

unread,
Nov 15, 2016, 5:34:43 AM11/15/16
to apps-dev
Hi i have created an extension and i would like to do a redirection to google.com

actually i have this in my code:

};
function extract_domain(url)    {
    var matches = url.match(/^https?\:\/\/([^\/?#]+)(?:[\/?#]|$)/i);
    return matches[1];
}
 
chrome.webRequest.onBeforeRequest.addListener(function(details) { 
var domain = extract_domain(details.url);
return { cancel: AUTHORIZED_DOMAINS[domain ]===false }; 
}, {urls: ["<all_urls>"]},["blocking"]);})();



can somene tell me how to do a redirection, how to change my code?

THANKS

Antony Sargent

unread,
Nov 15, 2016, 12:28:21 PM11/15/16
to Leonardo Sedevcic, apps-dev
Instead of providing a 'cancel' property in your BlockingResponse return value, you probably want to have 'redirectUrl' instead. See here for more details: https://developer.chrome.com/extensions/webRequest#type-BlockingResponse.

Antony Sargent

unread,
Nov 23, 2016, 12:30:29 PM11/23/16
to Leonardo Sedevcic, apps-dev
Try something like this:

function onBeforeRequestListener(details) {
  var shouldCancel = determineCancel(details.url);
  if (shouldCancel) {
    return { 'cancel': true };
  }
  var newUrl = determineRedirectUrl(details.url);
  if (newUrl) {
    return { 'redirectUrl': newUrl };
  }
  return {};
}
 
chrome.webRequest.onBeforeRequest.addListener(
 onBeforeRequestListener,
 {urls: ["<all_urls>"]}, ["blocking"]
);






On Wed, Nov 23, 2016 at 4:21 AM, Leonardo Sedevcic <mattepai...@gmail.com> wrote:
i tried to change

return { cancel: AUTHORIZED_DOMAINS[domain ]===false }; 

to

redirect = {urls: ['https://www.google.be/']};

but its not working :s
Reply all
Reply to author
Forward
0 new messages