简体   繁体   English

JavaScript从Crossrider浏览器扩展中的URL加载图像

[英]JavaScript Load Image from URL in a Crossrider browser extension

I'm currently writing an extension using Crossrider, and I need to load an image directly using the URL for doing some image processing on it. 我目前正在使用Crossrider编写扩展程序,并且需要直接使用URL加载图像以对其进行一些图像处理。 However, the onload event doesn't seem to be firing at all. 但是, onload事件似乎根本没有触发。

Am I doing something wrong or is it even possible to do that in a browser extension? 我是在做错什么,还是有可能在浏览器扩展中做到这一点?

Here is the relevant piece of code: 这是相关的代码段:

var imga = document.createElement('img');
imga.src = obj.attr('href'); // URL of the image
imga.style.display = 'none';
imga.onload = function() {
        alert('Image loaded');
        var imgData = getImageData(imga, 0, imga.height - 3);
        alert('Got Image data');
};

EDIT 编辑

Here is the full code 这是完整的代码

function readImage(obj)
        {
            console.log('Reading');
            relayReadImage(obj.attr('href'));
        }

        function relayReadImage(link)
        {
            var dateObj = new Date();
            var newlink = link + "?t=" + dateObj.getTime();
            console.log(newlink);
            appAPI.request.get(
                {
                url: newlink,

                onSuccess: function(response, additionalInfo) {
                    console.log(response);
                },

                    onFailure: function(httpCode) {
                    alert('GET:: Request failed. HTTP Code: ' + httpCode);
                }

                });
        }

I'm a Crossrider employee and would be happy to help you. 我是Crossrider的员工,很乐意为您提供帮助。 If I understand correctly, you are attempting to use the URL (href) of an object in a page's dom ( obj.attr('href') ) to load the image into a variable in the extension. 如果我理解正确,您正在尝试使用页面dom( obj.attr('href') )中对象的URL(href)将图像加载到扩展名的变量中。 You can achieve this using our cross-browser appAPI.request.get method in your extension.js file, as follows: 您可以使用extension.js文件中的跨浏览器appAPI.request.get方法来实现此目的 ,如下所示:

appAPI.ready(function($) {
    appAPI.request.get({
        url: obj.attr('href'),
        onSuccess: function(response) {
            var imgData = response;
            console.log(imgData);
        }
    });
});

However, if I've misunderstood your question, please can you clarify the following: 但是,如果我误解了您的问题,请您澄清以下几点:

  1. What is the obj object is? obj对象是什么?
  2. What are you trying to achieve, and in which context (in the Extension or on the Page)? 您想达到什么目的,以及在哪种情况下(在扩展程序中或在页面上)?

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

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