Comments (8)
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.
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.
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.
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.
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.
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.
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.
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)
- can't post formData HOT 2
- How to get the request in the iframe HOT 1
- Warning 'Event.path' is deprecated and will be removed in M109, around January 2023. Please use 'Event.composedPath()' instead HOT 1
- Slowing down the network calls HOT 2
- Errors are passed to Promise.resolve instead of Promise.reject with xhook 1.4.9 HOT 6
- Automate release process HOT 10
- Speed up tests
- Some files are unnecessary. HOT 1
- Automatic Github releases using conventional commits
- xhook breaks `navigator.sendBeacon(url, formData)` HOT 6
- Change the output of xhook in src/main.js HOT 1
- FormData fails all calls - testing solution but can't make it work at all HOT 17
- Before handlers for fetch(new Request('/foo')) requests : 1) sync handlers get no information; 2) sync and async can't change request parameters or replace the Request instance.
- When other programs modify XMLHttpRequest.prototype., xhook will not work HOT 1
- [Question] How to skip certain URL (some URL use mock data and some still go to real API )? HOT 1
- When you use xhr.response to read response data, the hook becomes invalid
- [Bug] Unable to retrieve the requested URL in the response
- The HTTP field name should be lowercase
- [Bug] The default value of XHR's response is incorrect
- [Bug] isFetch parameter is undefined
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from xhook.