michaelmcgurk michaelmcgurk - 2 months ago 32
jQuery Question

Read HTML page element value into Chrome Extension

I have a page called

Page1.html
held at http://intranet/ with the contents:

<div id="username">my.username</div>
.

I'd like to use jQuery to read this value into my Chrome Extension.

Currently I have hard coded text:
document.getElementById("username").value="mcgurk"
in my
script.js
of my Chrome Extension but I'd prefer to grab the value in the
username
element.




Potentially useful:

I'm not great with Javascript but I did follow this tutorial a little: http://www.dynamicdrive.com/forums/showthread.php?67448-document-getElementById-from-another-page which recommends something like this code:

<script type="text/javascript">
jQuery(function($){
$('#result').load('Page2.html #intro');
});
</script>


Their example works great but I'd like to build the above into a Chrome Extension.

Can anyone provide assistance for this issue?

Answer
$.ajax({
    url: 'https://intranet/index.php',
    success: function(html) {
        var nodes = $.parseHTML(html);
        var username = $("<div>").append(nodes).find("#username").text();
        $('#username').val(username);
    }
});