Intercept request and modify response [duplicate] - javascript

This question already has answers here:
Modify HTTP responses from a Chrome extension
(8 answers)
Closed 4 years ago.
I want to make a Chrome extension that when the user requests mysite/file.js it will replace the alert('hi') to alert('bye'). I can't figure this out I am new at Chrome Extensions. Thanks.

I think there might be better ways to achieve what you need, depending on your use case.
For example, you could inject a content script that overrides alert on the page world.
context.js
const script = document.createElement('script');
script.innerHTML = `
const originalAlert = window.alert;
window.alert = (msg) => {
originalAlert(msg === 'hello' ? 'bye' : msg);
};
`;
document.body.appendChild(script);

Related

javascript - download a file and wait for it to finish [duplicate]

This question already has answers here:
Detect when a browser receives a file download
(24 answers)
Browser event when downloaded file is saved to disk
(3 answers)
Closed 5 years ago.
I have looked in many threads but cannot find an answer that works with current chrome & IE ...
I want to download a file (from the same domain as the page, no CORS issues) and save it locally and I also need a callback when the download is done in order to turn off my 'downloading' notification.
I tried with a hidden iframe like this:
// to download a file we don't want to use window.location.assign because in case of error the broswer
// will redirect to the error page, instead we create an invisible iframe and set src to it
function downloadToFile(url, done) {
const iframe = document.createElement('iframe');
iframe.style.display = 'none';
document.body.appendChild(iframe);
iframe.setAttribute('src', url);
if (done) {
const doc = iframe.contentDocument || iframe.contentWindow.document;
const inter = setInterval(() => {
if (doc.readyState === "complete") {
clearInterval(inter);
done();
}
}, 50);
}
}
However this does not work - Chrome seems to set the iframe's readyState to 'complete' immediately and so done is called while the file is still loading.
How can this be achived ?
(I also tried to hook a 'load' event but it seems it is not called in chrome and the standard says no iframe events are guaranteed).

Jquery/Ajax Code which check the internet [duplicate]

This question already has answers here:
How to detect online/offline event cross-browser?
(15 answers)
Closed 8 years ago.
Jquery Code which check the internet/network is there or not(mobile/PC/Tablet).It must just check on page load.I thinkAjax will good because Ajax will check after certain interval.
I am looking just like http://tomriley.net/, But it is plugin, I am looking for simple jquery/Javascript.
Its static page which check system internet only.
Any idea is appreciated.
You might try a $.ajax() invoication with a .fail() handler, for example JQuery's getJSON():
var network_available; // bool
var url = '/some/json/call'; // must be relative to the site that
// you are already addressing
$.getJSON(url, function(data) {
network_available = true;
})
.fail(function() {
network_available = false;
});
Though I doubt this will solve all of your problems. The Javascript engine won't allow 'foreign' URL's, just the domain that the script or page was received from. So you'd not be really testing network availability, but also whether your site is up and responding within a reasonable time.

How to reroute console.log to document.write [duplicate]

This question already has answers here:
JavaScript Document.Write Replaces All Body Content When Using AJAX
(7 answers)
Closed 9 years ago.
I would like to create a "Console" app for JavaScript so that messages are written to the screen.
console.log("Rerouting log messages to the screen");
console.log = function (message) { document.writeln(message); };
console.log("Log messages are now routed to the screen.");
This works, except that each time something is written to the screen, it wipes out any existing content.
Is there a way to do this?
This is how document.write works. To avoid this you should use document.createElement and document.body.appendChild for example.
For example you can try this code:
console.log = function (message) {
var p = document.createElement( 'p' );
p.innerHTML = message;
document.body.appendChild( p );
};
Read more about document.write at MDN.

Redirect to page according to url path [duplicate]

This question already has answers here:
How do I redirect to another webpage?
(58 answers)
Closed 9 years ago.
Is that possible to redirect to page accordingly for example http://mysite.com/page.html to http://mysite.com/10010/page1.html (where cookies set to 10010); http://mysite.com/page2.html to http://mysite.com/10010/page2.html (where cookies set to 10010) and so on.
if ($.cookie('10010'))
{
window.location.href = 'http://mysite.com/10010'+'what to insert here?';
}
Can I do it just with one string or I have to declare every page ?
Not a good way to handle cookies. You'd better of doing something like this
Setting up a cookie
var mylocation = window.location.href.split("/")[1];
$.cookie("mylocation", mylocation);
Getting your location
var mylocation = $.cookie("mylocation");
if( mylocation ) {
window.location.href = "/"+mylocation+window.location.href;
}

Is there a way to deny location.reload call and href modification? [duplicate]

This question already has answers here:
Programmatically disable window.location.reload?
(2 answers)
Closed 9 years ago.
I tried to do this:
location.reload = function() { return false }
But it seems to be denied as it still reloads the page.
EDIT
I do not want to do any harm for a user and lock it into the page. The case where I need it is - integration testing on an existing code base which has location.reload() and location.href = '...' all over the place.
You can use onbeforeunload
window.onbeforeunload = function () {
return confirm("Are you sure you want to leave?");
};
However this is not supported in firefox and opera for that this may be help you
window.onbeforeunload and window.onunload is not working in Firefox , Safari , Opera?

Categories

Resources