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

Side by Side Diff: lib/ui.js

Issue 5538776168267776: Implemented anti-adblock message notification (Closed)
Patch Set: Created Feb. 13, 2014, 12:50 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 | « lib/notification.js ('k') | no next file » | no next file with comments »
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-2013 Eyeo GmbH 3 * Copyright (C) 2006-2013 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 18 Cu.import("resource://gre/modules/XPCOMUtils.jsm");
19 Cu.import("resource://gre/modules/Services.jsm"); 19 Cu.import("resource://gre/modules/Services.jsm");
20 20
21 let {Utils} = require("utils"); 21 let {Utils} = require("utils");
22 let {Prefs} = require("prefs"); 22 let {Prefs} = require("prefs");
23 let {Policy} = require("contentPolicy"); 23 let {Policy} = require("contentPolicy");
24 let {FilterListener} = require("filterListener"); 24 let {FilterListener} = require("filterListener");
25 let {FilterStorage} = require("filterStorage"); 25 let {FilterStorage} = require("filterStorage");
26 let {FilterNotifier} = require("filterNotifier"); 26 let {FilterNotifier} = require("filterNotifier");
27 let {RequestNotifier} = require("requestNotifier"); 27 let {RequestNotifier} = require("requestNotifier");
28 let {Filter} = require("filterClasses"); 28 let {Filter, ActiveFilter} = require("filterClasses");
29 let {Subscription, SpecialSubscription, DownloadableSubscription} = require("sub scriptionClasses"); 29 let {Subscription, SpecialSubscription, DownloadableSubscription} = require("sub scriptionClasses");
30 let {Synchronizer} = require("synchronizer"); 30 let {Synchronizer} = require("synchronizer");
31 let {KeySelector} = require("keySelector"); 31 let {KeySelector} = require("keySelector");
32 let {Notification} = require("notification"); 32 let {Notification} = require("notification");
33 33
34 let CustomizableUI; 34 let CustomizableUI;
35 try 35 try
36 { 36 {
37 ({CustomizableUI}) = Cu.import("resource:///modules/CustomizableUI.jsm", null) ; 37 ({CustomizableUI}) = Cu.import("resource:///modules/CustomizableUI.jsm", null) ;
38 } 38 }
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 391
392 if (this.overlay.all.length) 392 if (this.overlay.all.length)
393 this.overlay.all[0].appendChild(menuSource); 393 this.overlay.all[0].appendChild(menuSource);
394 if ("abp-toolbarbutton" in this.overlay) 394 if ("abp-toolbarbutton" in this.overlay)
395 this.overlay["abp-toolbarbutton"].appendChild(fixId(menuSource.cloneNode (true), "abp-toolbar")); 395 this.overlay["abp-toolbarbutton"].appendChild(fixId(menuSource.cloneNode (true), "abp-toolbar"));
396 if ("abp-menuitem" in this.overlay) 396 if ("abp-menuitem" in this.overlay)
397 this.overlay["abp-menuitem"].appendChild(fixId(menuSource.cloneNode(true ), "abp-menuitem")); 397 this.overlay["abp-menuitem"].appendChild(fixId(menuSource.cloneNode(true ), "abp-menuitem"));
398 } 398 }
399 }, 399 },
400 400
401 _initAntiAdblockNotification: function()
402 {
403 let notification = {
404 id: "antiadblock",
405 type: "question",
406 title: Utils.getString("notification_antiadblock_title"),
407 message: Utils.getString("notification_antiadblock_message"),
408 urlFilters: []
409 };
410
411 function notificationListener(approved)
412 {
413 let subscription = Subscription.fromURL(Prefs.subscriptions_antiadblockurl );
414 if (subscription.url in FilterStorage.knownSubscriptions)
415 subscription.disabled = !approved;
416 }
417
418 function addAntiAdblockNotification(subscription)
419 {
420 let urlFilters = [];
421 for each (let filter in subscription.filters)
422 if (filter instanceof ActiveFilter)
423 for (let domain in filter.domains)
424 if (domain && urlFilters.indexOf(domain) == -1)
425 urlFilters.push(domain);
426 notification.urlFilters = urlFilters;
427 Notification.addNotification(notification);
428 Notification.addQuestionListener(notification.id, notificationListener);
429 }
430
431 function removeAntiAdblockNotification()
432 {
433 Notification.removeNotification(notification);
434 Notification.removeQuestionListener(notification.id, notificationListener) ;
435 }
436
437 let subscription = Subscription.fromURL(Prefs.subscriptions_antiadblockurl);
438 if (subscription.lastDownload && subscription.disabled)
439 addAntiAdblockNotification(subscription);
440
441 FilterNotifier.addListener(function(action, value, newItem, oldItem)
442 {
443 if (!/^subscription\.(updated|removed|disabled)$/.test(action) || value.ur l != Prefs.subscriptions_antiadblockurl)
444 return;
445
446 if (action == "subscription.updated")
447 addAntiAdblockNotification(value);
448 else if (action == "subscription.removed" || (action == "subscription.disa bled" && !value.disabled))
449 removeAntiAdblockNotification();
450 });
451
452 var documentCreationObserver = {
453 observe: function(subject, topic, data)
454 {
455 if (!(subject instanceof Ci.nsIDOMWindow))
456 return;
457
458 this._showNextNotification(subject.location.href);
459 }.bind(this)
460 };
461 Services.obs.addObserver(documentCreationObserver, "content-document-global- created", false);
462 onShutdown.add(function()
463 {
464 Services.obs.removeObserver(documentCreationObserver, "content-document-gl obal-created", false);
465 });
466 },
467
401 /** 468 /**
402 * Gets called once the initialization is finished and Adblock Plus elements 469 * Gets called once the initialization is finished and Adblock Plus elements
403 * can be added to the UI. 470 * can be added to the UI.
404 */ 471 */
405 initDone: function() 472 initDone: function()
406 { 473 {
407 let {WindowObserver} = require("windowObserver"); 474 let {WindowObserver} = require("windowObserver");
408 new WindowObserver(this); 475 new WindowObserver(this);
409 476
410 // Add toolbar icon 477 // Add toolbar icon
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
518 window.addEventListener("popupshowing", this.onPopupShowing, false); 585 window.addEventListener("popupshowing", this.onPopupShowing, false);
519 window.addEventListener("keypress", this.onKeyPress, false); 586 window.addEventListener("keypress", this.onKeyPress, false);
520 587
521 addBrowserLocationListener(window, function() 588 addBrowserLocationListener(window, function()
522 { 589 {
523 this.updateIconState(window, window.document.getElementById("abp-status")) ; 590 this.updateIconState(window, window.document.getElementById("abp-status")) ;
524 this.updateIconState(window, window.document.getElementById("abp-toolbarbu tton")); 591 this.updateIconState(window, window.document.getElementById("abp-toolbarbu tton"));
525 }.bind(this)); 592 }.bind(this));
526 addBrowserClickListener(window, this.onBrowserClick.bind(this, window)); 593 addBrowserClickListener(window, this.onBrowserClick.bind(this, window));
527 594
595 window.document.getElementById("abp-notification-close").addEventListener("c ommand", function(event)
596 {
597 window.document.getElementById("abp-notification").hidePopup();
598 }, false);
599
600 // Add "anti-adblock messages" notification
601 this._initAntiAdblockNotification();
602
528 // First-run actions? 603 // First-run actions?
529 if (!this.firstRunDone) 604 if (!this.firstRunDone)
530 { 605 {
531 this.firstRunDone = true; 606 this.firstRunDone = true;
532 607
533 let {addonVersion} = require("info"); 608 let {addonVersion} = require("info");
534 let prevVersion = Prefs.currentVersion; 609 let prevVersion = Prefs.currentVersion;
535 if (prevVersion != addonVersion) 610 if (prevVersion != addonVersion)
536 { 611 {
537 Prefs.currentVersion = addonVersion; 612 Prefs.currentVersion = addonVersion;
(...skipping 225 matching lines...) Expand 10 before | Expand all | Expand 10 after
763 { 838 {
764 subscription.title = "Allow non-intrusive advertising"; 839 subscription.title = "Allow non-intrusive advertising";
765 FilterStorage.addSubscription(subscription); 840 FilterStorage.addSubscription(subscription);
766 if (subscription instanceof DownloadableSubscription && !subscription.la stDownload) 841 if (subscription instanceof DownloadableSubscription && !subscription.la stDownload)
767 Synchronizer.execute(subscription); 842 Synchronizer.execute(subscription);
768 } 843 }
769 else 844 else
770 addAcceptable = false; 845 addAcceptable = false;
771 } 846 }
772 847
848 // Add "anti-adblock messages" subscription
849 let subscription = Subscription.fromURL(Prefs.subscriptions_antiadblockurl);
850 if (subscription)
851 {
852 subscription.disabled = true;
853 FilterStorage.addSubscription(subscription);
854 if (subscription instanceof DownloadableSubscription && !subscription.last Download)
855 Synchronizer.execute(subscription);
856 }
857
773 if (!addSubscription && !addAcceptable) 858 if (!addSubscription && !addAcceptable)
774 return; 859 return;
775 860
776 function notifyUser() 861 function notifyUser()
777 { 862 {
778 let {addTab} = require("appSupport"); 863 let {addTab} = require("appSupport");
779 if (addTab) 864 if (addTab)
780 { 865 {
781 addTab(window, "chrome://adblockplus/content/ui/firstRun.html"); 866 addTab(window, "chrome://adblockplus/content/ui/firstRun.html");
782 } 867 }
(...skipping 975 matching lines...) Expand 10 before | Expand all | Expand 10 after
1758 Prefs.hideContributeButton = true; 1843 Prefs.hideContributeButton = true;
1759 1844
1760 for each (let id in ["abp-status-contributebutton", "abp-toolbar-contributeb utton", "abp-menuitem-contributebutton"]) 1845 for each (let id in ["abp-status-contributebutton", "abp-toolbar-contributeb utton", "abp-menuitem-contributebutton"])
1761 { 1846 {
1762 let button = window.document.getElementById(id); 1847 let button = window.document.getElementById(id);
1763 if (button) 1848 if (button)
1764 button.hidden = true; 1849 button.hidden = true;
1765 } 1850 }
1766 }, 1851 },
1767 1852
1768 _showNextNotification: function() 1853 _showNextNotification: function(url)
1769 { 1854 {
1770 let window = this.currentWindow; 1855 let window = this.currentWindow;
1771 if (!window) 1856 if (!window)
1772 return; 1857 return;
1773 1858
1774 let button = window.document.getElementById("abp-toolbarbutton") 1859 let button = window.document.getElementById("abp-toolbarbutton")
1775 || window.document.getElementById("abp-status"); 1860 || window.document.getElementById("abp-status");
1776 if (!button) 1861 if (!button)
1777 return; 1862 return;
1778 1863
1779 let notification = Notification.getNextToShow(); 1864 let notification = Notification.getNextToShow(url);
1780 if (!notification) 1865 if (!notification)
1781 return; 1866 return;
1782 1867
1783 this._showNotification(window, button, notification); 1868 this._showNotification(window, button, notification);
1784 }, 1869 },
1785 1870
1786 _showNotification: function(window, button, notification) 1871 _showNotification: function(window, button, notification)
1787 { 1872 {
1873 let panel = window.document.getElementById("abp-notification");
1874 if (panel.state !== "closed")
1875 return;
1876
1788 function insertMessage(element, text, links) 1877 function insertMessage(element, text, links)
1789 { 1878 {
1790 let match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(text); 1879 let match = /^(.*?)<(a|strong)>(.*?)<\/\2>(.*)$/.exec(text);
1791 if (!match) 1880 if (!match)
1792 { 1881 {
1793 element.appendChild(window.document.createTextNode(text)); 1882 element.appendChild(window.document.createTextNode(text));
1794 return; 1883 return;
1795 } 1884 }
1796 1885
1797 let [_, before, tagName, value, after] = match; 1886 let [_, before, tagName, value, after] = match;
1798 1887
1799 insertMessage(element, before, links); 1888 insertMessage(element, before, links);
1800 1889
1801 let newElement = window.document.createElementNS("http://www.w3.org/1999/x html", tagName); 1890 let newElement = window.document.createElementNS("http://www.w3.org/1999/x html", tagName);
1802 if (tagName === "a" && links && links.length) 1891 if (tagName === "a" && links && links.length)
1803 newElement.setAttribute("href", links.shift()); 1892 newElement.setAttribute("href", links.shift());
1804 insertMessage(newElement, value, links); 1893 insertMessage(newElement, value, links);
1805 element.appendChild(newElement); 1894 element.appendChild(newElement);
1806 1895
1807 insertMessage(element, after, links); 1896 insertMessage(element, after, links);
1808 } 1897 }
1809 1898
1810 let texts = Notification.getLocalizedTexts(notification); 1899 let texts = Notification.getLocalizedTexts(notification);
1811 let titleElement = window.document.getElementById("abp-notification-title"); 1900 let titleElement = window.document.getElementById("abp-notification-title");
1812 titleElement.setAttribute("value", texts.title); 1901 titleElement.setAttribute("value", texts.title);
1813 let messageElement = window.document.getElementById("abp-notification-messag e"); 1902 let messageElement = window.document.getElementById("abp-notification-messag e");
1903 messageElement.innerHTML = "";
1814 let docLinks = []; 1904 let docLinks = [];
1815 for each (let link in notification.links) 1905 for each (let link in notification.links)
1816 docLinks.push(Utils.getDocLink(link)); 1906 docLinks.push(Utils.getDocLink(link));
1817 insertMessage(messageElement, texts.message, docLinks); 1907 insertMessage(messageElement, texts.message, docLinks);
1818 1908
1819 messageElement.addEventListener("click", function(event) 1909 messageElement.addEventListener("click", function(event)
1820 { 1910 {
1821 let link = event.target; 1911 let link = event.target;
1822 while (link && link !== messageElement && link.localName !== "a") 1912 while (link && link !== messageElement && link.localName !== "a")
1823 link = link.parentNode; 1913 link = link.parentNode;
1824 if (!link) 1914 if (!link || link.localName !== "a")
1825 return; 1915 return;
1826 event.preventDefault(); 1916 event.preventDefault();
1827 event.stopPropagation(); 1917 event.stopPropagation();
1828 this.loadInBrowser(link.href, window); 1918 this.loadInBrowser(link.href, window);
1829 }.bind(this)); 1919 }.bind(this));
1830 1920
1831 let panel = window.document.getElementById("abp-notification"); 1921 if (notification.type === "question")
1922 {
1923 function buttonHandler(approved, event)
1924 {
1925 event.preventDefault();
1926 event.stopPropagation();
1927 panel.hidePopup();
1928 Notification.triggerQuestionListeners(notification.id, approved)
1929 Notification.markAsShown(notification.id);
1930 }
1931 window.document.getElementById("abp-notification-yes").onclick = buttonHan dler.bind(null, true);
1932 window.document.getElementById("abp-notification-no").onclick = buttonHand ler.bind(null, false);
1933 }
1934
1935 panel.setAttribute("class", "abp-" + notification.type);
1936 panel.setAttribute("noautohide", notification.type === "question");
1832 panel.openPopup(button, "bottomcenter topcenter", 0, 0, false, false, null); 1937 panel.openPopup(button, "bottomcenter topcenter", 0, 0, false, false, null);
1833 } 1938 }
1834 }; 1939 };
1835 UI.onPopupShowing = UI.onPopupShowing.bind(UI); 1940 UI.onPopupShowing = UI.onPopupShowing.bind(UI);
1836 UI.onKeyPress = UI.onKeyPress.bind(UI); 1941 UI.onKeyPress = UI.onKeyPress.bind(UI);
1837 UI.onIconClick = UI.onIconClick.bind(UI); 1942 UI.onIconClick = UI.onIconClick.bind(UI);
1838 UI.init(); 1943 UI.init();
1839 1944
1840 /** 1945 /**
1841 * List of event handers to be registered for each window. For each event 1946 * List of event handers to be registered for each window. For each event
(...skipping 16 matching lines...) Expand all
1858 ["abp-command-contribute", "command", UI.openContributePage.bind(UI)], 1963 ["abp-command-contribute", "command", UI.openContributePage.bind(UI)],
1859 ["abp-command-contribute-hide", "command", UI.hideContributeButton.bind(UI)] 1964 ["abp-command-contribute-hide", "command", UI.hideContributeButton.bind(UI)]
1860 ]; 1965 ];
1861 1966
1862 onShutdown.add(function() 1967 onShutdown.add(function()
1863 { 1968 {
1864 for (let window in UI.applicationWindows) 1969 for (let window in UI.applicationWindows)
1865 if (UI.isBottombarOpen(window)) 1970 if (UI.isBottombarOpen(window))
1866 UI.toggleBottombar(window); 1971 UI.toggleBottombar(window);
1867 }); 1972 });
OLDNEW
« no previous file with comments | « lib/notification.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld