繁体   English   中英

Chrome扩展程序img.onload函数连续执行

[英]Chrome Extension img.onload function continuously executes

我正在写一个Chrome扩展程序,该程序可以阻止令人反感的内容。 我正在实现的一种方法是扫描所有图像,并查看正在显示多少皮肤。 我创建一个新的图像对象,将crossOrigin标志设置为“”,然后创建一个onload函数,将图像绘制到画布上,从画布中读取数据,然后执行分析,为调用函数设置一个布尔标志。 定义onload函数后,我从网页的源列表中为图像节点分配一个src。 在for循环内调用image_scanner函数,该循环遍历网页上的每个图像节点并执行各种操作以进行阻止。 这是我执行的最后一个操作。 这是调用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。 我尝试在image_scanner函数中等待,直到return_value!= null,但这似乎进入了一个无限循环,我什至没有从onload函数得到警报。 如果有人知道为什么onload函数会反复执行,我将不胜感激。

如果要将.src设置为空白图像,并且不希望在加载.onload时再次调用它, .onload在设置.src之前清除.src

if (image_scanner(options.scanner_sensitivity, images[i])) {
    // clear our onload handler
    images[i].onload = function() {};
    // Replace the image with a blank white image
    images[i].src = chrome.extension.getURL("replacement.png");
}

看起来您正在从onload处理程序中返回一个值,并期望从image_scanner函数中返回该值。 那样行不通。 image_scanner已经返回很长时间之后,onload处理程序将在image_scanner长时间后被image_scanner 您将需要重写代码以与onload的异步处理一起使用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM