Chrome扩展程序将局部变量作用于函数内部(来自backgound.js)并在全局范围内(或在popup.js中)使用。

时间:2018-09-14 22:41:00

标签: javascript google-chrome-extension clarifai

最近,我想搭载someone's extension来启动项目。我想确定一个图像源的范围(局部变量,base64 url​​),然后在弹出页面上将其识别出来。我不断收到错误消息“ imgb64.replace不是函数”或未定义“ imgb64”。

就像我的标题所说的那样,我想在函数内部(在backgound.js中)范围内的局部变量,并在全局范围内(或在popup.js中)使用它。对此非常新,请帮助大家。

// this is popup.js
chrome.runtime.getBackgroundPage(function(bg) {
bg.capture(window);
});

/// what I did

function img_find() {
var imgs = document.getElementsByTagName("img");
var imgSrcs = [];

for (var i = 0; i < imgs.length; i++) {
    imgSrcs.push(imgs[i].src);
}
return imgSrcs;
}

var imgb64 = img_find();

try {
const app = new Clarifai.App({
apiKey: 'mykey'
});
    }

    catch(err) {
alert("Need a valid API Key!");
throw "Invalid API Key";
}

// Checks for valid image type
function validFile(imageName) {
var lowerImageName = imageName.toLowerCase();
return lowerImageName.search(/jpg|png|bmp|tiff/gi) != -1;
}


var imageDetails = imgb64.replace(/^data:image\/(.*);base64,/, '');


console.log(imageDetails)

app.models.predict("e466caa0619f444ab97497640cefc4dc", {base64: 
imageDetails}).then(

  function(response) {

    // do something with response
  },
  function(err) {
    // there was an error
  }
  );

 /// end what I did

下面是background.js,我想我需要的是本地var img.src,仅此而已。

function capture(popup) {

function callOnLoad(func) {
    popup.addEventListener("load", func);
    if (popup.document.readyState === "complete") {
        func();
    }
}


crxCS.insert(null, { file: "capture.js" }, function() {


    crxCS.callA(null, "get", function(result) {

        var scrShot, zm, bufCav, bufCavCtx;



        function mkImgList() {
            for (var i = 0; i < result.vidShots.length; i++) {
                var img = new popup.Image();
                img.onload = function() {
                    this.style.height = this.naturalHeight / 
(this.naturalWidth / 400) + "px";
                };

                if (result.vidShots[i].constructor === String) {
                    img.src = result.vidShots[i];
                } else {
                    bufCav.width = result.vidShots[i].width * zm;
                    bufCav.height = result.vidShots[i].height * zm;
                    bufCavCtx.drawImage(scrShot, -result.vidShots[i].left * 
zm, -result.vidShots[i].top * zm);
                    img.src = bufCav.toDataURL('image/png');

////// maybe clarifai here ?


////end clarifai
                }

                popup.document.body.appendChild(img);
            }
            popup.onclick = function(mouseEvent) {
                if (mouseEvent.target.tagName === "IMG") {
                    chrome.downloads.download({ url: mouseEvent.target.src, 
saveAs: true, filename: "chrome_video_capture_" + (new Date()).getTime() + 
".png" });
                }
            };
            popup.onunload = function(mouseEvent) {
                crxCS.callA(null, "rcy");
            };
        }   /// end mkImgList

        if (result.needScrShot) {
            bufCav = popup.document.createElement("canvas");
            bufCavCtx = bufCav.getContext("2d");

            chrome.tabs.captureVisibleTab({ format: "png" }, 
function(dataUrl) {
                scrShot = new Image();
                scrShot.onload = function() {
                    chrome.tabs.getZoom(function(zoomFactor) {
                        zm = zoomFactor;
                        callOnLoad(function() {
                            mkImgList(zoomFactor);
                        });
                    });
                };
                scrShot.src = dataUrl;


            });
        } else if (result.vidShots.length) {
            callOnLoad(mkImgList);
        } else {
            popup.document.body.appendChild(notFound);
        }

    });  // end crxCS.callA
});   // end crxCS.insert
}  // end capture

请帮助大家。 :)

0 个答案:

没有答案