Programmatic method to let the user modify the man

2020-05-03 10:16发布

问题:

I am developing a Chrome extension for use with the Canvas LMS. A problem with this is that Canvas subscribers have different URLs which do not have a common top level domain. For instance, my University's Canvas site has the URL canvas.gu.se while another school might have canvas.myschool.edu. But I can't enter "matches":"https://canvas.*/*" in the manifest.json file, since top-level wildcards are not allowed (see this post for elaboration). Instead, I have to enter "matches":"https://*/*", and then programmatically (in the content.js code) weed out sites that don't have "canvas" in them.

That works in its own kludgy way, but Chrome Web Store is not very happy about it, which delays my updates by days.

One could of course use a narrow/dummy matches value and then ask the users to edit the manifest themselves to include the specific URL in used in each respective case, but how likely would they be to do that? Instead I would like the extension to launch a local page that prompts the user to input the specific URL and then edits the manifest.json file on that particular machine accordingly. Would that be possible and if so, how?

回答1:

An installed extension from the web store cannot be modified, its contents is protected and verified by Chrome. Only unpacked local extensions in developer-mode can be modified.

Solutions:

  1. Add "include_globs": ["*://canvas.*/*"] (info) in addition to your "matches".
    This won't help you reduce the review time in the web store, most likely.

  2. Remove "content_scripts" section, as well as hosts from "permissions" and switch to programmatic injection in a chrome.tabs.onUpdated (info) listener.

    This requires you to switch to optional permissions (you'll show a button/command in your UI to grant the URL permission) which will help you with the web store review times.

    There's even a more advanced declarativeContent API (with RequestContentScript action which is actually supported in stable Chrome).