Git Product home page Git Product logo

Comments (8)

j-5-s avatar j-5-s commented on June 24, 2024 1

What if you overwrote document.createElement?

var createElement = document.createElement;
document.createElement = function(){
  var args = [].slice.apply(arguments)
  var el = createElement.apply(this, args);
  if (el.nodeNode === 'SCRIPT') {
   //  attach listeners to onload here
  }
  return el;
}

You would still not be covered by document.appendChild with script's as strings but not sure how common that is with jsonp libraries. I've really only seen that with analytics libraries like google analytics.

from xhook.

jpillora avatar jpillora commented on June 24, 2024

JSONP cannot be globally hooked. Since JSONP just uses scripts and function calls, the only way to do this would be to manually insert hook points in each JSONP library. XHook is an XMLHttpRequest wrapper with hooking options and since JSONP doesn't use XMLHttpRequest at all, it's out of scope.

My recommendation would be to choose your JSONP library, jQuery for example, and modify it to do what you need.

from xhook.

jpillora avatar jpillora commented on June 24, 2024

Intercepting all possible ways of inserting a script tag isn't practical. Consider:

fooElement.innerHTML = '<script src="//server.com/my-jsonp-request.json?param=42"></script>';

This is a valid way for a library to make a JSONP request and would be impossible to catch.

from xhook.

j-5-s avatar j-5-s commented on June 24, 2024

one more idea. you could intercept all jsonp requests with a MutationObserver since it requires a script tag get added to the page. I've created an example here its not complete and only console.log's the data.

from xhook.

jpillora avatar jpillora commented on June 24, 2024

That's actually quite neat, I've never used MutationObserver. This looks like it might support the "after" hook, though I'm not sure about the "before" hook (and optional response replacement) as this would require the altering/cancelling of the script's request for script[src] fires off to the server

from xhook.

j-5-s avatar j-5-s commented on June 24, 2024

Yeah, before wouldn't work in terms of altering/canceling. The mutation observer would fire off after the script tag is appended to the page, but from my initial tests always before the request completes. The only concern would be all the mutations it would have to filter through to look for scripts. It could potentially slow down the execution of the page.

from xhook.

morsdyce avatar morsdyce commented on June 24, 2024

While MutationObserver is a good way to detect script tags in the page, we won't be able to stop the request from finishing and parsing, we might be able to remove the script tag in time and that would tell the browser to not process the incoming javascript but we need to test how each browser reacts to this change.

We also need to keep in mind this will only work in evergreen browsers and the user must provide a valid jsonp response using the correct callback name.

The only other way I can think about doing this is trying to override the original Script is using service workers. However using service workers having many limitations which aren't very logical for a library to implement. The 2 main ones are that you must be running on https and you can only have 1 service worker per scope (and this would need to run on the root scope).

from xhook.

dickeylth avatar dickeylth commented on June 24, 2024

Intercepting all possible ways of inserting a script tag isn't practical. Consider:

fooElement.innerHTML = '<script src="//server.com/my-jsonp-request.json?param=42"></script>';

This is a valid way for a library to make a JSONP request and would be impossible to catch.

It seems that in this way the script in the src would not be executed, even not be loaded.

from xhook.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.