1+ importScripts ( 'utils.js' ) ;
2+
3+ // Initialize global variables
4+ var data = {
5+ lastVersionRun : null ,
6+ readOnly : [ ] ,
7+ filters : [ ] ,
8+ nCookiesProtected : 0 ,
9+ nCookiesFlagged : 0 ,
10+ nCookiesShortened : 0
11+ } ;
12+
13+ // Initialize global preferences
14+ var preferences = {
15+ showContextMenu : true ,
16+ showChristmasIcon : false ,
17+ useMaxCookieAge : false ,
18+ maxCookieAgeType : 0
19+ } ;
20+
121var showContextMenu = undefined ;
222
3- updateCallback = function ( ) {
23+ // Load preferences and data from Chrome storage
24+ chrome . storage . local . get ( [ 'lastVersionRun' , 'readOnly' , 'filters' , 'preferences' ] , function ( items ) {
25+ data . lastVersionRun = items . lastVersionRun || null ;
26+ data . readOnly = items . readOnly || [ ] ;
27+ data . filters = items . filters || [ ] ;
28+ preferences = items . preferences || preferences ;
29+
30+ showContextMenu = preferences . showContextMenu ;
31+
32+ var currentVersion = chrome . runtime . getManifest ( ) . version ;
33+ var oldVersion = data . lastVersionRun ;
34+
35+ data . lastVersionRun = currentVersion ;
36+ chrome . storage . local . set ( { lastVersionRun : currentVersion } ) ;
37+
38+ if ( oldVersion !== currentVersion ) {
39+ if ( oldVersion === null || oldVersion === undefined ) {
40+ // Is firstrun
41+ chrome . tabs . create ( { url : 'http://www.editthiscookie.com/start/' } ) ;
42+ } else {
43+ chrome . notifications . onClicked . addListener ( function ( notificationId ) {
44+ chrome . tabs . create ( {
45+ url : 'http://www.editthiscookie.com/changelog/'
46+ } ) ;
47+ chrome . notifications . clear ( notificationId , function ( ) { } ) ;
48+ } ) ;
49+ var opt = {
50+ type : "basic" ,
51+ title : "EditThisCookie" ,
52+ message : _getMessage ( "updated" ) ,
53+ iconUrl : "/img/icon_128x128.png" ,
54+ isClickable : true
55+ } ;
56+ chrome . notifications . create ( "" , opt , function ( ) { } ) ;
57+ }
58+ }
59+
60+ updateCallback ( ) ;
61+ } ) ;
62+
63+ function updateCallback ( ) {
464 if ( showContextMenu !== preferences . showContextMenu ) {
565 showContextMenu = preferences . showContextMenu ;
666 setContextMenu ( showContextMenu ) ;
767 }
868 setChristmasIcon ( ) ;
9- } ;
10-
11- setChristmasIcon ( ) ;
12- setInterval ( setChristmasIcon , 60 * 60 * 1000 ) ; //Every hour
13-
14- //Every time the browser restarts the first time the user goes to the options he ends up in the default page (support)
15- localStorage . setItem ( "option_panel" , "null" ) ;
69+ }
1670
17- var currentVersion = chrome . runtime . getManifest ( ) . version ;
18- var oldVersion = data . lastVersionRun ;
71+ function setChristmasIcon ( ) {
72+ if ( isChristmasPeriod ( ) && preferences . showChristmasIcon ) {
73+ chrome . action . setIcon ( { path : "/img/cookie_xmas_19x19.png" } ) ;
74+ } else {
75+ chrome . action . setIcon ( { path : "/img/icon_19x19.png" } ) ;
76+ }
77+ }
1978
20- data . lastVersionRun = currentVersion ;
79+ setChristmasIcon ( ) ;
80+ setInterval ( setChristmasIcon , 60 * 60 * 1000 ) ;
2181
22- if ( oldVersion !== currentVersion ) {
23- if ( oldVersion === undefined ) { //Is firstrun
24- chrome . tabs . create ( { url : 'http://www.editthiscookie.com/start/' } ) ;
82+ // Every time the browser restarts, the first time the user goes to the options he ends up in the default page (support)
83+ chrome . storage . local . set ( { option_panel : "null" } , function ( ) {
84+ if ( chrome . runtime . lastError ) {
85+ console . error ( "Error setting option_panel: " , chrome . runtime . lastError ) ;
2586 } else {
26- chrome . notifications . onClicked . addListener ( function ( notificationId ) {
27- chrome . tabs . create ( {
28- url : 'http://www.editthiscookie.com/changelog/'
29- } ) ;
30- chrome . notifications . clear ( notificationId , function ( wasCleared ) { } ) ;
31- } ) ;
32- var opt = {
33- type : "basic" ,
34- title : "EditThisCookie" ,
35- message : _getMessage ( "updated" ) ,
36- iconUrl : "/img/icon_128x128.png" ,
37- isClickable : true
38- } ;
39- chrome . notifications . create ( "" , opt , function ( notificationId ) {
40- } ) ;
87+ console . log ( "option_panel set to null" ) ;
4188 }
42- }
89+ } ) ;
4390
4491setContextMenu ( preferences . showContextMenu ) ;
4592
@@ -48,24 +95,18 @@ chrome.cookies.onChanged.addListener(function (changeInfo) {
4895 var cookie = changeInfo . cookie ;
4996 var cause = changeInfo . cause ;
5097
51- var name = cookie . name ;
52- var domain = cookie . domain ;
53- var value = cookie . value ;
54-
55- if ( cause === "expired" || cause === "evicted" )
56- return ;
98+ if ( cause === "expired" || cause === "evicted" ) return ;
5799
58100 for ( var i = 0 ; i < data . readOnly . length ; i ++ ) {
59101 var currentRORule = data . readOnly [ i ] ;
60102 if ( compareCookies ( cookie , currentRORule ) ) {
61103 if ( removed ) {
62104 chrome . cookies . get ( {
63- ' url' : "http" + ( ( currentRORule . secure ) ? "s" : "" ) + "://" + currentRORule . domain + currentRORule . path ,
64- ' name' : currentRORule . name ,
65- ' storeId' : currentRORule . storeId
105+ url : "http" + ( ( currentRORule . secure ) ? "s" : "" ) + "://" + currentRORule . domain + currentRORule . path ,
106+ name : currentRORule . name ,
107+ storeId : currentRORule . storeId
66108 } , function ( currentCookie ) {
67- if ( compareCookies ( currentCookie , currentRORule ) )
68- return ;
109+ if ( compareCookies ( currentCookie , currentRORule ) ) return ;
69110 var newCookie = cookieForCreationFromFullCookie ( currentRORule ) ;
70111 chrome . cookies . set ( newCookie ) ;
71112 ++ data . nCookiesProtected ;
@@ -75,55 +116,54 @@ chrome.cookies.onChanged.addListener(function (changeInfo) {
75116 }
76117 }
77118
78- //Check if a blocked cookie was added
79119 if ( ! removed ) {
80120 for ( var i = 0 ; i < data . filters . length ; i ++ ) {
81121 var currentFilter = data . filters [ i ] ;
82- if ( filterMatchesCookie ( currentFilter , name , domain , value ) ) {
83- chrome . tabs . query (
84- { active : true } ,
85- function ( tabs ) {
86- var url = tabs [ 0 ] . url ;
87- var toRemove = { } ;
88- toRemove . url = url ;
89- toRemove . url = "http" + ( ( cookie . secure ) ? "s" : "" ) + "://" + cookie . domain + cookie . path ;
90- toRemove . name = name ;
91- chrome . cookies . remove ( toRemove ) ;
92- ++ data . nCookiesFlagged ;
93- } ) ;
122+ if ( filterMatchesCookie ( currentFilter , cookie . name , cookie . domain , cookie . value ) ) {
123+ chrome . tabs . query ( { active : true } , function ( tabs ) {
124+ var toRemove = {
125+ url : "http" + ( cookie . secure ? "s" : "" ) + "://" + cookie . domain + cookie . path ,
126+ name : cookie . name
127+ } ;
128+ chrome . cookies . remove ( toRemove ) ;
129+ ++ data . nCookiesFlagged ;
130+ } ) ;
94131 }
95132 }
96133 }
97134
98- if ( ! removed && preferences . useMaxCookieAge && preferences . maxCookieAgeType > 0 ) { //Check expiration, if too far in the future shorten on user's preference
99- var maxAllowedExpiration = Math . round ( ( new Date ) . getTime ( ) / 1000 ) + ( preferences . maxCookieAge * preferences . maxCookieAgeType ) ;
135+ if ( ! removed && preferences . useMaxCookieAge && preferences . maxCookieAgeType > 0 ) {
136+ var maxAllowedExpiration = Math . round ( Date . now ( ) / 1000 ) + ( preferences . maxCookieAge * preferences . maxCookieAgeType ) ;
100137 if ( cookie . expirationDate !== undefined && cookie . expirationDate > maxAllowedExpiration + 60 ) {
101138 var newCookie = cookieForCreationFromFullCookie ( cookie ) ;
102- if ( ! cookie . session )
103- newCookie . expirationDate = maxAllowedExpiration ;
139+ if ( ! cookie . session ) newCookie . expirationDate = maxAllowedExpiration ;
104140 chrome . cookies . set ( newCookie ) ;
105141 ++ data . nCookiesShortened ;
106142 }
107143 }
108144} ) ;
109145
110146function setContextMenu ( show ) {
111- chrome . contextMenus . removeAll ( ) ;
112- if ( show ) {
113- chrome . contextMenus . create ( {
114- "title" : "EditThisCookie" ,
115- "contexts" : [ "page" ] ,
116- "onclick" : function ( info , tab ) {
117- showPopup ( info , tab ) ;
118- }
119- } ) ;
120- }
121- }
147+ chrome . contextMenus . removeAll ( function ( ) {
148+ if ( chrome . runtime . lastError ) {
149+ console . error ( "Error removing context menus: " , chrome . runtime . lastError ) ;
150+ } else {
151+ console . log ( "Context menus removed" ) ;
152+ }
122153
123- function setChristmasIcon ( ) {
124- if ( isChristmasPeriod ( ) && preferences . showChristmasIcon ) {
125- chrome . browserAction . setIcon ( { "path" : "/img/cookie_xmas_19x19.png" } ) ;
126- } else {
127- chrome . browserAction . setIcon ( { "path" : "/img/icon_19x19.png" } ) ;
128- }
154+ if ( show ) {
155+ chrome . contextMenus . create ( {
156+ id : "editThisCookie" , // Unique identifier for the context menu item
157+ title : "EditThisCookie" ,
158+ contexts : [ "page" ]
159+ } , function ( ) {
160+ if ( chrome . runtime . lastError ) {
161+ console . error ( "Error creating context menu: " , chrome . runtime . lastError ) ;
162+ } else {
163+ console . log ( "Context menu created" ) ;
164+ }
165+ } ) ;
166+ }
167+ } ) ;
129168}
169+
0 commit comments