Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: background.js

Issue 5095280043098112: Issue 375 - Make context menus per page (Closed)
Patch Set: Created April 24, 2014, 1:22 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/ext/background.js » ('j') | chrome/ext/background.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 } 71 }
72 72
73 if (canUseChromeNotifications) 73 if (canUseChromeNotifications)
74 initChromeNotifications(); 74 initChromeNotifications();
75 initAntiAdblockNotification(); 75 initAntiAdblockNotification();
76 } 76 }
77 77
78 // update browser actions when whitelisting might have changed, 78 // update browser actions when whitelisting might have changed,
79 // due to loading filters or saving filter changes 79 // due to loading filters or saving filter changes
80 if (action == "load" || action == "save") 80 if (action == "load" || action == "save")
81 { 81 refreshIconAndContextMenuForAllPages();
82 ext.pages.query({}, function(pages)
83 {
84 pages.forEach(refreshIconAndContextMenu);
85 });
86 }
87 }); 82 });
88 83
89 // Special-case domains for which we cannot use style-based hiding rules. 84 // Special-case domains for which we cannot use style-based hiding rules.
90 // See http://crbug.com/68705. 85 // See http://crbug.com/68705.
91 var noStyleRulesHosts = ["mail.google.com", "mail.yahoo.com", "www.google.com"]; 86 var noStyleRulesHosts = ["mail.google.com", "mail.yahoo.com", "www.google.com"];
92 87
93 function removeDeprecatedOptions() 88 function removeDeprecatedOptions()
94 { 89 {
95 var deprecatedOptions = ["specialCaseYouTube", "experimental", "disableInlineT extAds"]; 90 var deprecatedOptions = ["specialCaseYouTube", "experimental", "disableInlineT extAds"];
96 deprecatedOptions.forEach(function(option) 91 deprecatedOptions.forEach(function(option)
97 { 92 {
98 if (option in ext.storage) 93 if (option in ext.storage)
99 delete ext.storage[option]; 94 delete ext.storage[option];
100 }); 95 });
101 } 96 }
102 97
103 // Remove deprecated options before we do anything else. 98 // Remove deprecated options before we do anything else.
104 removeDeprecatedOptions(); 99 removeDeprecatedOptions();
105 100
106 var activeNotification = null; 101 var activeNotification = null;
107 102
103 var contextMenuItem = {
104 title: ext.i18n.getMessage("block_element"),
105 contexts: ["image", "video", "audio"],
106 onclick: function(srcUrl, page)
107 {
108 if (srcUrl)
109 page.sendMessage({type: "clickhide-new-filter", filter: srcUrl});
110 }
111 };
112
108 // Adds or removes browser action icon according to options. 113 // Adds or removes browser action icon according to options.
109 function refreshIconAndContextMenu(page) 114 function refreshIconAndContextMenu(page)
110 { 115 {
111 var whitelisted = isWhitelisted(page.url); 116 var whitelisted = isWhitelisted(page.url);
112 117
113 var iconFilename; 118 var iconFilename;
114 if (whitelisted && require("info").platform != "safari") 119 if (whitelisted && require("info").platform != "safari")
115 // There is no grayscale version of the icon for whitelisted pages 120 // There is no grayscale version of the icon for whitelisted pages
116 // when using Safari, because icons are grayscale already and icons 121 // when using Safari, because icons are grayscale already and icons
117 // aren't per page in Safari. 122 // aren't per page in Safari.
118 iconFilename = "icons/abp-$size-whitelisted.png"; 123 iconFilename = "icons/abp-$size-whitelisted.png";
119 else 124 else
120 iconFilename = "icons/abp-$size.png"; 125 iconFilename = "icons/abp-$size.png";
121 126
122 page.browserAction.setIcon(iconFilename); 127 page.browserAction.setIcon(iconFilename);
123 iconAnimation.registerPage(page, iconFilename); 128 iconAnimation.registerPage(page, iconFilename);
124 129
125 // show or hide the context menu entry dependent on whether 130 // show or hide the context menu entry dependent on whether
126 // adblocking is active on that page 131 // adblocking is active on that page
127 if (whitelisted || !/^https?:/.test(page.url)) 132 page.contextMenus.removeAll();
128 ext.contextMenus.hideMenuItems(); 133
129 else 134 if (Prefs.shouldShowBlockElementMenu && !whitelisted && /^https?:/.test(page.u rl))
130 ext.contextMenus.showMenuItems(); 135 page.contextMenus.create(contextMenuItem);
136 }
137
138 function refreshIconAndContextMenuForAllPages()
139 {
140 ext.pages.query({}, function(pages)
141 {
142 pages.forEach(refreshIconAndContextMenu);
143 });
131 } 144 }
132 145
133 /** 146 /**
134 * Old versions for Opera stored patterns.ini in the localStorage object, this 147 * Old versions for Opera stored patterns.ini in the localStorage object, this
135 * will import it into FilterStorage properly. 148 * will import it into FilterStorage properly.
136 * @return {Boolean} true if data import is in progress 149 * @return {Boolean} true if data import is in progress
137 */ 150 */
138 function importOldData() 151 function importOldData()
139 { 152 {
140 if ("patterns.ini" in localStorage) 153 if ("patterns.ini" in localStorage)
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
245 258
246 notifyUser(); 259 notifyUser();
247 } 260 }
248 }, false); 261 }, false);
249 request.send(null); 262 request.send(null);
250 } 263 }
251 else 264 else
252 notifyUser(); 265 notifyUser();
253 } 266 }
254 267
255 function setContextMenu()
256 {
257 if (Prefs.shouldShowBlockElementMenu)
258 {
259 // Register context menu item
260 ext.contextMenus.addMenuItem(ext.i18n.getMessage("block_element"), ["image", "video", "audio"], function(srcUrl, page)
261 {
262 if (srcUrl)
263 page.sendMessage({type: "clickhide-new-filter", filter: srcUrl});
264 });
265 }
266 else
267 ext.contextMenus.removeMenuItems();
268 }
269
270 Prefs.addListener(function(name) 268 Prefs.addListener(function(name)
271 { 269 {
272 if (name == "shouldShowBlockElementMenu") 270 if (name == "shouldShowBlockElementMenu")
273 setContextMenu(); 271 refreshIconAndContextMenuForAllPages();
274 }); 272 });
275 setContextMenu();
276 273
277 /** 274 /**
278 * Opens options page or focuses an existing one, within the last focused windo w. 275 * Opens options page or focuses an existing one, within the last focused windo w.
279 * @param {Function} callback function to be called with the 276 * @param {Function} callback function to be called with the
280 Page object of the options page 277 Page object of the options page
281 */ 278 */
282 function openOptions(callback) 279 function openOptions(callback)
283 { 280 {
284 ext.pages.query({lastFocusedWindow: true}, function(pages) 281 ext.pages.query({lastFocusedWindow: true}, function(pages)
285 { 282 {
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after
598 page.sendMessage({type: "clickhide-deactivate"}); 595 page.sendMessage({type: "clickhide-deactivate"});
599 refreshIconAndContextMenu(page); 596 refreshIconAndContextMenu(page);
600 }); 597 });
601 598
602 setTimeout(function() 599 setTimeout(function()
603 { 600 {
604 var notificationToShow = Notification.getNextToShow(); 601 var notificationToShow = Notification.getNextToShow();
605 if (notificationToShow) 602 if (notificationToShow)
606 showNotification(notificationToShow); 603 showNotification(notificationToShow);
607 }, 3 * 60 * 1000); 604 }, 3 * 60 * 1000);
OLDNEW
« no previous file with comments | « no previous file | chrome/ext/background.js » ('j') | chrome/ext/background.js » ('J')

Powered by Google App Engine
This is Rietveld