Skip to content

Commit

Permalink
Do not show permission request for file URL every time.
Browse files Browse the repository at this point in the history
/close: #23
  • Loading branch information
jinliming2 committed Dec 9, 2020
1 parent 137df0b commit 08dd76c
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions j/background.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Created by Liming on 2017/2/14.
*/
const charsetPattern = /; ?charset=([^;]+)/;
const charsetPattern = /; ?charset=([^;]+)/i;
const html_special_chars = html => html
.replace(/&/g, '>')
.replace(/</g, '&lt;')
Expand Down Expand Up @@ -83,6 +83,7 @@ const bodyModifier = (tabId, changeInfo, tab) => {
let xmlHttp = new XMLHttpRequest();
xmlHttp.overrideMimeType(`text/plain; charset=${encoding}`);
xmlHttp.onload = () => {
localStorage.removeItem('alertedCannotLoadLocalFile');
const is_html = /\.html?$/.test(tab.url);
const data = is_html ? encodeURIComponent(xmlHttp.responseText) : encodeURIComponent(html_special_chars(xmlHttp.responseText));
chrome.tabs.executeScript(tabId, {
Expand All @@ -92,7 +93,13 @@ const bodyModifier = (tabId, changeInfo, tab) => {
runAt: 'document_start',
});
};
xmlHttp.onerror = () => alert(chrome.i18n.getMessage('cannotLoadLocalFile'));
xmlHttp.onerror = () => {
if (localStorage.getItem('alertedCannotLoadLocalFile')) {
return;
}
localStorage.setItem('alertedCannotLoadLocalFile', '1');
alert(chrome.i18n.getMessage('cannotLoadLocalFile'));
};
xmlHttp.open('GET', tab.url, true);
xmlHttp.send();
};
Expand Down

0 comments on commit 08dd76c

Please sign in to comment.