2012-11-02 51 views
-1

我有一個Greasemonkey腳本(原始線程在這裏:Javascript to grab Javascript comments within <head>),從網頁抓取一個字符串 - 一個PageID,我們的CMS郵票在頁面上 - 然後這個字符串然後顯示在我的瀏覽器使用Greasemonkey。這樣做的目的是爲了網站維護,所以我可以瀏覽我們的網站,並快速查看特定頁面的PageID。用來實現這個過程的代碼:Javascript來比較價值

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); 
} 

這顯示在我的瀏覽器(Chrome或Firefox W/Greasemonkey的)爲:

Page ID: 0001 

我現在已經從我們的CMS發佈一個單獨的HTML報告格式如下:

<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> 

本網頁報告保存在內部訪問的位置\內部PC \ CMS-report.html

我現在想要做的是讓我的Greasemonkey仍然抓取PageID字符串,然後在cms-report.html的表中查找此PageID,然後在Greasemonkey覆蓋圖中顯示此信息,如下所示:

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 

任何人都可以讓我走下正確的道路嗎?

回答

1

使用jQuery和假設你有一個變量pID可用的頁面ID和結果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文件。