Remove shortcuts and adapt extension to activeTab permission
diff --git a/src/js/background.js b/src/js/background.js
index 021bed3..ff4eaef 100644
--- a/src/js/background.js
+++ b/src/js/background.js
@@ -270,23 +270,8 @@
return navigator.userAgent.toLowerCase().indexOf(operationSystem) > -1;
},
- executeScriptsInExistingTabs: function() {
- chrome.windows.getAll(null, function(wins) {
- for (var j = 0; j < wins.length; ++j) {
- chrome.tabs.getAllInWindow(wins[j].id, function(tabs) {
- for (var i = 0; i < tabs.length; ++i) {
- if (tabs[i].url.indexOf("chrome://") != 0) {
- chrome.tabs.executeScript(tabs[i].id, { file: 'js/page.js' });
- }
- }
- });
- }
- });
- },
-
init: function() {
localStorage.screenshootQuality = localStorage.screenshootQuality || 'png';
- screenshot.executeScriptsInExistingTabs();
screenshot.addMessageListener();
}
};
diff --git a/src/js/options.js b/src/js/options.js
index bbbce70..abb2efc 100644
--- a/src/js/options.js
+++ b/src/js/options.js
@@ -28,9 +28,11 @@
}
function save() {
- localStorage.screenshootQuality =
+ chrome.storage.local.set({
+ 'screenshootQuality':
$('lossy').checked ? 'jpeg' : '' ||
- $('lossless').checked ? 'png' : '';
+ $('lossless').checked ? 'png' : '',
+ });
return true;
}
@@ -43,8 +45,10 @@
}
function initScreenCaptureQuality() {
- $('lossy').checked = localStorage.screenshootQuality == 'jpeg';
- $('lossless').checked = localStorage.screenshootQuality == 'png';
+ chrome.storage.local.get('screenshotQuality', value => {
+ $('lossy').checked = value == 'jpeg';
+ $('lossless').checked = value == 'png';
+ });
}
function i18nReplace(id, name) {
diff --git a/src/js/page.js b/src/js/page.js
index 9ca2463..5fe494d 100644
--- a/src/js/page.js
+++ b/src/js/page.js
@@ -847,10 +847,11 @@
/**
* Remove an element
*/
- init: function() {
+ init: function() {
if (document.body.hasAttribute('screen_capture_injected')) {
return;
}
+ document.body.setAttribute('screen_capture_injected', true);
if (isPageCapturable()) {
chrome.extension.sendMessage({msg: 'page_capturable'});
} else {
diff --git a/src/js/popup.js b/src/js/popup.js
index 2319055..5eeb471 100644
--- a/src/js/popup.js
+++ b/src/js/popup.js
@@ -72,80 +72,98 @@
document.body.style.minWidth = "140px";
var isScriptLoad = false;
- chrome.tabs.getSelected(null, function(tab) {
+ chrome.tabs.query({
+ active: true,
+ }, function(tabs) {
+ if (tabs.length < 1) {
+ console.error('Expected 1 tab');
+ return;
+ }
+ var tab = tabs[0];
if (tab.url.indexOf('chrome') == 0 || tab.url.indexOf('about') == 0) {
i18nReplace('tip', 'special');
return;
- } else {
- $('tip').style.display = 'none';
- $('captureSpecialPageItem').style.display = 'block';
- showOption();
}
- chrome.tabs.sendMessage(tab.id, {msg: 'is_page_capturable'},
- function(response) {
- isScriptLoad = true;
- if (response.msg == 'capturable') {
- $('tip').style.display = 'none';
- $('captureSpecialPageItem').style.display = 'none';
- $('captureWindowItem').style.display = 'block';
- $('captureAreaItem').style.display = 'block';
- $('captureWebpageItem').style.display = 'block';
- var textWidth = $('captureWindowText')['scrollWidth'];
- resizeDivWidth('captureWindowText', textWidth);
- resizeDivWidth('captureAreaText', textWidth);
- resizeDivWidth('captureWebpageText', textWidth);
- var bg = chrome.extension.getBackgroundPage();
- if (bg.screenshot.isThisPlatform('mac')) {
- $('captureAreaShortcut').innerText = '\u2325\u2318R';
- $('captureWindowShortcut').innerText = '\u2325\u2318V';
- $('captureWebpageShortcut').innerText = '\u2325\u2318H';
- }
- } else if (response.msg == 'uncapturable') {
- i18nReplace('tip', 'special');
- $('tip').style.display = 'block';
- } else {
- i18nReplace('tip', 'loading');
- }
- });
- });
- chrome.tabs.executeScript(null, {file: 'js/isLoad.js'});
- var insertScript = function() {
- if (isScriptLoad == false) {
- chrome.tabs.getSelected(null, function(tab) {
- if (tab.url.indexOf('chrome') == 0 ||
- tab.url.indexOf('about') == 0) {
- i18nReplace('tip', 'special');
- } else {
- $('tip').style.display = 'none';
- $('captureSpecialPageItem').style.display = 'block';
- showOption();
- }
- });
- }
- var captureItems = document.querySelectorAll('li.menuI');
- var showSeparator = false;
- for (var i = 0; i < captureItems.length; i++) {
- if (window.getComputedStyle(captureItems[i]).display != 'none') {
- showSeparator = true;
- break;
- }
- }
- $('separatorItem').style.display = showSeparator ? 'block' : 'none';
- }
- setTimeout(insertScript, 500);
+ chrome.tabs.executeScript({ file: 'js/page.js' }, _ => {
+ chrome.tabs.executeScript(null, {file: 'js/isLoad.js'}, _ => {
+ $('tip').style.display = 'none';
+ $('captureSpecialPageItem').style.display = 'block';
+ showOption();
- // Update hot key.
- $('captureSpecialPageItem').addEventListener('click', function(e) {
- toDo('capture_special_page');
- });
- $('captureAreaItem').addEventListener('click', function(e) {
- toDo('capture_area');
- });
- $('captureWindowItem').addEventListener('click', function(e) {
- toDo('capture_window');
- });
- $('captureWebpageItem').addEventListener('click', function(e) {
- toDo('capture_webpage');
+ chrome.tabs.sendMessage(tab.id, {msg: 'is_page_capturable'},
+ response => {
+ isScriptLoad = true;
+ if (response.msg == 'capturable') {
+ $('tip').style.display = 'none';
+ $('captureSpecialPageItem').style.display = 'none';
+ $('captureWindowItem').style.display = 'block';
+ $('captureAreaItem').style.display = 'block';
+ $('captureWebpageItem').style.display = 'block';
+ var textWidth = $('captureWindowText')['scrollWidth'];
+ resizeDivWidth('captureWindowText', textWidth);
+ resizeDivWidth('captureAreaText', textWidth);
+ resizeDivWidth('captureWebpageText', textWidth);
+ var bg = chrome.extension.getBackgroundPage();
+ if (bg.screenshot.isThisPlatform('mac')) {
+ $('captureAreaShortcut').innerText = '\u2325\u2318R';
+ $('captureWindowShortcut').innerText = '\u2325\u2318V';
+ $('captureWebpageShortcut').innerText = '\u2325\u2318H';
+ }
+ } else if (response.msg == 'uncapturable') {
+ i18nReplace('tip', 'special');
+ $('tip').style.display = 'block';
+ } else {
+ i18nReplace('tip', 'loading');
+ }
+ });
+
+ var insertScript = function() {
+ if (isScriptLoad == false) {
+ chrome.tabs.query({
+ active: true,
+ }, function(tabs) {
+ if (tabs.length < 1) {
+ console.error('Expected 1 tab.');
+ return;
+ }
+ var tab = tabs[0];
+ if (tab.url.indexOf('chrome') == 0 ||
+ tab.url.indexOf('about') == 0) {
+ i18nReplace('tip', 'special');
+ } else {
+ $('tip').style.display = 'none';
+ $('captureSpecialPageItem').style.display = 'block';
+ showOption();
+ }
+ });
+ }
+ var captureItems = document.querySelectorAll('li.menuI');
+ var showSeparator = false;
+ for (var i = 0; i < captureItems.length; i++) {
+ if (window.getComputedStyle(captureItems[i]).display != 'none') {
+ showSeparator = true;
+ break;
+ }
+ }
+ $('separatorItem').style.display = showSeparator ? 'block' : 'none';
+ }
+ setTimeout(insertScript, 500);
+
+ // Update hot key.
+ $('captureSpecialPageItem').addEventListener('click', function(e) {
+ toDo('capture_special_page');
+ });
+ $('captureAreaItem').addEventListener('click', function(e) {
+ toDo('capture_area');
+ });
+ $('captureWindowItem').addEventListener('click', function(e) {
+ toDo('capture_window');
+ });
+ $('captureWebpageItem').addEventListener('click', function(e) {
+ toDo('capture_webpage');
+ });
+ });
+ });
});
}
diff --git a/src/js/showimage.js b/src/js/showimage.js
index b0d5f03..6e6d8ad 100644
--- a/src/js/showimage.js
+++ b/src/js/showimage.js
@@ -504,13 +504,15 @@
save: function() {
photoshop.draw();
- var formatParam = localStorage.screenshootQuality || 'png';
- var dataUrl;
- var isJpeg = formatParam == 'jpeg';
- $('canvas').toBlob(function(blob) {
- saveAs(blob, chrome.extension.getBackgroundPage().screenshot.screenshotName+".png");
- }, 'image/' + (isJpeg ? 'jpeg' : 'png'), (isJpeg ? 0.5 : null));
- photoshop.finish();
+ chrome.storage.local.get('screenshotQuality', formatParam => {
+ var formatParam = formatParam || 'png';
+ var dataUrl;
+ var isJpeg = formatParam == 'jpeg';
+ $('canvas').toBlob(function(blob) {
+ saveAs(blob, chrome.extension.getBackgroundPage().screenshot.screenshotName+".png");
+ }, 'image/' + (isJpeg ? 'jpeg' : 'png'), (isJpeg ? 0.5 : null));
+ photoshop.finish();
+ });
},
drawLineOnMaskCanvas: function(startX, startY, endX, endY, type, layerId) {