Asked  6 Months ago    Answers:  5   Viewed   76 times

I've got the following...

chrome.extension.sendRequest({
  req: "getDocument",
  docu: pagedoc,
  name: 'name'
}, function(response){
  var efjs = response.reply;
});

which calls the following..

case "getBrowserForDocumentAttribute":
  alert("ZOMG HERE");
  sendResponse({
    reply: getBrowserForDocumentAttribute(request.docu,request.name)
  });
  break;

However, my code never reaches "ZOMG HERE" but rather throws the following error while running chrome.extension.sendRequest

 Uncaught TypeError: Converting circular structure to JSON
 chromeHidden.JSON.stringify
 chrome.Port.postMessage
 chrome.initExtension.chrome.extension.sendRequest
 suggestQuery

Does anyone have any idea what is causing this?

 Answers

66

It means that the object you pass in the request (I guess it is pagedoc) has a circular reference, something like:

var a = {};
a.b = a;

JSON.stringify cannot convert structures like this.

N.B.: This would be the case with DOM nodes, which have circular references, even if they are not attached to the DOM tree. Each node has an ownerDocument which refers to document in most cases. document has a reference to the DOM tree at least through document.body and document.body.ownerDocument refers back to document again, which is only one of multiple circular references in the DOM tree.

Tuesday, June 1, 2021
 
insomiac
answered 6 Months ago
67

JSON doesn't accept circular objects - objects which reference themselves. JSON.stringify() will throw an error if it comes across one of these.

The request (req) object is circular by nature - Node does that.

In this case, because you just need to log it to the console, you can use the console's native stringifying and avoid using JSON:

console.log("Request data:");
console.log(req);
Monday, August 9, 2021
 
Philippe
answered 4 Months ago
18

Pardon me if this is too obvious. At the time of writing, I dont know what you have tried.

insert

console.log(the object); 

replacing 'the object' with the object you are passing to JSON.stringify()

insert this line before the JSON.stringify call

and look in the console log (shift control J) for the object. In the console log the object will be tagged with a ">" symbol which can be clicked to expand to the fields.

It is complaining about an object that has pointers into itself, like this kind of object:

A = [];
A[0] = A; 
JSON.stringify(A); // circular error
Monday, August 9, 2021
 
0skar
answered 4 Months ago
18

You have one fundamental misunderstanding: your background script can't modify the webpage, because document refers to the background page itself.

Take a look at the Architecture overview. After you do, you should know that you need a Content Script.

Once we've established that, your content script can independently track where the click happened - and that would contain a reference to the element. Then, when your background requests a modification, you know the last such element.

There is a very good question discussing your problem in great detail too.

Sunday, October 17, 2021
 
vuuduu
answered 2 Months ago
53

The current API for omnibox suggestions requires that they be specified as encoded XML text, not just plain text. Some characters including & will need to be appropriately encoded.

To encode an entire XML string in browser JavaScript, you may do something like this:

function encodeXml(s) {
  var holder = document.createElement('div');
  holder.textContent = s;
  return holder.innerHTML;
}

console.log(encodeXml("Good & Bad > Bad & Good"));
// "Good & Bad > Bad & Good"

If you perform this operation on your text before passing it to the omnibox API, the error should go away.

Per the documentation you can use <url>, <match>, and <dim> to further annotate your result. However, you may want to use a more structured XML-building approach for that, rather than simply concatenating strings. (I don't know if these XML elements have any attributes, but if they do, the approach above may not be adequate for encoding attribute values.)

Sunday, November 21, 2021
 
Mr.D
answered 1 Week ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share