From 3f9b598800dc7b12bc2c0c6da02427f32a588388 Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Tue, 10 Mar 2015 22:34:22 -0400 Subject: [PATCH] Change title checking mechanism, use MutationObserver and contentscripts, should resolve #13 and re-resolve #4 --- background.js | 35 ++++++++++++++++++----------------- manifest.json | 10 ++++++++++ title_listener.js | 13 +++++++++++++ 3 files changed, 41 insertions(+), 17 deletions(-) create mode 100644 title_listener.js diff --git a/background.js b/background.js index ec32f98..e555fb8 100644 --- a/background.js +++ b/background.js @@ -62,7 +62,7 @@ function clearWindowStorage(callback){ function saveWindows(callback){ windows.forEach(function(currentWindow){ - if (currentWindow.tabs.indexOf(null)>-1){ + if (currentWindow.tabs.indexOf(null)>-1 || currentWindow.tabs.indexOf(undefined)>-1){ console.log("[DEBUG] FOUND A NULL ELEMENT."); console.log(new Error().stack); console.log(currentWindow.tabs); @@ -107,6 +107,9 @@ function findTabById(queryWindow,tabId){ var windowIndex = result.index; queryWindow = result.window; } + if (queryWindow.tabs.indexOf(null)>-1 || queryWindow.tabs.indexOf(undefined)>-1){ + console.log("[DEBUG] FOUND A NULL ELEMENT! POSITION "+queryWindow.tabs.indexOf(undefined)+" "+queryWindow.tabs.indexOf(null)); + } var t = queryWindow.tabs.filter(function(currentTab){ return currentTab.id==tabId; }); @@ -157,21 +160,10 @@ chrome.tabs.onCreated.addListener(function(currentTab){ }); -chrome.tabs.onUpdated.addListener(function(tabId,changeInfo,resultingTab){ - var tab = findTabById(resultingTab.windowId, tabId); - // tab.window.tabs[tab.index] = resultingTab; //Old method that worked, but was weird on some pages such a gist - chrome.tabs.get(tabId,function(currentTab){ - if (currentTab===null){ - console.log("[DEBUG] FOUND A NULL ELEMENT."); - console.log(tab.window.tabs); - } - tab.window.tabs[tab.index] = currentTab; - saveWindows(); - if (tab.window.tabs.indexOf(null)>-1){ - console.log("[DEBUG] FOUND A NULL ELEMENT."); - console.log(tab.window.tabs); - } - }); +chrome.tabs.onUpdated.addListener(function(tabId,changeInfo,currentTab){ + var tab = findTabById(currentTab.windowId, tabId); + tab.window.tabs[tab.index] = currentTab; + saveWindows(); }); chrome.tabs.onMoved.addListener(function(tabId,objects){ @@ -232,12 +224,21 @@ chrome.tabs.onAttached.addListener(function(tabId,objects){ } }); -chrome.runtime.onMessage.addListener(function(response){ +chrome.runtime.onMessage.addListener(function(response,sender){ if (response.hasOwnProperty('nameChange')){ var currentWindow = findWindowById(response.nameChange.windowId); currentWindow.window.name = response.nameChange.name; saveWindows(); } + else if (response.hasOwnProperty('titleChange')){ + if (sender.tab!==undefined){ + var windowId = sender.tab.windowId; + var tabId = sender.tab.id; + var currentTab = findTabById(windowId, tabId); + windows[currentTab.windowIndex].tabs[currentTab.index].title = response.titleChange.newTitle; + saveWindows(); + } + } }); //init diff --git a/manifest.json b/manifest.json index 66d1f8f..9179dc1 100644 --- a/manifest.json +++ b/manifest.json @@ -9,7 +9,9 @@ "48":"img/icon48.png", "128":"img/icon128.png" }, + "offline_enabled":true, + "browser_action":{ "default_icon":{ "19":"img/icon19.png", @@ -17,6 +19,14 @@ }, "default_popup":"popup.html" }, + + "content_scripts": [ + { + "matches":[""], + "js":["title_listener.js"] + } + ], + "commands":{ "_execute_browser_action":{ "suggested_key":{ diff --git a/title_listener.js b/title_listener.js new file mode 100644 index 0000000..c7d60ce --- /dev/null +++ b/title_listener.js @@ -0,0 +1,13 @@ +var title = document.querySelector("title"); +if (title!=null){ + var observer = new MutationObserver(function(mutations){ + mutations.forEach(function(mutation){ + if (mutation.addedNodes.length===0 && mutation.removedNodes.length===0){ + var newTitle = mutation.addedNodes[0].nodeValue; + var oldTitle = mutation.removedNodes[0].nodeValue; + chrome.runtime.sendMessage({"titleChange":{"newTitle":newTitle,"oldTitle":oldTitle}}); + } + }); + }); + observer.observe(title,{childList:true,characterData:true,characterDataOldValue:true}); +} \ No newline at end of file