では、まず、必要なページにコードを挿入する方法を説明します。正しい番号を選択する方法については、後ほど説明します。この例全体で使用jQuery
しますが、厳密には必要ではありませんが、少し簡単になると思います。
最初にcontent script
、マニフェストとhost permissions
注入先のページでa を宣言します。
"content_scripts": [
{
"matches": ["http://www.domain.com/page.html"],
"js": ["jquery.js","highlightNumbers.js"],
"css": ["highlight.css"]
}],
"permissions": ["http://www.domain.com/*"]
これにより、変更しようとしているページにコードが配置されます。ここで、ハイライトしたい数字が約 100 あるとおっしゃいましたが、これらはどのパターンとも一致しない特定の数字であると仮定します。そのため、それらすべてを選択する唯一の方法は、数字の明示的なリストを作成することです。ハイライトします。
highlightNumbers.js
// This array will contain all of the numbers you want to highlight
// in no particular order
var numberArray = [670,710,820,1000,...];
numberArray.forEach(function(v){
// Without knowing exactly what the page looks like I will just show you
// how to highlight just the numbers in question, but you could easily
// similarly highlight surrounding text as well
var num = "(" + v + ")";
// Select the '<td>' that contains the number we are looking for
var td = $('td.col-question:contains('+num+')');
// Make sure that this number exists
if(td.length > 0){
// Now that we have it we need to single out the number and replace it
var span = td.html().replace(num,'<span class="highlight-num">'+num+'</span>');
var n = td.html(span);
}
// Now instead of '(1000)' we have
// '<span class="highlight-num">(1000)</span>'
// We will color it in the css file
});
重要な数字をすべて選び出したので、それらに色を付ける必要があります。もちろん、好きな色を使用できますが、例として明るい緑を使用します。
ハイライト.css
span.highlight-num{
background-color: rgb(100, 255, 71);
}
これにより、ファイル内の配列に配置したすべての数値に色が付けられjs
ます。正確にテストできないため、問題がある場合はお知らせください。