Change title checking mechanism, use MutationObserver and contentscripts, should resolve #13 and re-resolve #4

This commit is contained in:
Nick Krichevsky 2015-03-10 22:34:22 -04:00
parent c9c1a6b4c8
commit 3f9b598800
3 changed files with 41 additions and 17 deletions

View file

@ -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

View file

@ -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":["<all_urls>"],
"js":["title_listener.js"]
}
],
"commands":{
"_execute_browser_action":{
"suggested_key":{

13
title_listener.js Normal file
View file

@ -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});
}