-1

I have a Greasemonkey script (original thread here: Javascript to grab Javascript comments within <head>) that grabs a string from a webpage - a PageID that our CMS stamps on pages - and this string is then displayed in my browser using Greasemonkey. The purpose of this is for website maintenance so I can browse our website and quickly see what PageID a specific page is. The code used to achieve this is:

var commentNode = [].slice.call(document.head.childNodes).filter(function(node) {
            return node.nodeType == 8;
          })[0],
    id = commentNode.data.match(/^\s*(\d+)/)[1];

var elem = document.createElement('div');
elem.id = 'id-display';
elem.appendChild(document.createTextNode(id));
document.body.appendChild(elem);

GM_addStyle = function(css) {
        var head = document.getElementsByTagName('head')[0], style = document
                .createElement('style');
        if (!head) {
            return
        }
        style.type = 'text/css';
        style.textContent = css;
        head.appendChild(style);
}

This displays in my browser (Chrome or Firefox w/ Greasemonkey) as:

Page ID: 0001

I now have a separate HTML report that publishes from our CMS in the following format:

<table class="report-table">
 <thead>
    <tr>
      <th scope="col">Page Id</th>
      <th scope="col">Page title</th>
      <th scope="col">Page URL</th>
      <th scope="col">Content Editor</th>
      <th scope="col">Content manager</th>
      <th scope="col">Date updated</th>     
    </tr>
  </thead>
    <tr>
     <td>0001</td>
     <td>Webpage title</td>
     <td><a href="http://www.website.com/page01.htm">www.website.com/page01.htm</a></td>
     <td>Joe Bloggs</td>
     <td>Sussan Smith</td>
     <td>01/11/2012</td>
   </tr>
</table>

This webpage report is saved in an internally accessible location \internal-pc\cms-report.html

What I would like to do now is for my Greasemonkey to still grab the PageID string and to then lookup this PageID against the table in cms-report.html and to then display this information in the Greasemonkey overlay as:

Page ID: 0001
Page title: Webpage title
Page URL: www.website.com/page01.htm
Content Editor: Joe Bloggs
Content manager: Sussan Smith
Date Updated: 01/11/2012

Can anyone lead me down the right path?

Community
  • 1
  • 1
rlsaj
  • 735
  • 1
  • 12
  • 37

1 Answers1

1

Using jQuery and assuming you have the page id available in a variable pID and a result div #result:

var pID = '0001';
$(function(){
    $.get('internal-pc/cms-report.html', function(data){
        var table = $(data),
            selectedTD = table.find('td:contains("' + pID + '")'),
            parentTR = selectedTD.parent();
            parentTR.find('td').each(function(i){
                $('#result').append('<p>' + table.find('th:eq(' + i + ')').text() + ': ' + $(this).text() + '</p>');
            });
    });         
});

Note: You will need to modify the $.get URL to point to your cms-report.html file.

kayen
  • 4,838
  • 3
  • 19
  • 20