Can We Insert JavaScript into Any Webpage Loaded in the Browser

Can we insert javascript into any webpage loaded in the browser

Try using Greasemonkey: http://www.greasespot.net/. You can use it to execute custom scripts on page load for any website you want. You can find some basic tutorials here: http://wiki.greasespot.net/Tutorials.

Inject Javascript code into a web page

As you are only doing this once, starting your script from the browsers JavaScript console should be enough. Open the developer tools, navigate to the console tab, paste your script content, and press enter.

To get the edited HTML, evaluate the expression document.documentElement.outerHTML in the console. Copy the output to a text editor of your choice, prepend it with a doctype, and save it as html.

Run my Javascript code on every page on my browser, similar to how a chrome extension would

Actually the best thing to do is to create a chrome extension

How to inject JavaScript into a webpage

I got the answer. The problem is when I run the document.getElementbyId() method in the address bar it returns a string value which is not being stored and therefore the browser displays that string on web page.
just added var a= before the statement i.e.

javascript:var a=document.getElementById("Name").value="Yougansh"

Inject local .js file into a webpage?

Since your already using a fiddler script, you can do something like this in the OnBeforeResponse(oSession: Session) function

    if ( oSession.oResponse.headers.ExistsAndContains("Content-Type", "html") &&
oSession.hostname.Contains("MY.TargetSite.com") ) {

oSession.oResponse.headers.Add("DEBUG1_WE_EDITED_THIS", "HERE");

// Remove any compression or chunking
oSession.utilDecodeResponse();

var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);

// Find the end of the HEAD script, so you can inject script block there.
var oRegEx = oRegEx = /(<\/head>)/gi
// replace the head-close tag with new-script + head-close
oBody = oBody.replace(oRegEx, "<script type='text/javascript'>console.log('We injected it');</script></head>");

// Set the response body to the changed body string
oSession.utilSetResponseBody(oBody);
}

Working example for www.html5rocks.com :

    if ( oSession.oResponse.headers.ExistsAndContains("Content-Type", "html") &&
oSession.hostname.Contains("html5rocks") ) { //goto html5rocks.com
oSession.oResponse.headers.Add("DEBUG1_WE_EDITED_THIS", "HERE");
oSession.utilDecodeResponse();
var oBody = System.Text.Encoding.UTF8.GetString(oSession.responseBodyBytes);
var oRegEx = oRegEx = /(<\/head>)/gi
oBody = oBody.replace(oRegEx, "<script type='text/javascript'>alert('We injected it')</script></head>");
oSession.utilSetResponseBody(oBody);
}

Note, you have to turn streaming off in fiddler : http://www.fiddler2.com/fiddler/help/streaming.asp and I assume you would need to decode HTTPS : http://www.fiddler2.com/fiddler/help/httpsdecryption.asp

I have been using fiddler script less and less, in favor of fiddler .Net Extensions - http://fiddler2.com/fiddler/dev/IFiddlerExtension.asp

How to manually insert js code on each load of page via the console of the browser

Thanks for posting! In future posts, please try to provide some code or an example of something you've tried previously.

Anyways, here is a brief example of a script that will check for an existing number, check to see if there is a &source parameter set, begin the timer if there isn't one, and generate a new number if the timer finishes or the parameter is set.

To save the information between pages, you should consider using window.localStorage. This will allow you to check for and save the number to be used on later loads.

Note that this snippet isn't going to work until you bring it into your own page. Also, as @Sorin-Vladu mentioned, you'll have to use a browser extension if you don't have access to modify the pages you're running the script on.

const timeout = 120000
// This can be replaced by your manual executionwindow.onload = () => { start()}
function start() { // Attempt to pull the code from storage let code = localStorage.getItem('code') console.log(code) // Get the URL parameters let urlParams = new URLSearchParams(window.location.search) // Check to see if the source parameter exists if (!urlParams.has('source')) { // If not, begin the timer setTimeout(() => { setCode() }, timeout) } else { setCode() }}
function setCode() { const code = Math.floor(Math.random() * 1000000) localStorage.setItem('code', code) console.log(code)}


Related Topics



Leave a reply



Submit