enenen
enenen

Reputation: 1967

How to allow an extension for two domains only?

I've written a google chrome extension. It's okay and works now but I want the extension to be usebale only on two domains because it's written for these two websites only and is useless for others. There is a context menu only. For now it hasn't even popup, or action button in the top right corner (hidden by default). How can achieve this?

My current manifest.json:

{
  "manifest_version": 2,
  "background": {
    "scripts": ["scripts/jquery.min.js", "scripts/background.js"]
  },
  "name": "Export Entries",
  "description": "some descriptions here",
  "version": "1.0",

  "icons": {
    "16": "images/logo.png",
    "48": "images/logo.png",
    "128": "images/logo.png"
  },
  "permissions": ["downloads", "tabs", "contextMenus", "http://my-own-domain-accessed-via-ajax-for-creating-some-files-there.com/*"],
  "content_scripts": [{
    "matches": ["*://allowed-domain1.com/*", "*://allowed-domain2.com/*"],
    "css": ["styles/style.css"],
    "js": ["scripts/jquery.min.js", "scripts/popup.js", "scripts/background.js"],
    "run_at": "document_end"
  }],
  "web_accessible_resources": [
    "images/logo.png"
  ]
}

As I understand the extension cannot be disabled absolutely, its process will run in background again. But it's not a problem. I just want to not display the context menu item on other websites.

background.js creates the context menu item and handles its click event:

function exportEntries(info, tab) {
  if (info['linkUrl'].indexOf('domain1.com/user/') > -1) {
    var user = info['linkUrl'].substr('27');
  } else {
    var user = null; // export all entries from this topic
  }

  $.ajax({
    url: 'http://my-own-domain-which-creates-the-file.eu/exportEntries/create.php',
    method: 'POST',
    data: {
      topic: tab.url,
      user: user
    }
  }).done(function(url) {
    forceDownload(url);
  });
}

function forceDownload(url) {
  var filename = url.replace(/^.*\/|\.[^.]*$/g, '');

  chrome.downloads.download({
      url: url,
      saveAs: true
    }, // options array
    function(id) {
      // callback function
    }
  );
};

document.addEventListener('DOMContentLoaded', function() {
  chrome.contextMenus.create({
    'title': 'Export Entries',
    'contexts': ['link'],
    'onclick': function(info, tab) {
      exportEntries(info, tab);
    }
  });
});

create.php is on my own domain. It just gets the current page's URL and the user's nickname. Then export all entries from the given topic (i.e. page URL) for the given user, creates a file (.txt, .pdf etc.) and sends back url for downloading the file.

popup.html, popup.js, css file and other stuff is not used for now.

Upvotes: 0

Views: 3139

Answers (2)

Rob W
Rob W

Reputation: 349112

Remove all of your content scripts, they're useless, because the chrome.contextMenus API can only be used on the background page.

To limit the context menu entry to certain domains, pecify the documentUrlPatterns key when you create the context menu using the chrome.contextMenus.create:

chrome.contextMenus.create({
    'title': 'Export Entries',
    'contexts': ['link'],
    'onclick': function(info, tab) {
        exportEntries(info, tab);
    },
    'documentUrlPatterns': [
        '*://allowed-domain1.com/*',
        '*://allowed-domain2.com/*'
    ]
});

Upvotes: 4

Brandon
Brandon

Reputation: 284

According to the content scripts documentation:

"If you want to inject the code only sometimes, use the permissions field instead, as described in Programmatic injection."

So instead of

"content_scripts": [
  {
     "matches": [ "http://allowed-domain.com" ]
  }
],

use

permissions: [
  "tabs", "http://allowed-domain.com"
],

Upvotes: 2

Related Questions