 Issue 4937860104847360:
  Issue 2082 - Detect existing tabs when loading extension on Safari  (Closed)
    
  
    Issue 4937860104847360:
  Issue 2082 - Detect existing tabs when loading extension on Safari  (Closed) 
  | Left: | ||
| Right: | 
| LEFT | RIGHT | 
|---|---|
| 1 /* | 1 /* | 
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 
| 3 * Copyright (C) 2006-2015 Eyeo GmbH | 3 * Copyright (C) 2006-2015 Eyeo GmbH | 
| 4 * | 4 * | 
| 5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify | 
| 6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as | 
| 7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. | 
| 8 * | 8 * | 
| 9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, | 
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 226 for (var id in tab._pages) | 226 for (var id in tab._pages) | 
| 227 { | 227 { | 
| 228 if (id != page._id) | 228 if (id != page._id) | 
| 229 forgetPage(id); | 229 forgetPage(id); | 
| 230 } | 230 } | 
| 231 | 231 | 
| 232 if (isPageActive(page)) | 232 if (isPageActive(page)) | 
| 233 updateToolbarItemForPage(page, tab.browserWindow); | 233 updateToolbarItemForPage(page, tab.browserWindow); | 
| 234 }; | 234 }; | 
| 235 | 235 | 
| 236 var addPage = function(tab, url, prerendered) | 236 var addPage = function(tab, url, prerendered) | 
| 
Sebastian Noack
2015/03/03 19:51:34
This function is new.
 | |
| 237 { | 237 { | 
| 238 var pageId = ++pageCounter; | 238 var pageId = ++pageCounter; | 
| 239 | 239 | 
| 240 if (!('_pages' in tab)) | 240 if (!('_pages' in tab)) | 
| 241 tab._pages = Object.create(null); | 241 tab._pages = Object.create(null); | 
| 242 | 242 | 
| 243 var page = new Page(pageId, tab, url); | 243 var page = new Page(pageId, tab, url); | 
| 244 pages[pageId] = tab._pages[pageId] = page; | 244 pages[pageId] = tab._pages[pageId] = page; | 
| 245 | 245 | 
| 
kzar
2015/03/04 10:46:57
Seems a shame you stripped out the useful comment
 
Sebastian Noack
2015/03/04 11:04:35
I've re-added that comment. Also added some more c
 | |
| 246 // When a new page is shown, forget the previous page associated | |
| 247 // with its tab, and reset the toolbar item if necessary. | |
| 248 // Note that it wouldn't be sufficient to do that when the old | |
| 249 // page is unloading, because Safari dispatches window.onunload | |
| 250 // only when reloading the page or following links, but not when | |
| 251 // you enter a new URL in the address bar. | |
| 246 if (!prerendered) | 252 if (!prerendered) | 
| 247 replacePage(page); | 253 replacePage(page); | 
| 248 | 254 | 
| 249 return pageId; | 255 return pageId; | 
| 250 }; | 256 }; | 
| 251 | 257 | 
| 252 ext.pages = { | 258 ext.pages = { | 
| 253 open: function(url, callback) | 259 open: function(url, callback) | 
| 254 { | 260 { | 
| 255 var tab = safari.application.activeBrowserWindow.openTab(); | 261 var tab = safari.application.activeBrowserWindow.openTab(); | 
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 299 return; | 305 return; | 
| 300 | 306 | 
| 301 // when a tab is closed, forget the previous page associated with that | 307 // when a tab is closed, forget the previous page associated with that | 
| 302 // tab. Note that it wouldn't be sufficient do that when the old page | 308 // tab. Note that it wouldn't be sufficient do that when the old page | 
| 303 // is unloading, because Safari dispatches window.onunload only when | 309 // is unloading, because Safari dispatches window.onunload only when | 
| 304 // reloading the page or following links, but not when closing the tab. | 310 // reloading the page or following links, but not when closing the tab. | 
| 305 for (var id in event.target._pages) | 311 for (var id in event.target._pages) | 
| 306 forgetPage(id); | 312 forgetPage(id); | 
| 307 }, true); | 313 }, true); | 
| 308 | 314 | 
| 315 // We generally rely on content scripts to report new pages, | |
| 316 // since Safari's extension API doesn't consider pre-rendered | |
| 317 // pages. However, when the extension initializes we have to | |
| 318 // use Safari's extension API to detect existing tabs. | |
| 309 safari.application.browserWindows.forEach(function(win) | 319 safari.application.browserWindows.forEach(function(win) | 
| 
Sebastian Noack
2015/03/03 19:51:34
This is where we detect existing tabs now.
 | |
| 310 { | 320 { | 
| 311 for (var i = 0; i < win.tabs.length; i++) | 321 for (var i = 0; i < win.tabs.length; i++) | 
| 312 { | 322 { | 
| 313 var tab = win.tabs[i]; | 323 var tab = win.tabs[i]; | 
| 314 var url = tab.url; | 324 var url = tab.url; | 
| 315 | 325 | 
| 326 // For the new tab page the url property is undefined. | |
| 316 if (url) | 327 if (url) | 
| 317 addPage(tab, url, false); | 328 addPage(tab, url, false); | 
| 318 } | 329 } | 
| 319 }); | 330 }); | 
| 320 | 331 | 
| 321 | 332 | 
| 322 /* Web requests */ | 333 /* Web requests */ | 
| 323 | 334 | 
| 324 ext.webRequest = { | 335 ext.webRequest = { | 
| 325 onBeforeRequest: new ext._EventTarget(), | 336 onBeforeRequest: new ext._EventTarget(), | 
| (...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 678 tab.activate(); | 689 tab.activate(); | 
| 679 if (callback) | 690 if (callback) | 
| 680 callback(page); | 691 callback(page); | 
| 681 return; | 692 return; | 
| 682 } | 693 } | 
| 683 } | 694 } | 
| 684 | 695 | 
| 685 ext.pages.open(optionsUrl, callback); | 696 ext.pages.open(optionsUrl, callback); | 
| 686 }; | 697 }; | 
| 687 })(); | 698 })(); | 
| LEFT | RIGHT |