summaryrefslogtreecommitdiff
path: root/data/extensions/spyblock@gnu.org/lib/child/elemHideEmulation.js
blob: 7c4ee171196246840bebdca822ee308ad05a798c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
 * This file is part of Adblock Plus <https://adblockplus.org/>,
 * Copyright (C) 2006-2017 eyeo GmbH
 *
 * Adblock Plus is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * Adblock Plus is distributed in the hope that it will be useful,
 * 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/>.
 */

"use strict";

(function()
{
  let {Services} = Cu.import("resource://gre/modules/Services.jsm", {});

  let {port} = require("messaging");
  let {getFrames, isPrivate} = require("child/utils");
  let {RequestNotifier} = require("child/requestNotifier");

  function getFilters(window, callback)
  {
    let message = {
      frames: getFrames(window),
      payload: {
        type: "filters.get",
        what: "elemhideemulation"
      }
    };
    port.emitWithResponse("ext_message", message).then(callback);
  }

  function addUserCSS(window, cssCode)
  {
    let uri = Services.io.newURI("data:text/css," + encodeURIComponent(cssCode),
        null, null);
    let utils = window.QueryInterface(Ci.nsIInterfaceRequestor)
                      .getInterface(Ci.nsIDOMWindowUtils);
    utils.loadSheet(uri, Ci.nsIDOMWindowUtils.USER_SHEET);
  }

  function initElemHideEmulation()
  {
    let scope = Object.assign({}, require("common"));
    Services.scriptloader.loadSubScript(
        "chrome://adblockplus/content/elemHideEmulation.js", scope);

    let onContentWindow = (subject, topic, data) =>
    {
      if (!(subject instanceof Ci.nsIDOMWindow))
        return;

      let onReady = event =>
      {
        subject.removeEventListener("load", onReady);
        let handler = new scope.ElemHideEmulation(
          subject, getFilters.bind(null, subject), (selectors, filters) =>
          {
            if (selectors.length == 0)
              return;

            addUserCSS(subject, selectors.map(
              selector => selector + "{display: none !important;}"
            ).join("\n"));

            if (!isPrivate(subject))
              port.emit("addHits", filters);

            let docDomain = null;
            try
            {
              // We are calling getFrames() here because it will consider
              // "inheritance" for about:blank and data: frames.
              docDomain = new URL(getFrames(subject)[0].location).hostname;
            }
            catch (e)
            {
              // Invalid URL?
            }

            for (let i = 0; i < filters.length; i++)
            {
              RequestNotifier.addNodeData(subject.document, subject.top, {
                contentType: "ELEMHIDE",
                docDomain: docDomain,
                thirdParty: false,
                location: "##" + selectors[i],
                filter: filters[i],
                filterType: "elemhideemulation"
              });
            }
          }
        );

        handler.apply();
      };

      subject.addEventListener("load", onReady);
    };

    Services.obs.addObserver(onContentWindow, "content-document-global-created",
        false);
    onShutdown.add(() =>
    {
      Services.obs.removeObserver(onContentWindow,
          "content-document-global-created");
    });
  }

  initElemHideEmulation();
})();