使用Mozactivity Firefox操作系统将预先准备好的图像用作壁纸

Use a prepended image as wallpaper using Mozactivity Firefox OS

本文关键字:图像 准备好 Firefox Mozactivity 操作系统 使用      更新时间:2023-09-26

我想从网络上获取一个图像,并将其设置为Firefox操作系统设备上的壁纸。

到目前为止,我已经尝试了两种方法,

$(document).on( 'click', '#share-image', function() {
  walp = new Image()
  walp.onload = function () {
  var walCanvas = document.createElement('canvas');
  walCanvas.width = walp.width;
  walCanvas.height = walp.height;
  var walCtx = walCanvas.getContext('2d');
  walCtx.drawImage(walp,0,0);
  walCanvas.toBlob( function (walBlob) {
    var Activitiy = new MozActivity({
      name: 'share',
      data: {
        type: 'image/*',
        number: 1,
        blobs: [walBlob]
      }
    });
  });

};
walp.src = image; //image is the link  to jpeg.
});

而这个。。。

var shareImage = document.querySelector("#share-image"),
imgToShare = document.querySelector('#prependedImage');
alert(imgToShare);
if (shareImage && imgToShare) {
  shareImage.onclick = function () {
     if(imgToShare.naturalWidth > 0) {
      // Create dummy canvas
      var blobCanvas = document.createElement("canvas");
      blobCanvas.width = imgToShare.width;
      blobCanvas.height = imgToShare.height;
      // Get context and draw image
      var blobCanvasContext = blobCanvas.getContext("2d");
      blobCanvasContext.drawImage(imgToShare, 0, 0);
      // Export to blob and share through a Web Activitiy
      blobCanvas.toBlob(function (blob) {
        new MozActivity({
          name: "share",
          data: {
            type: "image/*",
            number: 1,
            blobs: [blob]
          }
        });
      });
    }
    else {
      alert("Image failed to load, can't be shared");
    }
  };
}

两者都适用于本地保存的图像,但不适用于从远程位置获取的图像。

我也尝试过以下方法。

  var pre = '<img width="100%" data-l10n-id="prependedImage" id="prependedImage" src="'+image+'" />';
          $('#image1').prepend(pre);

$('#prependedImage').attr('src',image);

两者都可以,但都不会显示壁纸菜单,除非它是本地保存的图像。我是javascript的新手,如果能给我一个解释代码示例问题的答案,我将不胜感激。谢谢

你能试试这样的东西吗:

    var xhr = new XMLHttpRequest({
        mozSystem: true
    });
    xhr.open("GET", "http://25.media.tumblr.com/4751d1eccf9311ee0e05bdff819a7248/tumblr_n2yxzxzxr81rsg8blo1_250.png", true);
    xhr.responseType = "blob";
    xhr.onload = function () {
        //sample activity
        var activity = new MozActivity({
        name: "share",
        data: {
            type: "image/*",
            number:1,
            blobs: [this.response],
            filenames:["wallpapertest.png"]
        },
        });
    };
    xhr.onerror = function () {
        alert("Error with System XHR");
    };
    xhr.send();

您还需要将应用程序设置为特权,并在清单中添加systemxhr权限:

  "permissions": {  
    "systemXHR": {} 
  },
  "type": "privileged"