Random URL redirect from array

前端 未结 8 1232
刺人心
刺人心 2021-01-27 05:37

/** * Political Animals * contentscript.js is loaded on each page(s) listed in manifest.json * This plugin replaces all the images on the website of news sites with pic

8条回答
  •  太阳男子
    2021-01-27 06:25

    window.location doesn't work since content scripts are unprivileged. Further more, window.location.href returns the current location, but it is not a method so you cannot overwrite it.

    you'll need to:

    Send redirect url from a content script to a background page:

    var url = acceptedWebsites[Math.floor(Math.random()*acceptedWebsites.length)];
    chrome.extension.sendRequest({redirect: url });
    

    In a background page update tab's url which would cause redirect:

    chrome.extension.onRequest.addListener(function(request, sender) {
        chrome.tabs.update(sender.tab.id, {url: request.redirect});
    });
    

提交回复
热议问题