1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// found in the LICENSE file.
4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
5cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// Applies DomDistillerJs to the content of the page and returns a
6cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)// DomDistillerResults (as a javascript object/dict).
7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)(function() {
8c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  try {
9f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // The generated domdistiller.js accesses the window object only explicitly
10f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // via the window name. So, we create a new object with the normal window
11f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // object as its prototype and initialize the domdistiller.js with that new
12f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    // context so that it doesn't change the real window object.
13f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    function initialize(window) {
14f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      // This include will be processed at build time by grit.
15f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      <include src="../../../../third_party/dom_distiller_js/package/js/domdistiller.js"/>
16f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    }
17f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    context = Object.create(window);
18f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    initialize(context);
19f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // The OPTIONS placeholder will be replaced with the DomDistillerOptions at
21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    // runtime.
22f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    res = context.com.dom_distiller.DomDistiller.applyWithOptions($$OPTIONS);
23cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    return res;
24c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  } catch (e) {
25cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    window.console.error("Error during distillation: " + e);
26cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    if (e.stack != undefined) window.console.error(e.stack);
27c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch  }
28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  return undefined;
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)})()
30