OshGoshJosh
OshGoshJosh

Reputation: 31

Update chrome extension manifest permissions from options page

I am trying to update the permissions in the chrome extension manifest file from the options page. Basically the user should be able to input the url for the extension to run on and that url will update in the extensions manifest file. I am currently storing all my options using chrome.storage.sync for use in multiple files. I am looking for a secure solution to give only the chosen url access. Any suggestions?

Upvotes: 3

Views: 1180

Answers (1)

Xan
Xan

Reputation: 77591

That's not possible (to update the manifest).

However, the particular use case you explain is:

  1. Have a list of "allowed" websites.
  2. When the extension is invoked by pressing the button, if the website is allowed - inject a content script that does something.

In this case "activeTab" permission and Programmatic Injection should solve your problem.

  • You do not declare any content scripts in the manifest. This ensures code runs only when you want to.
  • You do not declare any host permissions in the manifest. Your extension will ONLY work on the current tab at the moment of Browser Action press.
  • You ask the user for allowed URLs and store them in chrome.storage.
  • When your extension is invoked using the Browser Action, you can query the currently active tab and you will get its URL (because of the "activeTab" permission).
    • Your logic then compares it to stored whitelisted URLs.
    • If there's a match - you use chrome.tabs.executeScript to inject your content script.
    • If there's no match - do nothing, or somehow ask user to confirm whitelisting the new domain.

Here's some sample code:

chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
  var currentTab = tabs[0];
  // Pseudocode
  if (whitelisted(currentTab.url)) {
    chrome.tabs.executeScript(currentTab.id, {file: "content.js"});
  } else {
    // Do nothing or show some warning
  }
});

Alternatively, you can look at Optional Permissions API.

Upvotes: 1

Related Questions