1
Answer

chrome.runtime.sendMessage error “Message length exceeded”

When i am sending big size of json object from content.js to background.js usingchrome.runtime.sendMessage then showing error like
 
Error is : Uncaught Error: Message length exceeded maximum allowed length.
Content.js
  1. var ServerPath = "http://localhost:62513/api/account";  
  2. chrome.runtime.sendMessage({  
  3. method: 'POST',  
  4. action: 'xhttp',  
  5. url: ServerPath + "/rapdata",  
  6. data: JSON.stringify({ data: data, fileName: fileName })  
  7. },function (responseText) {  
  8. //alert(responseText);  
  9. console.log("response of call saverapdata method");  
  10. /*Callback function to deal with the response*/  
  11. });  
Background.js
  1. chrome.runtime.onMessage.addListener(function (request, sender, callback) {  
  2. console.log("background js onMessage call.");  
  3. var resp = "done";  
  4. if (request.action == "xhttp") {  
  5. debugger;  
  6. //alert(request.data);url: ServerPath + "/rapdata",  
  7. var ServerPath = "http://localhost:62513/api/account";  
  8. $.ajax({  
  9. type: "POST",  
  10. url: request.url,  
  11. data: request.data,  
  12. contentType: "application/json; charset=utf-8",  
  13. dataType: "json",  
  14. success: function (data) {  
  15. debugger;  
  16. console.log(data);  
  17. //alert("Data: " + data);  
  18. },  
  19. failure: function (response) {  
  20. debugger;  
  21. console.log(response);  
  22. ///alert(response.d);  
  23. }  
  24. });  
  25. return true// prevents the callback from being called too early on return  
  26. }  
  27. });  
Answers (1)
1
Mehul Prajapati

Mehul Prajapati

NA 65 1.5k 7y
chrome.runtime.sendMessage have limited size to parameter. os i have used URL.createObjectURL to transfer data from content to background js.
 
Content.js 
 
var filedata = JSON.stringify({ data: data, fileName: fileName });
var save = new File([filedata], "filename.json", { type: "text/json;charset=utf-8" });
var postdata = window.URL.createObjectURL(save);
 
Background.js
 
var xhr = new XMLHttpRequest();
xhr.open('GET', postdata, true);
xhr.responseType = "text";
xhr.onload = function (e) {
   if (this.status == 200) {
      var myBlob = this.response;
      // myBlob is now the blob that the object URL pointed to.
   }
};
xhr.send();