Skip to content

Instantly share code, notes, and snippets.

@ZoeLiao
Last active August 26, 2019 05:39
Show Gist options
  • Save ZoeLiao/3d6d7a9c80addad8a9083ad350564871 to your computer and use it in GitHub Desktop.
Save ZoeLiao/3d6d7a9c80addad8a9083ad350564871 to your computer and use it in GitHub Desktop.
Chrome - Extension - isAllowedFileSchemeAccess - Demo
const LOCAL_FILE_REGEX_WINDOWS = new RegExp('^([A-Za-z]:[^\<\>\:\"\|\?\*]+)');
const LOCAL_FILE_REGEX_MAC = new RegExp('^(file:\//\/)');
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (!tabs[0]) {
return;
}
let url = tabs[0].url;
if (url.match(LOCAL_FILE_REGEX_WINDOWS) || url.match(LOCAL_FILE_REGEX_MAC)) {
chrome.extension.isAllowedFileSchemeAccess(
function(isAllowedAccess) {
if (!isAllowedAccess) {
alert('Please allow the extension to access your local files')
// You can get the message from 1i8n (internationalization) like the following code:
// alert(chrome.i18n.getMessage('<your variable name>'));
// create a new tab to show the information of your extension
chrome.tabs.create({
url: 'chrome://extensions/?id=' + chrome.runtime.id
});
return
} // if condition
} // isAllowedFileSchemeAccess function callback
) // isAllowedFileSchemeAccess function
} else {
// Do something ...
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment