What is the best way to modify the value of a variable after the player clicks the link but before the next passage evaluates that var? (Chapbook)

I think this is not the same bug–issue with the code is that it’s inserting the value of set directly into HTML without escaping the quotation marks around "hello" in the example, so it returns HTML that closes the attribute too soon: <a ... data-set="name: "hello"">.

Here’s a tweak that works for me and also avoids the issue of having to use prependListener:

engine.extend('1.0.0', () => {
	engine.event.addListener('dom-click', el => {
		const setters = el.dataset.cbSetVars;
		const destination = el.dataset.cbSetVarsDestination;

		if (setters) {
			engine.render(setters.replace(/;/g, '\n') + '\n--');
		}

		if (destination) {
			go(destination);
		}
	});

  config.template.inserts = [
    {
      match: /^set\s+link/i,
      render(passage, props) {
        if (passage) {
          const link = document.createElement('a');

          link.setAttribute('href', 'javascript:void(0)');
          link.dataset.cbSetVarsDestination = passage;
          link.dataset.cbSetVars = props.set;
          link.innerText = props.label;
          return link.outerHTML;
        }
      }
    },
    ...config.template.inserts
  ];
});

3 Likes