@@ -29,6 +29,26 @@ var switchPlannerEnabledFor = {};
2929// rw / nrw stand for "rewritten" versus "not rewritten"
3030var switchPlannerInfo = { } ;
3131
32+ // Load prefs about whether http nowhere is on. Structure is:
33+ // { httpNowhere: true/false }
34+ var httpNowhereOn = false ;
35+ chrome . storage . sync . get ( { httpNowhere : false } , function ( item ) {
36+ httpNowhereOn = item . httpNowhere ;
37+ setIconColor ( ) ;
38+ } ) ;
39+ chrome . storage . onChanged . addListener ( function ( changes , areaName ) {
40+ if ( areaName === 'sync' ) {
41+ for ( key in changes ) {
42+ if ( key !== 'httpNowhere' ) {
43+ return ;
44+ } else {
45+ httpNowhereOn = changes [ key ] . newValue ;
46+ setIconColor ( ) ;
47+ }
48+ }
49+ }
50+ } ) ;
51+
3252var getStoredUserRules = function ( ) {
3353 var oldUserRuleString = localStorage . getItem ( USER_RULE_KEY ) ;
3454 var oldUserRules = [ ] ;
@@ -48,6 +68,15 @@ var loadStoredUserRules = function() {
4868} ;
4969
5070loadStoredUserRules ( ) ;
71+
72+ // Set the icon color correctly
73+ var setIconColor = function ( ) {
74+ var newIconPath = httpNowhereOn ? './icon38-red.png' : './icon38.png' ;
75+ chrome . browserAction . setIcon ( {
76+ path : newIconPath
77+ } ) ;
78+ }
79+
5180/*
5281for (var v in localStorage) {
5382 log(DBUG, "localStorage["+v+"]: "+localStorage[v]);
@@ -124,6 +153,9 @@ function onBeforeRequest(details) {
124153 // todo: check that this is enough
125154 var uri = new URI ( details . url ) ;
126155
156+ // Should the request be canceled?
157+ var shouldCancel = ( httpNowhereOn && uri . protocol ( ) === 'http' ) ;
158+
127159 // Normalise hosts such as "www.example.com."
128160 var canonical_host = uri . hostname ( ) ;
129161 if ( canonical_host . charAt ( canonical_host . length - 1 ) == "." ) {
@@ -145,7 +177,7 @@ function onBeforeRequest(details) {
145177 " changed before processing to " + canonical_url ) ;
146178 }
147179 if ( canonical_url in urlBlacklist ) {
148- return null ;
180+ return { cancel : shouldCancel } ;
149181 }
150182
151183 if ( details . type == "main_frame" ) {
@@ -162,7 +194,7 @@ function onBeforeRequest(details) {
162194 var hostname = uri . hostname ( ) ;
163195 domainBlacklist [ hostname ] = true ;
164196 log ( WARN , "Domain blacklisted " + hostname ) ;
165- return null ;
197+ return { cancel : shouldCancel } ;
166198 }
167199
168200 var newuristr = null ;
@@ -174,10 +206,11 @@ function onBeforeRequest(details) {
174206 }
175207 }
176208
209+ var finaluri = newuristr ? new URI ( newuristr ) : null ;
210+
177211 if ( newuristr && tmpuserinfo !== "" ) {
178212 // re-insert userpass info which was stripped temporarily
179213 // while rules were applied
180- var finaluri = new URI ( newuristr ) ;
181214 finaluri . userinfo ( tmpuserinfo ) ;
182215 newuristr = finaluri . toString ( ) ;
183216 }
@@ -192,20 +225,25 @@ function onBeforeRequest(details) {
192225 newuristr ) ;
193226 }
194227
228+ if ( httpNowhereOn ) {
229+ if ( finaluri && finaluri . protocol ( ) === "http" ) {
230+ // Abort early if we're about to redirect to HTTP in HTTP Nowhere mode
231+ return { cancel : true } ;
232+ }
233+ }
234+
195235 if ( newuristr ) {
196- log ( DBUG , "Redirecting from " + details . url + " to " + newuristr ) ;
197236 return { redirectUrl : newuristr } ;
198237 } else {
199- return null ;
238+ return { cancel : shouldCancel } ;
200239 }
201240}
202241
203242
204243// Map of which values for the `type' enum denote active vs passive content.
205244// https://developer.chrome.com/extensions/webRequest.html#event-onBeforeRequest
206245var activeTypes = { stylesheet : 1 , script : 1 , object : 1 , other : 1 } ;
207- // We consider sub_frame to be passive even though it can contain JS or Flash.
208- // This is because code running the sub_frame cannot access the main frame's
246+
209247// content, by same-origin policy. This is true even if the sub_frame is on the
210248// same domain but different protocol - i.e. HTTP while the parent is HTTPS -
211249// because same-origin policy includes the protocol. This also mimics Chrome's
@@ -402,27 +440,6 @@ wr.onBeforeRequest.addListener(onBeforeRequest, {urls: ["https://*/*", "http://*
402440wr . onBeforeRedirect . addListener ( onBeforeRedirect , { urls : [ "https://*/*" ] } ) ;
403441
404442
405- // Add the small HTTPS Everywhere icon in the address bar.
406- // Note: We can't use any other hook (onCreated, onActivated, etc.) because Chrome resets the
407- // pageActions on URL change. We should strongly consider switching from pageAction to browserAction.
408- chrome . tabs . onUpdated . addListener ( function ( tabId , changeInfo , tab ) {
409- if ( changeInfo . status === "loading" ) {
410- chrome . pageAction . show ( tabId ) ;
411- }
412- } ) ;
413-
414- // Pre-rendered tabs / instant experiments sometimes skip onUpdated.
415- // See http://crbug.com/109557
416- chrome . tabs . onReplaced . addListener ( function ( addedTabId , removedTabId ) {
417- chrome . tabs . get ( addedTabId , function ( tab ) {
418- if ( typeof ( tab ) === "undefined" ) {
419- log ( DBUG , "Not a real tab. Skipping showing pageAction." ) ;
420- } else {
421- chrome . pageAction . show ( addedTabId ) ;
422- }
423- } ) ;
424- } ) ;
425-
426443// Listen for cookies set/updated and secure them if applicable. This function is async/nonblocking.
427444chrome . cookies . onChanged . addListener ( onCookieChanged ) ;
428445
0 commit comments