Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 Cu.import("resource://gre/modules/Services.jsm"); | 1 Cu.import("resource://gre/modules/Services.jsm"); |
2 | |
3 function require(module) | |
4 { | |
Wladimir Palant
2012/09/21 15:36:18
This is unnecessary - module in lib/ directory can
| |
5 let result = {}; | |
6 result.wrappedJSObject = result; | |
7 Services.obs.notifyObservers(result, "abpcrawler-require", module); | |
8 return result.exports; | |
9 } | |
10 | 2 |
11 function abprequire(module) | 3 function abprequire(module) |
12 { | 4 { |
13 let result = {}; | 5 let result = {}; |
14 result.wrappedJSObject = result; | 6 result.wrappedJSObject = result; |
15 Services.obs.notifyObservers(result, "adblockplus-require", module); | 7 Services.obs.notifyObservers(result, "adblockplus-require", module); |
16 if ("exports" in result) | 8 return result.exports; |
17 return result.exports; | |
18 else | |
19 return Cu.import("chrome://adblockplus-modules/content/" + | |
Wladimir Palant
2012/09/21 15:36:18
This is unnecessary - Diagnostics is currently com
| |
20 module[0].toUpperCase() + module.substr(1) + ".jsm", null); | |
21 } | 9 } |
22 | 10 |
23 let {Storage} = require("storage"); | 11 let {Storage} = require("storage"); |
24 let {Client} = require("client"); | 12 let {Client} = require("client"); |
25 | 13 |
26 let {Policy} = abprequire("contentPolicy"); | 14 let {Policy} = abprequire("contentPolicy"); |
27 let {Filter} = abprequire("filterClasses"); | 15 let {Filter} = abprequire("filterClasses"); |
16 let {Utils} = abprequire("utils"); | |
28 | 17 |
29 let origProcessNode = Policy.processNode; | 18 let origProcessNode = Policy.processNode; |
30 | 19 |
31 let siteTabs; | 20 let siteTabs; |
32 let currentTabs; | 21 let currentTabs; |
33 | 22 |
34 function processNode(wnd, node, contentType, location, collapse) | 23 function processNode(wnd, node, contentType, location, collapse) |
35 { | 24 { |
36 let result = origProcessNode.apply(this, arguments); | 25 let result = origProcessNode.apply(this, arguments); |
37 let url = location.spec; | 26 let url = (contentType === Policy.type.ELEMHIDE) ? location.text : |
38 if (url) | 27 location.spec; |
Wladimir Palant
2012/09/21 15:36:18
I think that you can remove that check - an nsIURI
| |
28 | |
29 let topWindow = wnd.top; | |
30 if (!topWindow.document) | |
39 { | 31 { |
40 let site = siteTabs[wnd.top.location.href]; | 32 Cu.reportError("No document associated with the node's top window"); |
41 let filtered = !result; | 33 return result; |
42 Storage.write([url, site, filtered]); | |
43 } | 34 } |
35 | |
36 let tabbrowser = Utils.getChromeWindow(topWindow).gBrowser; | |
37 if (!tabbrowser) | |
38 { | |
39 Cu.reportError("Unable to get a tabbrowser reference"); | |
40 return result; | |
41 } | |
42 | |
43 let browser = tabbrowser.getBrowserForDocument(topWindow.document); | |
44 if (!browser) | |
45 { | |
46 Cu.reportError("Unable to get browser for the tab"); | |
47 return result; | |
48 } | |
49 | |
50 let site = siteTabs.get(browser); | |
51 let filtered = !result; | |
52 Storage.write([url, site, filtered]); | |
44 return result; | 53 return result; |
45 } | 54 } |
46 | 55 |
47 function loadSite(site, window, callback) | 56 function loadSite(site, window, callback) |
48 { | 57 { |
49 if (!site) | 58 if (!site) |
50 return; | 59 return; |
51 | 60 |
52 let tabbrowser = window.gBrowser; | 61 let tabbrowser = window.gBrowser; |
53 let tab = tabbrowser.addTab(site); | 62 let tab = tabbrowser.addTab(site); |
54 let browser = tabbrowser.getBrowserForTab(tab); | 63 let browser = tabbrowser.getBrowserForTab(tab); |
64 | |
65 siteTabs.set(browser, site); | |
55 | 66 |
56 let progressListener = { | 67 let progressListener = { |
57 onStateChange: function(aBrowser, aWebProgress, aRequest, aStateFlags, aStat us) | 68 onStateChange: function(aBrowser, aWebProgress, aRequest, aStateFlags, aStat us) |
58 { | 69 { |
59 if (browser !== aBrowser) | 70 if (browser !== aBrowser) |
60 return; | 71 return; |
61 | 72 |
62 if (!(aStateFlags & Ci.nsIWebProgressListener.STATE_STOP)) | 73 if (!(aStateFlags & Ci.nsIWebProgressListener.STATE_STOP)) |
63 return; | 74 return; |
64 | 75 |
65 tabbrowser.removeTabsProgressListener(progressListener); | 76 tabbrowser.removeTabsProgressListener(progressListener); |
66 tabbrowser.removeTab(tab); | 77 tabbrowser.removeTab(tab); |
67 callback(); | 78 callback(); |
68 }, | |
69 onLocationChange: function(aBrowser, aWebProgress, aRequest, aLocation, aFla gs) | |
70 { | |
71 if (browser === aBrowser) | |
72 siteTabs[aLocation.spec] = site; | |
Wladimir Palant
2012/09/21 15:36:18
Ok, that's a hack. What if two webpages in differe
| |
73 } | 79 } |
74 }; | 80 }; |
75 tabbrowser.addTabsProgressListener(progressListener); | 81 tabbrowser.addTabsProgressListener(progressListener); |
76 } | 82 } |
77 | 83 |
78 function loadSites(backendUrl, parallelTabs, window, sites, callback) | 84 function loadSites(backendUrl, parallelTabs, window, sites, callback) |
79 { | 85 { |
80 while (currentTabs < parallelTabs && sites.length) | 86 while (currentTabs < parallelTabs && sites.length) |
81 { | 87 { |
82 currentTabs++; | 88 currentTabs++; |
83 let site = sites[0]; | 89 let site = sites.shift(); |
84 sites = sites.slice(1); | |
Wladimir Palant
2012/09/21 15:36:18
let site = sites.shift()?
| |
85 loadSite(site, window, function() | 90 loadSite(site, window, function() |
86 { | 91 { |
87 currentTabs--; | 92 currentTabs--; |
88 if (!sites.length && !currentTabs) | 93 if (!sites.length && !currentTabs) |
89 { | 94 { |
90 Storage.finish(); | 95 Storage.finish(); |
91 let dataFilePath = Storage.dataFile.path; | 96 let dataFilePath = Storage.dataFile.path; |
92 Client.sendCrawlerDataFile(backendUrl, window, dataFilePath, function() | 97 Client.sendCrawlerDataFile(backendUrl, dataFilePath, function() |
93 { | 98 { |
94 Storage.destroy(); | 99 Storage.destroy(); |
95 callback(); | 100 callback(); |
96 }); | 101 }); |
97 } | 102 } |
98 else | 103 else |
99 loadSites(backendUrl, parallelTabs, window, sites, callback); | 104 loadSites(backendUrl, parallelTabs, window, sites, callback); |
100 }); | 105 }); |
101 } | 106 } |
102 } | 107 } |
103 | 108 |
104 let Crawler = exports.Crawler = {}; | 109 let Crawler = exports.Crawler = {}; |
105 | 110 |
106 Crawler.crawl = function(backendUrl, parallelTabs, window, callback) | 111 Crawler.crawl = function(backendUrl, parallelTabs, window, callback) |
107 { | 112 { |
108 if (Policy.processNode != origProcessNode) | 113 if (Policy.processNode != origProcessNode) |
109 return; | 114 return; |
110 | 115 |
111 Policy.processNode = processNode; | 116 Policy.processNode = processNode; |
112 | 117 |
113 siteTabs = {}; | 118 siteTabs = new WeakMap(); |
114 currentTabs = 0; | 119 currentTabs = 0; |
115 | 120 |
116 Storage.init(); | 121 Storage.init(); |
117 | 122 |
118 Client.fetchCrawlableSites(backendUrl, function(sites) | 123 Client.fetchCrawlableSites(backendUrl, function(sites) |
119 { | 124 { |
120 loadSites(backendUrl, parallelTabs, window, sites, function() | 125 loadSites(backendUrl, parallelTabs, window, sites, function() |
121 { | 126 { |
122 Policy.processNode = origProcessNode; | 127 Policy.processNode = origProcessNode; |
128 siteTabs = null; | |
123 callback(); | 129 callback(); |
124 }); | 130 }); |
125 }); | 131 }); |
126 }; | 132 }; |
LEFT | RIGHT |