Snarling / bitburner-scripts

Various scripts for the game Bitburner, available on steam or at https://danielyxie.github.io/bitburner/
GNU General Public License v3.0
36 stars 5 forks source link

Adding item with same name creates it without removing previous #6

Open viktorbarati opened 2 years ago

viktorbarati commented 2 years ago

https://github.com/Snarling/bitburner-scripts/blob/1776dec09ce1e69dc4dd8ebc3e24555b1b26beed/box/box.js#L17

Maybe this is a suggestion rather than an issue, but createSideBar item could check whether an item with a same name already exists and remove the previous one if it does. This way we can "update" items easily without having to manually close them.

Snarling commented 2 years ago

You can already update items by just updating the content in the original item, instead of making an entirely new item with the new content.

There are use cases where someone would want multiple items with the same title (e.g. "stock ticker") so I won't force close things that have duplicate names.

Snarling commented 2 years ago

If the issue you're running into is that the game is killing your script when you install augments (which will happen for scripts that require access to ns), you can work around this a few ways:

  1. If your script is only using ns for access to ns.sleep, then you can just use the slp function included in box.js as a replacement, or use an interval similar to timekeeper.js.
  2. If your script actually needs ns, you can use the game's ns.atExit function to close your box Element when the script is exited in game. ns.atExit(item["remove"]) should work.
  3. If you don't want to remove the item when the script is killed, then when you launch your new script you can search for the old one and update it's contents, and only add a new item if you couldn't find the original. This is a lot easier if you add an extra class to your item to identify it. There's a couple ways you could do it but here's what I would do if I needed that "replace contents if the item is already there" functionality.
let content = "Sample Content";
let hackMgr = doc.querySelector(".item.hackman")||createSidebarItem("Hack Manager", "", "\ueabe", "hackman");
hackMgr.body.innerHTML=content; //Replaces the body content whether it's an old or new item
viktorbarati commented 2 years ago

I came to this issue because I was creating a sidebar item and making small modifications to the layout, everytime rerunning the script which creates it after each change. I see now I can add extra classes and then query that indeed.

On Wed, 16 Feb 2022 at 18:11, Snarling @.***> wrote:

If the issue you're running into is that the game is killing your script when you install augments (which will happen for scripts that require access to ns), you can work around this a few ways:

  1. If your script is only using ns for access to ns.sleep, then you can just use the slp function included in box.js as a replacement, or use an interval similar to timekeeper.js.
  2. If your script actually needs ns, you can use the game's ns.atExit function to close your box Element when the script is exited in game. ns.atExit(item["remove"]) should work.
  3. If you don't want to remove the item when the script is killed, then when you launch your new script you can search for the old one and update it's contents, and only add a new item if you couldn't find the original. This is a lot easier if you add an extra class to your item to identify it. There's a couple ways you could do it but here's what I would do if I needed that "replace contents if the item is already there" functionality.

let content = "Sample Content"; let hackMgr = doc.querySelector(".item.hackman")||createSidebarItem("Hack Manager", "", "\ueabe", "hackman"); hackMgr.body.innerHTML=content; //Replaces the body content whether it's an old or new item

— Reply to this email directly, view it on GitHub https://github.com/Snarling/bitburner-scripts/issues/6#issuecomment-1041960758, or unsubscribe https://github.com/notifications/unsubscribe-auth/AMULV5TJPYGQVL3QQGZVRE3U3PSDFANCNFSM5OR66DRQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you authored the thread.Message ID: @.***>

RobLeachman commented 2 years ago

If someone comes along later, now "replace contents if already there" seems to rely on let hackMgr = doc.querySelector(".sbitem.hackman") not (".item.hackman")

Snarling commented 2 years ago

Still won't be changing the way this works, but I reopened the issue to improve visibility on the workaround:

let title = "Sample Box",
  content = "This box was initialized at " + new Date().toLocaleTimeString("en-gb"), //Just sample content
  icon = "\ueabe",
  uniqueClass = "samplebox", //a unique class name is needed so an existing box or sidebar item can be selected.
  box;

if (doc.querySelector("."+uniqueClass)){
  box = doc.querySelector("."+uniqueClass);
  box.body.innerHTML=content;
} else box=createBox(title, content, icon, uniqueClass);