var core = {}; // Main app object in background.js var app = {}; // Shared app object with pages core.init = function(){ // Init module core.Settings.load(function(){ core.GridNodes.sync(app.settings.grid.node, app.settings.grid.root, function(){ // Sync bookmarks with stored data core.ContextMenus.initMenu(); core.Bookmarks.initListener(); }); }); } core.Settings = {}; // Settings helper object core.Settings.load = function(callback){ // Load settings browser.storage.local.get({ version: 2, backgroundColor: '#3c4048', backgroundImage: null, grid: { margin: 10, rows: 4, columns: 5, backIcon: 'url(/img/back.png)', folderIcon: 'url(/img/folder.png)', loadingIcon: 'url(/img/throbber.gif)', cells: { margin: 4, marginHover: 4, ratioX: 4, ratioY: 3, backgroundColor: null, backgroundColorHover: null, borderColor: '#333333', borderColorHover: '#a9a9a9', borderRadius: 4, borderRadiusHover: 4, title: true, titleHeight: 16, titleFontSize: 10, titleFont: 'Arial, Verdana, Sans-serif', titleColor: '#ffffff', titleColorHover: '#33ccff', titleBackgroundColor: null, titleBackgroundColorHover: null, backPanel: true }, root: 'Quick Dial', node: {} } }).then(function(obj){ if(!obj.version){ // Upgrade Data Version obj.version = 2; obj.grid.backIcon = 'url(/img/back.png)'; obj.grid.folderIcon = 'url(/img/folder.png)'; obj.grid.loadingIcon = 'url(/img/throbber.gif)'; obj.grid.cells.backgroundColor = null; obj.grid.cells.backgroundColorHover = null; obj.grid.cells.titleBackgroundColor = null; obj.grid.cells.titleBackgroundColorHover = null; delete obj.grid.cells.backIcon; delete obj.grid.cells.folderIcon; delete obj.grid.cells.loadingIcon; } app.settings = obj; if(callback) callback(); }); } core.Settings.save = function(){ // Save settings browser.storage.local.set(app.settings); browser.runtime.sendMessage({ command: 'SettingsChanged' }); } core.init(); core.ContextMenus = {} // ContextMenu helper Object core.ContextMenus.initMenu = function(){ // (Called from core.init) Init context menu in all pages browser.contextMenus.create({ // Create Context menu id: 'AddToQuickDial', title: browser.i18n.getMessage("menuAddToQuickDial"), contexts: ["all"], documentUrlPatterns: [ 'http://*/*', 'https://*/*', 'file://*/*' ] }, function(){}); browser.contextMenus.onClicked.addListener(function(info, tab) { // Context menu click event if (info.menuItemId == "AddToQuickDial") core.GridNodes.createBookmark(app.settings.grid.node, info.pageUrl, tab.title, function(){}); }); } core.Bookmarks = {} // Bookmarks helper object core.Bookmarks._onCreated = function(){ core.GridNodes.sync(app.settings.grid.node, app.settings.grid.root); } core.Bookmarks._onChanged = function(){ core.GridNodes.sync(app.settings.grid.node, app.settings.grid.root); } core.Bookmarks._onMoved = function(){ core.GridNodes.sync(app.settings.grid.node, app.settings.grid.root); } core.Bookmarks._onRemoved = function(){ core.GridNodes.sync(app.settings.grid.node, app.settings.grid.root); } core.Bookmarks.initListener = function(){ // (Called from core.init) (/!\ Need filter to root tree only) Init listener of bookmarks browser.bookmarks.onCreated.addListener(core.Bookmarks._onCreated); //browser.bookmarks.onChanged.addListener(core.Bookmarks._onChanged); browser.bookmarks.onMoved.addListener(core.Bookmarks._onMoved); browser.bookmarks.onRemoved.addListener(core.Bookmarks._onRemoved); } core.Bookmarks.load = function(rootPath, callback){ // Load root bookmark and create it if not exist if(!callback) return; browser.bookmarks.getSubTree('menu________').then(function(bookmarkItems){ function getChildItem(bookmarkItem, path, callback){ if(path.length == 0){ callback(bookmarkItem); return; } for(var child of bookmarkItem.children) if((path + '/').startsWith(child.title + '/')) return getChildItem(child, path.substr(child.title.length + 1), callback); browser.bookmarks.create({ parentId: bookmarkItem.id, title: path.substr(0, (path + '/').indexOf('/')) }).then(function(bookmarkItem){ return getChildItem(bookmarkItem, path.substr(bookmarkItem.title.length + 1), callback); }, function(){ callback(); }); } getChildItem(bookmarkItems[0], rootPath, callback); }, function(){ callback(); }); } core.SiteInfos = {} // Siteinfos helper object core.SiteInfos.fromTab = function(callback){ // Retrieve infos from current tab. callback( { url, title, icon, screenshot } || error: callback() ) browser.tabs.getCurrent().then(function(tab){ function whaitLoaded(){ browser.tabs.get(tab.id).then(function(tab){ if(tab.status == 'loading') setTimeout(whaitLoaded, 300); else { browser.tabs.update(tab.id, {active: true}).then(function(){ setTimeout(function(){ browser.tabs.captureVisibleTab().then(function(img){ browser.tabs.remove(tab.id); if(callback) callback( { url: tab.url, title: tab.title, icon: tab.favIconUrl, screenshot: img } ); }, function(){ browser.tabs.remove(tab.id); if(callback) callback(); }); }, 300); }, function(){ if(callback) callback(); }); } }, function(){ if(callback) callback(); }); } setTimeout(whaitLoaded, 300); }, function(){ if(callback) callback(); }); } core.SiteInfos.fromNewTab = function(url, callback){ // Retrieve infos from a new tab. callback( { url, title, icon, screenshot } || error: callback() ) browser.tabs.create({url: url, active: false}).then(function(tab){ browser.tabs.update(tab.id, {muted: true}).then(); function whaitLoaded(){ browser.tabs.get(tab.id).then(function(tab){ if(tab.status == 'loading') setTimeout(whaitLoaded, 300); else { browser.tabs.update(tab.id, {active: true}).then(function(){ setTimeout(function(){ browser.tabs.captureVisibleTab().then(function(img){ browser.tabs.remove(tab.id); if(callback) callback( { url: tab.url, title: tab.title, icon: tab.favIconUrl, screenshot: img } ); }, function(){ browser.tabs.remove(tab.id); if(callback) callback(); }); }, 300); }, function(){ if(callback) callback(); }); } }, function(){ if(callback) callback(); }); } setTimeout(whaitLoaded, 300); }, function(){ if(callback) callback(); }); } core.SiteInfos.fromFrame = function(url, callback){ // Retrieve infos from an iframe. callback( { url, title, (/!\ Not handled now)icon, screenshot } || error: callback() ) function pageLoaded(){ if(!iframe) return; var docTitle = iframe.contentWindow.document.title; var docIcon = null; var docScreenshot = null; //title if(docTitle == '') docTitle = url; //icon //screenshot var canvas = document.createElement('canvas'); canvas.style.width = previewWidth.toString() + 'px'; canvas.style.height = previewHeight.toString() + 'px'; canvas.width = previewWidth / 2; canvas.height = previewHeight / 2; var ctx = canvas.getContext('2d'); ctx.clearRect(0, 0, previewWidth, previewHeight); ctx.save(); ctx.scale(0.5, 0.5); ctx.drawWindow(iframe.contentWindow, 0, 0, previewWidth, previewHeight, 'rgb(255, 255, 255)'); ctx.restore(); docScreenshot = canvas.toDataURL(); document.body.removeChild(iframe); iframe = null; if(callback) callback({ url: url, title: docTitle, icon: docIcon, screenshot:docScreenshot }); } var previewWidth = 1200; // Need to be linked to settings var previewHeight = 710; // Need to be linked to settings var iframe; var xmlHttp = new XMLHttpRequest(); xmlHttp.timeout = 10000 xmlHttp.open('GET', url, true); xmlHttp.onload = function(){ iframe = document.createElement('iframe'); iframe.width = previewWidth iframe.height = previewHeight iframe.style.position = 'absolute'; //iframe.style.visibility = 'hidden'; var content = xmlHttp.responseText.replace('
', '