From c8d0fd38140d87750d0e335bb17557ff12ef29e3 Mon Sep 17 00:00:00 2001 From: gorhill Date: Mon, 20 Apr 2015 08:28:19 -0400 Subject: [PATCH] this fixes #101 --- platform/chromium/vapi-background.js | 75 ++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 22 deletions(-) diff --git a/platform/chromium/vapi-background.js b/platform/chromium/vapi-background.js index e8114b110..98ee10fdb 100644 --- a/platform/chromium/vapi-background.js +++ b/platform/chromium/vapi-background.js @@ -73,6 +73,18 @@ vAPI.noTabId = '-1'; /******************************************************************************/ +var toChromiumTabId = function(tabId) { + if ( typeof tabId === 'string' ) { + tabId = parseInt(tabId, 10); + } + if ( typeof tabId !== 'number' || isNaN(tabId) || tabId === -1 ) { + return 0; + } + return tabId; +}; + +/******************************************************************************/ + vAPI.tabs.registerListeners = function() { var onNavigationClient = this.onNavigation || noopFunc; var onPopupClient = this.onPopup || noopFunc; @@ -204,17 +216,17 @@ vAPI.tabs.get = function(tabId, callback) { // Caller must be prepared to deal with nil tab value callback(tab); }; + if ( tabId !== null ) { - if ( typeof tabId === 'string' ) { - tabId = parseInt(tabId, 10); - } - if ( typeof tabId !== 'number' || isNaN(tabId) ) { + tabId = toChromiumTabId(tabId); + if ( tabId === 0 ) { onTabReady(null); } else { chrome.tabs.get(tabId, onTabReady); } return; } + var onTabReceived = function(tabs) { // https://code.google.com/p/chromium/issues/detail?id=410868#c8 if ( chrome.runtime.lastError ) { @@ -274,7 +286,7 @@ vAPI.tabs.open = function(details) { } // update doesn't accept index, must use move - chrome.tabs.update(parseInt(details.tabId, 10), _details, function(tab) { + chrome.tabs.update(toChromiumTabId(details.tabId), _details, function(tab) { // if the tab doesn't exist if ( vAPI.lastError() ) { chrome.tabs.create(_details, focusWindow); @@ -322,6 +334,11 @@ vAPI.tabs.open = function(details) { // Replace the URL of a tab. Noop if the tab does not exist. vAPI.tabs.replace = function(tabId, url) { + tabId = toChromiumTabId(tabId); + if ( tabId === 0 ) { + return; + } + var targetURL = url; // extension pages @@ -329,17 +346,10 @@ vAPI.tabs.replace = function(tabId, url) { targetURL = vAPI.getURL(targetURL); } - if ( typeof tabId !== 'number' ) { - tabId = parseInt(tabId, 10); - if ( isNaN(tabId) ) { - return; - } - } - chrome.tabs.update(tabId, { url: targetURL }, function() { - // this prevent console error + // https://code.google.com/p/chromium/issues/detail?id=410868#c8 if ( chrome.runtime.lastError ) { - return; + /* noop */ } }); }; @@ -347,20 +357,37 @@ vAPI.tabs.replace = function(tabId, url) { /******************************************************************************/ vAPI.tabs.remove = function(tabId) { + tabId = toChromiumTabId(tabId); + if ( tabId === 0 ) { + return; + } + var onTabRemoved = function() { - if ( vAPI.lastError() ) { + // https://code.google.com/p/chromium/issues/detail?id=410868#c8 + if ( chrome.runtime.lastError ) { + /* noop */ } }; - chrome.tabs.remove(parseInt(tabId, 10), onTabRemoved); + + chrome.tabs.remove(tabId, onTabRemoved); }; /******************************************************************************/ vAPI.tabs.reload = function(tabId /*, flags*/) { - if ( typeof tabId === 'string' ) { - tabId = parseInt(tabId, 10); + tabId = toChromiumTabId(tabId); + if ( tabId === 0 ) { + return; } - chrome.tabs.reload(tabId); + + var onReloaded = function() { + // https://code.google.com/p/chromium/issues/detail?id=410868#c8 + if ( chrome.runtime.lastError ) { + /* noop */ + } + }; + + chrome.tabs.reload(tabId, onReloaded); }; /******************************************************************************/ @@ -369,14 +396,14 @@ vAPI.tabs.injectScript = function(tabId, details, callback) { var onScriptExecuted = function() { // https://code.google.com/p/chromium/issues/detail?id=410868#c8 if ( chrome.runtime.lastError ) { + /* noop */ } if ( typeof callback === 'function' ) { callback(); } }; if ( tabId ) { - tabId = parseInt(tabId, 10); - chrome.tabs.executeScript(tabId, details, onScriptExecuted); + chrome.tabs.executeScript(toChromiumTabId(tabId), details, onScriptExecuted); } else { chrome.tabs.executeScript(details, onScriptExecuted); } @@ -392,7 +419,11 @@ vAPI.tabs.injectScript = function(tabId, details, callback) { // anymore, so this ensures it does still exist. vAPI.setIcon = function(tabId, iconStatus, badge) { - tabId = parseInt(tabId, 10); + tabId = toChromiumTabId(tabId); + if ( tabId === 0 ) { + return; + } + var onIconReady = function() { if ( vAPI.lastError() ) { return;