不快な可能性のあるコンテンツをブロックする Chrome 拡張機能を作成しています。私が実装している方法の 1 つは、すべての画像をスキャンして、肌がどれだけ表示されているかを確認することです。新しい画像オブジェクトを作成し、crossOrigin フラグを " " に設定します。次に、画像をキャンバスに描画する onload 関数を作成し、キャンバスからデータを読み取り、分析を実行して、呼び出し関数のブール フラグを設定します。 . onload 関数を定義した後、Web ページのソース リストからイメージ ノードに src を割り当てます。image_scanner 関数は、Web ページ上の各画像ノードをループし、ブロックするさまざまな操作を実行する for ループ内で呼び出されます。これが私が行う最後の手術です。image_scanner を呼び出すコードは次のとおりです。
if (image_scanner(options.scanner_sensitivity, images[i]))
{
// Replace the image with a blank white image
images[i].src = chrome.extension.getURL("replacement.png");
}
ここに image_scanner 関数があります
function image_scanner(sensitivity, image)
{
// Sensitivity is a number and image is an image node.
// Declare a variable to count the number of skin pixels
var skin_count = 0;
if (image.width == 0 && image.height ==0)
{
// This means the image has no size and we cannot block it.
return false;
} // end if
var return_value = null; // set bool flag
// Create an HTML5 canvas object.
var canvas = document.createElement('canvas');
//window.alert("Created Canvas."); // used for testing.
// Get the context for the canvas.
var context = canvas.getContext("2d"); // This is what we actually use to draw images and pull the data from them.
context.canvas.width = image.width; // Set the canvas width to the width of the image
context.canvas.height = image.height; // Set the canvas height to the height of the image
img = new Image(); // Create a new image node to circumvent cross-domain restrictions.
img.crossOrigin = " "; // Set crossOrigin flag to ' ' so we can extract data from it.
img.onload = function(){
window.alert(img.src); // This always gives the same src until Chrome ends the function
context.drawImage(this, 0,0); // Draw the image onto the canvas.
var pixels = context.getImageData(0, 0, image.width, image.height).data;
// Now pixels is an array where every four entries in the array is the RGBa for a single pixel.
// So pixels[0] is the R value for the first pixel, pixels[1] is the G value for the first pixel,
// pixels[2] is the B value for the first pixel, and pixels[3] is the a (alpha or transparency) value for the first pixel.
// This means that pixels.length/4 is the number of pixels in the image.
// Now we calculate the number of skin pixels we can have before blocking the image.
var limit = ((pixels.length)/4) * (sensitivity/100);
// Now we go through the array of pixel data, checking if each pixel is a skin colored pixel based on its RGB value (the first 3 entries for that pixel in the pixels array)
// Each time we find a skin colored pixel, we increment skin_count and check if skin_count >= limit. If so, we return true.
for (var i = 0; i < pixels.length; i += 4) // We go up by four since every four entries describes 1 pixel
{
// pixel is skin if 0 <= (R-G)/(R+G) <= .5 and B/(R+G) <= .5 pixels[i] is the R value, pixels[i+1] is the G value, and pixels[i+2] is the B value.
if ((0 <= ((pixels[i] - pixels[i+1])/(pixels[i] + pixels[i+1]))) && (((pixels[i] - pixels[i+1])/(pixels[i] + pixels[i+1])) <= 0.5) && ((pixels[i+2]/(pixels[i] + pixels[i+1])) <= 0.5))
{
skin_count++;
//window.alert("Found skin pixel."); // used for testing.
if (skin_count >= limit)
{
//window.alert("Blocking image with src: " + image.src); // used for testing.
img.onload = null; // try to clear the onload function
return_value = true;
return false;
} // end inner if
} // end outer if
} // end for loop
//var temp;
img.onload = null;
return_value = false;
return false;
}; // end onload function
img.src = image.src; // Set the new image to the same url as the old one.
return return_value;
} // end image_scanner
何が問題なのかわかりませんが、onload 関数が実行され、ピクセルを通過し、フラグが設定され、戻り、再度実行されます。Chrome のデバッガーでデバッグを試みましたが、これだけしか見つかりませんでした。onload 関数内で onload を null に設定しようとしましたが、うまくいきません。onload 関数から false を返そうとしました。return_value != null になるまで image_scanner 関数で待機しようとしましたが、無限ループに入ったようで、onload 関数からアラートを受け取ることさえありませんでした。onload 関数が繰り返し実行される理由を誰かが知っていれば、非常に感謝しています。