0

I'm literally out of options of trying to get it work and need your suggestions/inputs of what should I do further to make it work.

What I'm doing

I'm building a Google Chrome Extension to enhance the existing functionality of a website. For that I need to inject my own JavaScript to HTML of the website. For that there are two ways:

  1. Inject my script file using content_scripts which is written in manifest.json of extension
  2. On click of extension icon, via background script chrome.tabs.executeScript

Both of the ways are fine and working for me.

About Website

There's a div element for content, whose HTML keeps on changing based on which link/button user has clicked. The page doesn't refresh, as the HTML is added/replaced via JavaScript for that div. And there are lot of complex operations happening (with server) to replace the content which is not possible to understand and to rewrite.

The website has obfuscated JavaScript file.

When user clicks on a link: JavaScript receives the click event > does some magic with server and builds up the HTML which needs to be replaced > sets the innerHTML of content div > uses jQuery to registers events for newly added buttons, links, iframe, etc.

All of this happens in a single big obfuscated function. Something like below:

(window.webpackJsonp = window.webpackJsonp || []).push([[12], {
    "/5H/": function (e, n, d) { ... },
    "A3c2K": function (e, n, d) {
       var de = { ...
          render: function () { ... <some HTML changing code> ... var e = this; oe.call(e); // setting up events ... },
          ...
       }
      function oe() {
         // all events setup function
         var t = this; ...
         t.iframeNode.on("load", function () {
                t.iframeNode[0].elem.contentWindow.print() // the problem
            })
         ...
      }},
...

The problem

The problem is, when user clicks the link, JavaScript creates iframe and triggers on load function for that iframe, which in-turn triggers the print window from that iframe. All that happens in one chain because it is written in one big JS function. I do not want to print window to popup.

  • My injected JavaScript or Chrome Extension, can act before the function is called (when users clicks on the link) or after everything has happened and it will be too late to stop print window from opening.

  • Also, windows.print = function() {} also won't work as the actual window will be the iframe's window where this needs to be executed, and that too before onLoad event. Already tried this.

  • Chrome extension doesn't have capabilities to close the print window. It can add new printers inside the print window but not able to totally disable it or close it. I haven't found one.

  • Other chrome extensions which are fiddler like, ex: Resource Override doesn't seems to work in my case because of interdependency of JS files. The page won't even show up anything as the above main JS file is responsible of loading the full UI and there are some errors that comes up when resource is overridden. Moreover I was not able to find which chrome extension API they are using to override resource at runtime.

  • If I copy the resource on local from Chrome Dev Tools for testing it out and if I comment out the .print() body of onLoad event. Then, print window won't show up and that is exactly what I want.

Overall, I want to disable print window totally from iFrame. And if that is somehow not possible then I want to close the print window from Chrome extension. Any help, suggestion or direction is deeply appreciated.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Sunil Kumar
  • 622
  • 1
  • 12
  • 33
  • Try hooking EventTarget.prototype.addEventListener in [page context](/a/9517879) and check if the hook is called to add a `load` event listener on an iframe, in which case you'll ignore it, otherwise call the original addEventListener. – wOxxOm May 12 '20 at 14:15
  • I'm actually injecting my script by directions given in the answer. I will try to hook it and see if it works. I'm now trying to override the full function for key `A3c2K` somehow using this codebase `window.webpackJsonp .flatMap(x => x.flatMap(y => y)) .filter(x => isNaN(x)) .flatMap(x => x) .filter(x => x.hasOwnProperty("A3c2K"))[0] = function(e, n, t) { log("overriden function") }` – Sunil Kumar May 12 '20 at 15:34
  • Can you please tell what is the meaning of the following codebase? `(window.webpackJsonp = window.webpackJsonp || []).push([[12], { "key": function(){}, "key2": function(){} }])`. There are about 1000 keys in the json document. I'm not able to understand this part: `.push([[12], {}])` – Sunil Kumar May 12 '20 at 15:37
  • This is how WebPack loads JS modules. – wOxxOm May 12 '20 at 15:40
  • If I override the object after WebPack has loaded JS modules, it's not working. Can it work with overriding like above? after modules are loaded, or should I drop this plan and find something else? – Sunil Kumar May 12 '20 at 15:42
  • 1
    You can try creating your own window.webpackJsonp = [] and override its `push` method to check what's being added so you can skip it if needed. This should be done in page context in a content script with `"run-at": "document-start"` so it runs before webpack. – wOxxOm May 12 '20 at 15:45
  • Thank you @wOxxOm Your last suggestion to create own web pack inside content script with `"run-at": "document-start"` worked. I have successfully overridden the function and disabled the window. – Sunil Kumar May 12 '20 at 18:15

0 Answers0