2

I want to set a user-uploaded image as a wallpaper using the chrome.wallpaper api.

I managed to convert the uploaded file to an ArrayBuffer using JavaScript, but using that in the 'data' key resulted in an error message similar to this:

"Invalid value for argument 1. Property 'data': Expected 'binary' but got 'object'."

I converted the ArrayBuffer to a blob using new Blob(), but I got the same error. Converting it to a string resulted in the same error, but with 'object' replaced by 'string'.

Searching on Google has yielded nothing so far. What is the binary format in this case, and how would I send it?

The manifest definitely has the wallpaper attribute, and I was able to set the wallpaper through URLs as well. Binary is the only problem I'm facing.

The following is my code so far:

app.js:

reader.addEventListener('load', function() {
  chrome.runtime.sendMessage({
    'action': 'new_wallpaper',
    'wallpaper_data': reader.result // I've also tried new Blob([reader.result])
  }, function(response) {
    console.log(response);
  });
}, false);
reader.readAsArrayBuffer(file);

background.js:

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
  if(request.action == 'new_wallpaper') {
    chrome.wallpaper.setWallpaper({
      'data': request.wallpaper_data,
      'layout': 'CENTER_CROPPED',
      'filename': 'Wallpaper Manager',
      'thumbnail': true
    }, function(thumbnail) {
       sendResponse(thumbnail);
    });
  }
  return true;
});
Trot Arey
  • 21
  • 2
  • 1
    Show the code that you used – Patrick Evans Apr 12 '17 at 21:22
  • Can you include `manifest.json` and full `javascript` tried at Question? See http://stackoverflow.com/help/mcve – guest271314 Apr 12 '17 at 21:23
  • @PatrickEvans Updated. – Trot Arey Apr 12 '17 at 21:42
  • You should log result.wallpaper_data in your onMessage listener. The object passed through [sendMessage](https://developer.chrome.com/extensions/runtime#method-sendMessage) _"should be a JSON-ifiable object"_ So your data maybe getting converted to something you didnt expect – Patrick Evans Apr 12 '17 at 21:51
  • @PatrickEvans I tried it just now. It outputs an empty object to the console. The result in sendMessage is an ArrayBuffer, so now I'm confused as to what happened while sending it. Are ArrayBuffers not JSON-ifiable? – Trot Arey Apr 12 '17 at 21:56
  • 1
    I think this branch can help you http://stackoverflow.com/questions/8593896/chrome-extension-how-to-pass-arraybuffer-or-blob-from-content-script-to-the-bac – Nutscracker Apr 13 '17 at 00:08
  • @Nutscracker Definitely did! Problem solved. Am I supposed to just leave this question, or should I write an answer and mark it as right? – Trot Arey Apr 13 '17 at 08:45
  • @TrotArey i will copy my message to unswer, and you can vote it) – Nutscracker Apr 13 '17 at 09:53
  • @TrotArey hm...i can't(...please write an answer and mark it. – Nutscracker Apr 13 '17 at 09:57

0 Answers0