OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 Google Inc. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Return true if the extension is permitted to run on the given URL, with |
| 6 // the indicated permission for file:// schemes. |
| 7 function urlAllowed(url, file_ok) { |
| 8 // Extensions are prohibited on the Chrome Web Store. |
| 9 if (url.startsWith('https://chrome.google.com/webstore/')) |
| 10 return false; |
| 11 |
| 12 // Extensions are permitted to run on pages with schemes http, https, and |
| 13 // ftp, plus file if enabled. |
| 14 if (url.startsWith('http://') || |
| 15 url.startsWith('https://') || |
| 16 url.startsWith('ftp://')) |
| 17 return true; |
| 18 |
| 19 if (file_ok && url.startsWith('file://')) |
| 20 return true; |
| 21 |
| 22 return false; |
| 23 } |
| 24 |
| 25 // Update the popup display depending on the current options and the URL of |
| 26 // the current page. |
| 27 function updatePopup(options, url) { |
| 28 var button = document.getElementById('list_edit_button'); |
| 29 |
| 30 if (!urlAllowed(url, options.file_ok)) { |
| 31 button.textContent = chrome.i18n.getMessage('popupAddBlacklist'); |
| 32 button.disabled = true; |
| 33 setStatusError('popupDisallowedURL'); |
| 34 } else if (options.blacklist.includes(url)) { |
| 35 button.textContent = chrome.i18n.getMessage('popupRemoveBlacklist'); |
| 36 } else { |
| 37 button.textContent = chrome.i18n.getMessage('popupAddBlacklist'); |
| 38 } |
| 39 |
| 40 // If the current page is prohibited because it's a file:// scheme, show a |
| 41 // link to chrome://extensions, where that setting can be changed. |
| 42 if (!options.file_ok && url.startsWith('file://')) { |
| 43 document.getElementById('file_url_message').hidden = false; |
| 44 document.getElementById('status').hidden = true; |
| 45 } |
| 46 } |
| 47 |
| 48 // Set the status text and display it in the error style. |
| 49 function setStatusError(message_id, placeholder) { |
| 50 setStatus(message_id, placeholder); |
| 51 document.getElementById('status').classList.add('error'); |
| 52 } |
| 53 |
| 54 // Set the status text. |
| 55 function setStatus(message_id, placeholder) { |
| 56 var status = document.getElementById('status'); |
| 57 if (placeholder === undefined) |
| 58 status.textContent = chrome.i18n.getMessage(message_id); |
| 59 else |
| 60 status.textContent = chrome.i18n.getMessage(message_id, placeholder); |
| 61 } |
| 62 |
| 63 // Initialize the page. |
| 64 function init() { |
| 65 LoadInternationalizedStrings(); |
| 66 |
| 67 // Load the active tab's URL, then set up page features that depend on it. |
| 68 chrome.tabs.query({active: true, currentWindow: true}, function(tabList) { |
| 69 var url = tabList[0].url; |
| 70 var element = document.getElementById('current_url'); |
| 71 element.textContent = chrome.i18n.getMessage('popupCurrentURL', url); |
| 72 |
| 73 // Load settings. |
| 74 var options = {}; |
| 75 chrome.storage.sync.get({ |
| 76 blacklist: [], |
| 77 disableInApplets: true, |
| 78 whitelist: [] |
| 79 }, function(items) { |
| 80 options = items; |
| 81 // Read the extension file-access permission and update the popup UI. |
| 82 chrome.extension.isAllowedFileSchemeAccess(function(file_ok) { |
| 83 options.file_ok = file_ok; |
| 84 updatePopup(options, url); |
| 85 }); |
| 86 }); |
| 87 |
| 88 // Set event handlers that depend on the URL. |
| 89 document.getElementById('list_edit_button').onclick = function() { |
| 90 var index = options.blacklist.indexOf(url); |
| 91 if (index > -1) |
| 92 options.blacklist.splice(index, 1); |
| 93 else |
| 94 options.blacklist.push(url); |
| 95 |
| 96 chrome.storage.sync.set({ |
| 97 blacklist: options.blacklist, |
| 98 whitelist: options.whitelist |
| 99 }, function() { |
| 100 if (chrome.runtime.lastError) { |
| 101 setStatusError('errorSaving', chrome.runtime.lastError.message); |
| 102 } else { |
| 103 updatePopup(options, url); |
| 104 setStatus('popupStatusSaved'); |
| 105 } |
| 106 }); |
| 107 }; |
| 108 |
| 109 document.getElementById('report_page').onclick = function() { |
| 110 reportPage(url); |
| 111 }; |
| 112 }); |
| 113 |
| 114 // Set event handlers that don't need the URL. |
| 115 document.getElementById('open_options').onclick = function() { |
| 116 chrome.runtime.openOptionsPage(); |
| 117 }; |
| 118 |
| 119 document.getElementById('open_extensions').onclick = function() { |
| 120 chrome.tabs.create({url: 'chrome://extensions', active:true}); |
| 121 window.close(); |
| 122 }; |
| 123 } |
| 124 |
| 125 window.addEventListener('load', init, false); |
OLD | NEW |