Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8660855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:21:47+00:00 2026-06-12T16:21:47+00:00

Now, I’m working with userscript for a browser game. And I have some problems

  • 0

Now, I’m working with userscript for a browser game. And I have some problems with sandbox and initialization.
As a solution, I found a way with creating a new element (script) and inserting the object in element.

var script = document.createElement("script");
script.type = "text/javascript";
script.innerHTML = "var Class = { ... }";
document.body.appendChild(script);

But I have a very large object and convert it to a string rather problematic and time consuming.
I have an object that looks like this:

{
addToStorage : Function,
getFromStorage : Function,
getID : Function
settings : Object,
langs : Object,
init : Function,
version : Number,
author: String
}

Any ideas, how to convert it to string or insert into global window?

Sorry for my bad English.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:21:48+00:00Added an answer on June 12, 2026 at 4:21 pm

    In Firefox, you can use the .toSource() function on objects. So the answer is ditch Chrome and switch to Firefox. (^_^)

    But assuming you don’t want to do that, the answer gets messy, as even JSON.stringify() won’t work if the object as function properties, like yours does.

    So, you can inject your object source using a multiline string (see the link for a variety of techniques).

    Or you can make or use a an object stringifier like objToString, below. Warning, objToString is just a quick and dirty first attempt, you might be smarter to grab something like JavaScript var_export, instead.

    var myObjX =  {
        someFunction:   function () {console.log ("foo");},
        settings:       {init: 2},
        langs:          {a: "En", b: "Jp"},
        version:        99,
        author:         "Anon"
    }
    
    function objToString (obj) {
        var str = '{ ';
        for (var p in obj) {
            if (obj.hasOwnProperty (p) ) {
                str += p + ': ';
                switch (typeof obj[p]) {
                    case "object":
                        str += objToString (obj[p]);
                        break;
                    case "string":
                        str += '"' + obj[p] + '"';
                        break;
                    default:
                        str += obj[p];
                        break;
                }
                str += ',\n';
            }
        }
        return str.slice (0, str.length-2) + ' }';
    }
    
    var script          = document.createElement("script");
    script.type         = "text/javascript";
    script.textContent  = "var injectedX = " + objToString (myObjX);
    document.body.appendChild (script);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

now i'm working on a project for creating audio unit instrument that provide the
Now I am creating an address book in Java 1.6. Now how I have
Now facebook require to have these options when you want to use bulit-in actions
Now I have a long query and I'd like to alternate with such a
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
Now I have an XSD file (e.g. FOO_SCHEMA.xsd) that looks something like this: <xs:schema
Now I have been trying for fun to make my own version of the
Now I am learning Codeigniter. I have more databases, so I choosed this time
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.