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

Unified Diff: background.js

Issue 5095280043098112: Issue 375 - Make context menus per page (Closed)
Patch Set: Created April 24, 2014, 1:22 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/ext/background.js » ('j') | chrome/ext/background.js » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: background.js
===================================================================
--- a/background.js
+++ b/background.js
@@ -78,12 +78,7 @@
// update browser actions when whitelisting might have changed,
// due to loading filters or saving filter changes
if (action == "load" || action == "save")
- {
- ext.pages.query({}, function(pages)
- {
- pages.forEach(refreshIconAndContextMenu);
- });
- }
+ refreshIconAndContextMenuForAllPages();
});
// Special-case domains for which we cannot use style-based hiding rules.
@@ -105,6 +100,16 @@
var activeNotification = null;
+var contextMenuItem = {
+ title: ext.i18n.getMessage("block_element"),
+ contexts: ["image", "video", "audio"],
+ onclick: function(srcUrl, page)
+ {
+ if (srcUrl)
+ page.sendMessage({type: "clickhide-new-filter", filter: srcUrl});
+ }
+};
+
// Adds or removes browser action icon according to options.
function refreshIconAndContextMenu(page)
{
@@ -124,10 +129,18 @@
// show or hide the context menu entry dependent on whether
// adblocking is active on that page
- if (whitelisted || !/^https?:/.test(page.url))
- ext.contextMenus.hideMenuItems();
- else
- ext.contextMenus.showMenuItems();
+ page.contextMenus.removeAll();
+
+ if (Prefs.shouldShowBlockElementMenu && !whitelisted && /^https?:/.test(page.url))
+ page.contextMenus.create(contextMenuItem);
+}
+
+function refreshIconAndContextMenuForAllPages()
+{
+ ext.pages.query({}, function(pages)
+ {
+ pages.forEach(refreshIconAndContextMenu);
+ });
}
/**
@@ -252,27 +265,11 @@
notifyUser();
}
-function setContextMenu()
-{
- if (Prefs.shouldShowBlockElementMenu)
- {
- // Register context menu item
- ext.contextMenus.addMenuItem(ext.i18n.getMessage("block_element"), ["image", "video", "audio"], function(srcUrl, page)
- {
- if (srcUrl)
- page.sendMessage({type: "clickhide-new-filter", filter: srcUrl});
- });
- }
- else
- ext.contextMenus.removeMenuItems();
-}
-
Prefs.addListener(function(name)
{
if (name == "shouldShowBlockElementMenu")
- setContextMenu();
+ refreshIconAndContextMenuForAllPages();
});
-setContextMenu();
/**
* Opens options page or focuses an existing one, within the last focused window.
« 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