Note that there are some explanatory texts on larger screens.

plurals
  1. POReplace server JSON response in Chrome
    primarykey
    data
    text
    <p>I have a problem with some web-based tool that queries a server for JSON data in the AJAX way. It's not working properly - returned results are often not what I want and I need to <strong>wait a lot of time</strong> for the server to process the request.</p> <p>The truth is I always want the same data (about a few people, to be precise) returned, so I thought of faking the server response. I tried to achieve this in a few ways, but didn't succeed.</p> <p>I tried using Chrome's WebRequest functionality - I prepared an extension and redirected the request to a data URL with my JSON encoded. It worked, but only when I entered address manually in the Chrome's address field. When the web app tried to access the address, redirect silently failed. I immediately thought of Access Origin restrictions and quickly set up a webserver sending Access-Control-Allow-Origin headers. </p> <p>Unfortunately, this didn't work too. When I entered the address by hand, the server was being queried and returned the right JSON, but in the app the request... silently failed. It looked like Chrome totally ignored possible Access-Control-Allow-Origin headers, since it didn't even queried the server. It's quite possible that Chrome only analyzes URLs since when I redirected the problematic request to random URL in app's domain it worked.</p> <p>I also tried faking the whole JavaScript file of the app - which is statically linked to a document (e.g. it's included via standard script tag) using the same method. I failed miserably - the redirection just silently failed like it was failing on my first attempts.</p> <p>A common solution to my problem is to use some kind of a proxy which changes server output, but I can't do that - I don't want to wait a few minutes for server to process the request. And by the way, the app is accessible only via HTTPS. </p> <p>Is there any other way to feed the app with my hardcoded JSON? Or maybe I'm missing something?</p> <p><strong>EDIT</strong> Plugin code, based on "catblock" example:</p> <pre><code>chrome.webRequest.onBeforeRequest.addListener( function(info) { console.log("AJAX call intercepted: " + info.url) return {redirectUrl: 'data:application/json,"test"'}; }, // filters { urls: [ "*://address.to.the.js.file.requested" ] }, ['blocking']); </code></pre> <p>Manifest file, I only added the domain to the permissions section:</p> <pre><code>{ "name": "CatBlock", "version": "1.0", "description": "I can't has cheezburger!", "permissions": ["webRequest", "webRequestBlocking", "*://some.domain.com/*"], "background": { "scripts": ["background.js"] }, "manifest_version": 2 } </code></pre> <p><strong>EDIT 2:</strong> Here are the screenshots from network panel of Chrome's developer Console which show that request gets silently canceled: <img src="https://i.stack.imgur.com/i1DrF.png" alt="Console screenshot"> <img src="https://i.stack.imgur.com/WFZRZ.png" alt="Console screenshot"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload