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

Unified Diff: lib/child/subscribeLinks.js

Issue 29338861: Issue 3851 - Implement subscribe link handling via process scripts (Closed)
Patch Set: Added guard and removed dead code Created April 18, 2016, 3:36 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 | « lib/child/main.js ('k') | lib/ui.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/child/subscribeLinks.js
===================================================================
rename from chrome/content/subscribeLinkHandler.js
rename to lib/child/subscribeLinks.js
--- a/chrome/content/subscribeLinkHandler.js
+++ b/lib/child/subscribeLinks.js
@@ -10,97 +10,109 @@
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
*/
-(function()
+let {Services} = Cu.import("resource://gre/modules/Services.jsm", {});
+
+let {port} = require("messaging");
+
+Services.obs.addObserver(onContentWindow, "content-document-global-created",
+ false);
+onShutdown.add(() =>
{
- addEventListener("click", onClick, false);
- addMessageListener("AdblockPlus:Shutdown", onShutdown);
+ Services.obs.removeObserver(onContentWindow,
+ "content-document-global-created");
+});
- function onShutdown(message)
+function onContentWindow(subject, topic, data)
+{
+ if (subject instanceof Ci.nsIDOMWindow && subject.top == subject)
{
- if (message.data == Components.stack.filename)
+ let eventTarget = subject.QueryInterface(Ci.nsIInterfaceRequestor)
+ .getInterface(Ci.nsIWebNavigation)
+ .QueryInterface(Ci.nsIDocShell)
+ .chromeEventHandler;
+ if (eventTarget)
Wladimir Palant 2016/04/18 15:38:40 Not sure when this happens but there are apparentl
+ eventTarget.addEventListener("click", onClick, true);
+ }
+}
+
+function onClick(event)
+{
+ if (onShutdown.done)
+ return;
+
+ // Ignore right-clicks
+ if (event.button == 2)
+ return;
+
+ // Search the link associated with the click
+ let link = event.target;
+ while (!(link instanceof Ci.nsIDOMHTMLAnchorElement))
+ {
+ link = link.parentNode;
+
+ if (!link)
+ return;
+ }
+
+ let queryString = null;
+ if (link.protocol == "http:" || link.protocol == "https:")
+ {
+ if (link.host == "subscribe.adblockplus.org" && link.pathname == "/")
+ queryString = link.search.substr(1);
+ }
+ else
+ {
+ // Firefox doesn't populate the "search" property for links with
+ // non-standard URL schemes so we need to extract the query string
+ // manually
+ let match = /^abp:\/*subscribe\/*\?(.*)/i.exec(link.href);
+ if (match)
+ queryString = match[1];
+ }
+
+ if (!queryString)
+ return;
+
+ // This is our link - make sure the browser doesn't handle it
+ event.preventDefault();
+ event.stopPropagation();
+
+ // Decode URL parameters
+ let title = null;
+ let url = null;
+ let mainSubscriptionTitle = null;
+ let mainSubscriptionURL = null;
+ for (let param of queryString.split("&"))
+ {
+ let parts = param.split("=", 2);
+ if (parts.length != 2 || !/\S/.test(parts[1]))
+ continue;
+ switch (parts[0])
{
- removeEventListener("click", onClick, false);
- removeMessageListener("AdblockPlus:Shutdown", onShutdown);
+ case "title":
+ title = decodeURIComponent(parts[1]);
+ break;
+ case "location":
+ url = decodeURIComponent(parts[1]);
+ break;
+ case "requiresTitle":
+ mainSubscriptionTitle = decodeURIComponent(parts[1]);
+ break;
+ case "requiresLocation":
+ mainSubscriptionURL = decodeURIComponent(parts[1]);
+ break;
}
}
- function onClick(event)
- {
- // Ignore right-clicks
- if (event.button == 2)
- return;
-
- // Search the link associated with the click
- let link = event.target;
- while (!(link instanceof content.HTMLAnchorElement))
- {
- link = link.parentNode;
-
- if (!link)
- return;
- }
-
- let queryString = null;
- if (link.protocol == "http:" || link.protocol == "https:")
- {
- if (link.host == "subscribe.adblockplus.org" && link.pathname == "/")
- queryString = link.search.substr(1);
- }
- else
- {
- // Firefox doesn't populate the "search" property for links with
- // non-standard URL schemes so we need to extract the query string
- // manually
- let match = /^abp:\/*subscribe\/*\?(.*)/i.exec(link.href);
- if (match)
- queryString = match[1];
- }
-
- if (!queryString)
- return;
-
- // This is our link - make sure the browser doesn't handle it
- event.preventDefault();
- event.stopPropagation();
-
- // Decode URL parameters
- let title = null;
- let url = null;
- let mainSubscriptionTitle = null;
- let mainSubscriptionURL = null;
- for (let param of queryString.split("&"))
- {
- let parts = param.split("=", 2);
- if (parts.length != 2 || !/\S/.test(parts[1]))
- continue;
- switch (parts[0])
- {
- case "title":
- title = decodeURIComponent(parts[1]);
- break;
- case "location":
- url = decodeURIComponent(parts[1]);
- break;
- case "requiresTitle":
- mainSubscriptionTitle = decodeURIComponent(parts[1]);
- break;
- case "requiresLocation":
- mainSubscriptionURL = decodeURIComponent(parts[1]);
- break;
- }
- }
-
- sendAsyncMessage("AdblockPlus:SubscribeLink", {
- title: title,
- url: url,
- mainSubscriptionTitle: mainSubscriptionTitle,
- mainSubscriptionURL: mainSubscriptionURL
- });
- }
-})();
-
+ port.emit("subscribeLinkClick", {
+ title: title,
+ url: url,
+ mainSubscriptionTitle: mainSubscriptionTitle,
+ mainSubscriptionURL: mainSubscriptionURL
+ });
+}
« no previous file with comments | « lib/child/main.js ('k') | lib/ui.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld