What do I change in the Blockly JSON.7z code to implement “querySelector”?

I am on a steep learning curve learning code to create a website implementing Blockly → https://developers.google.com/blockly/. A good example of a Blockly end result → https://blockly.games/maze?lang=en

All comments in this post relate to two zipped files located at my website https://temp.foxping.com/ → Blockly XML.7z and Blockly JSON.7z.

Those two files are different from each other. But they are relevant to the questions.

==========
In Blockly XML.7z → HTML file

<body mode="maker">
   ...
  <main>
    ...  
    <button class="mode-maker mdl-button" id="edit">Edit</button>
      ...
      <div class="maker">
        <div>
          <div class="button mdl-color--amber-500">1</div>
          <div class="button mdl-color--yellow-500">2</div>
          <div class="button mdl-color--lime-500">3</div>
        </div>
        ...
      </div>
      ...
  </main>
  ...
</body>

In Blockly XML.7z → main.js file

document.querySelector('#edit').addEventListener('click', enableEditMode);
...
function enableEditMode() {
  document.body.setAttribute('mode', 'edit');
  document.querySelectorAll('.button').forEach(btn => {
    btn.removeEventListener('click', handlePlay);
    btn.addEventListener('click', enableBlocklyMode);
  });
}

The Blockly XML.7z code is using XML.

==========

In Blockly JSON.7z → HTML file

<body>
  <div id="blocklyDiv"></div>
  <script>
    var toolbox = 
    {
      "kind": "categoryToolbox",
      "contents": [
        {
          "kind": "category",
          "name": "Core",
          "contents": [
            {
              "kind": "block",
              "type": "controls_if"
            },
            {
              "kind": "block",
              "type": "logic_compare"
            },
           ]
         },
        ...
  </script>
</body>

The Blockly JSON.7z code is using JSON.

What do I change in the Blockly JSON.7z code to implement “querySelector” and “setAttribute”?

1 Like

I’m unfamiliar with Blockly. Does it work with these compressed files, or have you zipped them up to make them available for people to download?

It isn’t. It contains HTML, CSS and JS, as well as several m4a files.

It isn’t. It contains JavaScript files, a CSS file and an SVG.

Sorry if it seems picky to point this out, but it is important to understand what we are working with.

Decide which element in your HTML you want to target with querySelector, e.g. the blocklyDiv.

Then add your code at the bottom of the HTML file inside the closing script tag:

  const blocklyArea = document.querySelector('#blocklyDiv');
  // Do something with blocklyArea here
  blocklyArea.style.border = '5px solid red';
</script>

I’m not sure if that helps at all. Maybe post back with a bit more on what you are trying to acheive.

2 Likes

I zipped the files to make it easy to download. Blockly works when they are unzipped.

==========
Regarding JSON, I found this at https://developers.google.com/blockly/guides/configure/web/toolbox#jsonhttps://snipboard.io/VSY19f.jpg — and → https://snipboard.io/MS65Ky.jpg.

Perhaps labeling this as JSON is an error?

==========
Sorry for the confusion. I named the Blockly XML.7z incorrectly. I’ve renamed it to “Blockly not XML.7z”.

The XML folder is labeled “Blockly is XML.7z”. This does contain XML.

==========
In"Blockly JSON.7z" → index.html I inserted

  <body>
    <div id="blocklyDiv"></div>
    <script>
      var toolbox = 
      {
        "kind": "categoryToolbox",
        "contents": [
          {
            "kind": "category",
            "name": "Core",
            "contents": [
              {
                "kind": "block",
                "type": "controls_if"
              },
              {
                "kind": "block",
                "type": "logic_compare"
              },
            ]
          },
          {
            "kind": "category",
            "name": "Custom",
            "contents": [
              {
                "kind": "block",
                "type": "start"
              },
              {
                "kind": "category",
                "name": "Move",
                "contents": [
                  {
                    "kind": "block",
                    "type": "move_forward"
                  }
                ]
              },
              {
                "kind": "category",
                "name": "Turn",
                "contents": [
                  {
                    "kind": "block",
                    "type": "turn_left"
                  }
                ]
              }
            ]
          }
        ]
      }
      var workspace = Blockly.inject('blocklyDiv', {toolbox: toolbox});
    </script>
  </body>

When I run “Blockly JSON.7z” → index.html → https://snipboard.io/ToY9UW.jpg.

When I select the “Core” icon → https://snipboard.io/uvz61X.jpg.

When I select the “Custom” icon → https://snipboard.io/qkpXCi.jpg.

The code does not know how to handle “start”.

           "kind": "category",
            "name": "Custom",
            "contents": [
              {
                "kind": "block",
                "type": "start"
              },

After studying “Blockly not XML.7z”, it appeared to me that “querySelector” and “setAttribute” are needed to handle ‘start’.

What is needed to handle ‘start’?

1 Like

¯_(ツ)_/¯

In the screenshot it would be fair enough to call it JSON. In the context of your code it was being used as an object. In this case the difference is negligible though, so sorry for the confusion.

:tongue:

I’ve never used Blockly, so I’m not really sure. But I’d be surprised if it had anything to do with querySelector or setAttribute.

If you search for the error message, there are a couple of hits:

If those suggestions don’t help you, I would advise asking on the blockly forum.

See Blockly Forum → https://groups.google.com/g/blockly/c/OYASZ0GptUU

A while back, I created an HTML / JavaScript program that takes action with a keyboard entry →

document.body.addEventListener("keydown", e =>

See Javascript html - “All Directions Car on Street.7z” at https://temp.foxping.com/. This is not Blockly.

Enter “f”, and the car moves forward.

Is there something similar to the Blockly JSON.7z code that takes action, based on the item selected on the screen, instead of which key was entered?

AFAICT this is a different question to the one you are asking here.

It would be better to ask a clear question which has a clear answer. As you see, the one person that responded to you on the Blockly Forum is not sure what you are asking either.

In the same vein, I’m not sure what that means. As mentioned previously, I have no experience with Blockly and I’m afraid I don’t have the time to wade through your code to try and understand what you are asking.

If you can rephrase your question to something that I can understand and that has an answer, I’ll do my best to help.

Being new to JavaScript has been a handicap, but I have been able to write HTML / JavaScript code that creates 2D and 3D images on a browser.

Over a span of 40 years, it commonly takes me a couple of weeks to learn the fundamentals of languages (Fortran → C#). Over a span of three months, I have not been able to understand Blockly well enough to write some basic code.

This is the latest response on the Blockly Google Groups Forum → https://groups.google.com/g/blockly/c/OYASZ0GptUU. It is common to see responses that reference a website, with no code examples.

Are you aware of any other forums for Blockly? Thanks for your responses.

I now know that the key is “Blockly.defineBlocksWithJsonArray()” at https://developers.google.com/blockly/guides/configure/web/custom-blocks#json_array. This works now. Thanks for your patience!

I’m still not really sure what the question is/was, but I’m glad to hear that you are making progress :slight_smile:

Thanks for the link to StackOverflow! I posted on that forum — https://stackoverflow.com/questions/77703751/what-code-do-i-add-to-a-js-file-to-define-start-to-fix-the-invalid-block-def/77704530#77704530 — and got the answer!

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.