Index: go-back-with-backspace/pages/popup.js |
diff --git a/go-back-with-backspace/pages/popup.js b/go-back-with-backspace/pages/popup.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..aa4666b36a9a700dd2dbb537e8395eceb768379d |
--- /dev/null |
+++ b/go-back-with-backspace/pages/popup.js |
@@ -0,0 +1,125 @@ |
+// Copyright 2016 Google Inc. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+// Return true if the extension is permitted to run on the given URL, with |
+// the indicated permission for file:// schemes. |
+function urlAllowed(url, file_ok) { |
+ // Extensions are prohibited on the Chrome Web Store. |
+ if (url.startsWith('https://chrome.google.com/webstore/')) |
+ return false; |
+ |
+ // Extensions are permitted to run on pages with schemes http, https, and |
+ // ftp, plus file if enabled. |
+ if (url.startsWith('http://') || |
+ url.startsWith('https://') || |
+ url.startsWith('ftp://')) |
+ return true; |
+ |
+ if (file_ok && url.startsWith('file://')) |
+ return true; |
+ |
+ return false; |
+} |
+ |
+// Update the popup display depending on the current options and the URL of |
+// the current page. |
+function updatePopup(options, url) { |
+ var button = document.getElementById('list_edit_button'); |
+ |
+ if (!urlAllowed(url, options.file_ok)) { |
+ button.textContent = chrome.i18n.getMessage('popupAddBlacklist'); |
+ button.disabled = true; |
+ setStatusError('popupDisallowedURL'); |
+ } else if (options.blacklist.includes(url)) { |
+ button.textContent = chrome.i18n.getMessage('popupRemoveBlacklist'); |
+ } else { |
+ button.textContent = chrome.i18n.getMessage('popupAddBlacklist'); |
+ } |
+ |
+ // If the current page is prohibited because it's a file:// scheme, show a |
+ // link to chrome://extensions, where that setting can be changed. |
+ if (!options.file_ok && url.startsWith('file://')) { |
+ document.getElementById('file_url_message').hidden = false; |
+ document.getElementById('status').hidden = true; |
+ } |
+} |
+ |
+// Set the status text and display it in the error style. |
+function setStatusError(message_id, placeholder) { |
+ setStatus(message_id, placeholder); |
+ document.getElementById('status').classList.add('error'); |
+} |
+ |
+// Set the status text. |
+function setStatus(message_id, placeholder) { |
+ var status = document.getElementById('status'); |
+ if (placeholder === undefined) |
+ status.textContent = chrome.i18n.getMessage(message_id); |
+ else |
+ status.textContent = chrome.i18n.getMessage(message_id, placeholder); |
+} |
+ |
+// Initialize the page. |
+function init() { |
+ LoadInternationalizedStrings(); |
+ |
+ // Load the active tab's URL, then set up page features that depend on it. |
+ chrome.tabs.query({active: true, currentWindow: true}, function(tabList) { |
+ var url = tabList[0].url; |
+ var element = document.getElementById('current_url'); |
+ element.textContent = chrome.i18n.getMessage('popupCurrentURL', url); |
+ |
+ // Load settings. |
+ var options = {}; |
+ chrome.storage.sync.get({ |
+ blacklist: [], |
+ disableInApplets: true, |
+ whitelist: [] |
+ }, function(items) { |
+ options = items; |
+ // Read the extension file-access permission and update the popup UI. |
+ chrome.extension.isAllowedFileSchemeAccess(function(file_ok) { |
+ options.file_ok = file_ok; |
+ updatePopup(options, url); |
+ }); |
+ }); |
+ |
+ // Set event handlers that depend on the URL. |
+ document.getElementById('list_edit_button').onclick = function() { |
+ var index = options.blacklist.indexOf(url); |
+ if (index > -1) |
+ options.blacklist.splice(index, 1); |
+ else |
+ options.blacklist.push(url); |
+ |
+ chrome.storage.sync.set({ |
+ blacklist: options.blacklist, |
+ whitelist: options.whitelist |
+ }, function() { |
+ if (chrome.runtime.lastError) { |
+ setStatusError('errorSaving', chrome.runtime.lastError.message); |
+ } else { |
+ updatePopup(options, url); |
+ setStatus('popupStatusSaved'); |
+ } |
+ }); |
+ }; |
+ |
+ document.getElementById('report_page').onclick = function() { |
+ reportPage(url); |
+ }; |
+ }); |
+ |
+ // Set event handlers that don't need the URL. |
+ document.getElementById('open_options').onclick = function() { |
+ chrome.runtime.openOptionsPage(); |
+ }; |
+ |
+ document.getElementById('open_extensions').onclick = function() { |
+ chrome.tabs.create({url: 'chrome://extensions', active:true}); |
+ window.close(); |
+ }; |
+} |
+ |
+window.addEventListener('load', init, false); |