Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inject scripts error: Script returned non-structured-clonable data on Firefox extension

I want to inject script from firefox web extension to tabId thought browser.tabs.executeScript API.

I have a file Browser.js

MyFunc.Browser = (function() {
  var self;

  function Browser() {
    self = this;
  }

  Browser.getExtensionURI = function() {
    return "chrome-extension://";
  };

  return Browser;

})();

And execute script function:

var executing = browser.tabs.executeScript(tabId, {
            file: "js/contentscripts/Browser.js"
          });
 executing.then(function(results) {
    console.log("url: " + tabUrl + ", result", results);

 }, function(error) {
    return console.log("Inject scripts error: " + error);
 });

But script cannot inject to tab and show error.

How I can fix it?

Manifest file:

{
  "name": "abc",

  "background": {
    "page": "background.html"
  },
  "browser_action": {
    "default_icon": "icons/icon_19.png",
    "default_popup": "login.html",
  },
  "content_scripts": [
    {
      "web_accessible_resources": [


        "js/contentscripts/Browser.js",

      ],
      "js": [
        "js/contentscripts/ContentScript.js"
      ],
      "matches": [
        "file://*/*",
        "http://*/*",
        "https://*/*"
      ],
      "run_at": "document_end",
      "all_frames": true
    },
    {
      "js": [

        "js/contentscripts/Browser.js",
      ],
      "matches": [
        "file://*/*",
        "http://*/*",
        "https://*/*"
      ],
      "run_at": "document_start",
      "all_frames": true
    }
  ],
  "icons": {
    "16": "icons/icon_16.png",
    "19": "icons/icon_19.png"
  },
  "incognito": "spanning",
  "permissions": [
    "activeTab",
    "tabs",
    "http://*/*",
    "https://*/*",
    "<all_urls>"
  ],
  "content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
  "version": "1.1.16"
}
like image 806
Tam Vo Avatar asked Jun 15 '17 12:06

Tam Vo


2 Answers

in "js/contentscripts/Browser.js" file, add "undefined;" to last line.
the value will return to result of "executing.then" first callback argument
reference:https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/tabs/executeScript#Return_value

like image 97
Zero0evolution Avatar answered Nov 01 '22 19:11

Zero0evolution


I think I understand your issue now.

The return data of your executeScript has to be structured clonable.
In order to be considered structured clonable the data has to match one of these data types:

  • All primitive types - However not symbols
  • Boolean object
  • String object
  • Date
  • RegExp - The lastIndex field is not preserved.
  • Blob
  • File
  • FileList
  • ArrayBuffer
  • ArrayBufferView - This basically means all typed arrays like Int32Array etc.
  • ImageData
  • Array
  • Object - This just includes plain objects (e.g. from object literals)
  • Map
  • Set

https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm#Supported_types

In your case the problem is that you are returning an object that has functions in it's properties. Thus it is non-structured-clonable, which explains your error.

like image 36
Forivin Avatar answered Nov 01 '22 19:11

Forivin