4

I am dealing with a dilemma here and I cannot find a solution. Right now I have multiple PHP files with same <script></script> tag code which contains TAWK API code. Now I want to remove that particular <script></script> tag.

I tried a few solutions which didn't work.

  1. Selecting that script tag and remove it

The way I did this is

document.getElementByTagName('script')[index].innerText

The problem here is that this particular script tag is placed at random places at any given page, so I cannot put a static index number.

  1. IIFE to hide the Widget

TAWK API provides a method to hide the widget itself i.e.

Tawk_API.hideWidget() but the same problem I will have to write this code in each file as TAWK API's code isn't written in one file and in each file TAWK API's code is written at last. But in some file footer.php or analytics.php occur before this which shifts the index of this script tag so this will also not work.

Any suggestions or workaround for this problem?

TL; DR: Remove script tags from multiple PHP files, but the script tag is at random location with any id.

Yash Karanke
  • 764
  • 1
  • 15
  • 29
  • 3
    By the time you're removing the script tag via JS, its code might have already been executed. So it would be better to prevent the tag from being ouput on the PHP side of things. – Constantin Groß Apr 11 '19 at 07:06
  • Is there any reason why you can't just delete the code from the files? – Azer Apr 11 '19 at 07:08
  • @Azer Yes, there are around 50 files and I want a smart solution rather repeating – Yash Karanke Apr 11 '19 at 07:13
  • @Connum `Tawk_API.hideWidget` will hide the widget which is rendered even after script execution – Yash Karanke Apr 11 '19 at 07:13
  • @YashKaranke If you use VS Code (or almost any other editor) you can do a search and replace for the script tags in the files and replace them all with `''` in pretty much the click of a button. Seems much faster than writing more code to remove unwanted code. – Azer Apr 11 '19 at 07:21
  • @Azer Yes I am aware of that but I am doing this on cPanel, which means I'll have to get files via FTP first make changes and upload them, and I'm not sure which files this code exists – Yash Karanke Apr 11 '19 at 07:26
  • @YashKaranke Just zip and download the whole root directory. This can be done in cPanel's file manager. No need to connect via ftp. Then just run a search and replace locally on the whole directory and upload again. (keep a back up of the original files to be safe.) It will take you like 5 minutes. Seems like the easiest solution to me. If there's a reason why this can't be done then please explain. The ideas that you provide in your question seems very hacky. If you don't want the code to appear in the DOM, then don't put it there in the first place. – Azer Apr 11 '19 at 07:39
  • @Azer Yes, the question seems quite a bit hacky, and it will take ages to download the entire root directory on my internet connection, I'll just manually remove the code. thanks :) – Yash Karanke Apr 11 '19 at 07:44
  • I see. If you have linux shell access you can do a search and replace remotly: https://www.cyberciti.biz/faq/how-to-use-sed-to-find-and-replace-text-in-files-in-linux-unix-shell/ – Azer Apr 11 '19 at 07:47
  • @Azer I do not have shell access – Yash Karanke Apr 11 '19 at 07:52

0 Answers0