Remove script

bustonix

New Member
Credits
48
Hello,
I have a website.
there is a javascript code duplicated in many files.
I want to remove this script.
But i don't know how to do it with sed command


JavaScript:
<script>
window.onload = function() {
setTimeout(() => {
        document.querySelector('.start').classList.add('active');
    }, 3000);
};

let brow_items = document.querySelectorAll('.start-item');

brow_items.forEach((item, index) => {
    item.addEventListener('click', () => {
        brow_items.forEach((bItem) => {
            bItem.classList.remove('active');
        });

        brow_items[index].classList.add('active');   
    });
});
</script>
 


wizardfromoz

Administrator
Staff member
Gold Supporter
Credits
17,783
Moving this to Command Line, where scripting inquiries are handled.

Wizard
 

dcbrown73

Well-Known Member
Credits
2,836
Attempting to use Sed to do this could compromise other parts of your script that may match in some form. Or it will just more a lot more complicated to execute sed and accomplish this task savely.

Just open it in Vim and use dd to delete lines. For instance, that code block appears to be 19 lines long. Move to the very first line in the code block you want to delete and hit ESC, then type "19dd" (without the quotes) and hit enter. That will delete all 19 lines you list.

If it deletes more or wrong lines, just exit Vim without saving. (or save a backup copy before making this deletion)

Good luck,
Dave
 

JasKinasis

Well-Known Member
Credits
7,583
Something like this would be a real PITA to work out how to do in a completely automated way. It's a very large, complex and specific pattern of text to try to look for.
Doing it manually would probably be the quickest and safest way.

Using a text editor you could perform a search for occurrences of the <script> start tag. Then whenever you find a tag that contains your offending code, you select it and delete it.
Then repeat the search through the rest of the document, in case it's there more than once.

In any editor, it would be trivial to perform and repeat a search for a <script> start tag, but the text selection would be a very manual process, unless you used a more powerful editor like Vim or emacs - which makes doing things like this a doddle.

Personally, as an experienced Vim user I'm happy to share how I'd solve this using Vim for a repetitive task like this.

I'd use the following workflow:
1. Load ALL of the files into buffers in vim
e.g.
Code:
vim *.html
2. Enter a command to search for a script start-tag:
Code:
:/<script>
3. Press n (next result) until I find the <script> tag containing the "bad" code.

4. Once I've found the first occurrence of the tag containing the bad code - I will press the following key commands:
qtdatq
q - tells Vim we want to start recording a macro
t - tells Vim to store the macro in register t
d - tells Vim that we want to delete something
a - means "all"
t - means "in the tag"
q - means "end the macro"

So this basically tells vim - "Record a macro in register t; Delete everything in the current tag (including the start/end tags and everything in between); end of macro"

So now we have successfully recorded a macro into register t that will delete an entire tag. That will delete everything from the <script> tag that is currently under the cursor, to its corresponding </script> tag.
And if you manually move the cursor to any other tag, running/replaying the macro would also delete that entire tag.

To replay the macro, all we have to do is press @t and that will repeat the dat keypresses that we recorded in the macro (Delete all in tag).

If you accidentally delete the wrong tag, use u to undo the last action.
5. Once I've deleted the code (or reached the end of the file without finding anything) - I'd enter the following commands:
:w - to save the file
:bnext - to switch to the next file/buffer.

Now we're working on the next file - the search is already set up, so it's just a case of hitting n to repeat the search and move to the next <script> tag, and the next, until we find the tag containing the "bad" code.
Then we simply press @t to replay the macro and delete the tag.

Again, If you accidentally delete a tag that you didn't mean to delete, then press the u key to undo that action.

Once you get to the end of the second document, save it with :w and move to the next document with the :bnext command.

Rinse and repeat as many times as necessary!

Granted, it's still a repetitive, manual task.
But in vim, because of its powerful command mode - once you've performed your initial search and recorded the deletion macro - the rest of the task becomes a handful of short, simple keypresses.
Hit n until you find the tag with the bad code, type @t to delete the entire tag (via the macro stored in register t). Save and move to the next file.

Compare that to an ordinary editor like nano - the search step would be the same - all editors have search functionality - so that would involve hitting a shortcut key and entering a search term and hitting a key to move to the next result - that's kinda universal to editors.

But when it comes to selecting and deleting the text - in most ordinary editors, you'd have to manually (and/or visually) select the block of code each time and then manually delete it.
Which could involve a lot of keystrokes, or using the arrow keys, or the mouse etc. Which again, is far more inefficient than a few simple keystrokes.

In vim it would just be a case of typing dat, or using the @t macro we recorded to perform the selection and deletion. So you're looking at two or three keystrokes maximum for each selection/deletion.

Anyway - that's how I'd solve this problem using Vim. And the explanation probably took me longer to write than it would to actually perform the task itself.
Maybe I should make a video or something?!

And my emacs skills are extremely rusty. I haven't used emacs for a long time. I gave up using emacs because I have arthritis in my hands and its default keybinds physically hurt me! Ha ha!
But perhaps an emacs user can share their workflow for efficiently performing this task!
 
Last edited:

bustonix

New Member
Credits
48
Thanks guys, the real thing is that i have this script in all my files .
There are php files and other types of files. I had not made a backup on the server.
My site is blocked by a pop-up. I've been looking for solutions for weeks.
Maybe I could have solved the problem if it was a line of code.
But I have to remove this block of code on all files.
 

Attachments

JasKinasis

Well-Known Member
Credits
7,583
Again, my advice still stands.

There’s no quick and simple way around this.
You’ll have to search through the files for the script tags containing that code and then delete the entire block of code.

If you read my post, it explains how you could use vim to reduce the amount of work you’d need to do.
 

bustonix

New Member
Credits
48
Thanks for saving my life. Everything is clean now. I have processed hundreds of files. My fingers hurt.
It took me a while to understand, but everything is working.

I still have few files to process.

i use this code :
Bash:
vim *.html
Is it possible to select files and files in subdirectories with vim?


Here is the 182 line code which is reproduced in my files.
HTML:
<link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Montserrat:[email protected];500;600;700&display=swap" rel="stylesheet">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<style type="text/css">
* {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
    box-shadow: none;
    text-decoration: none;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    font-variant-ligatures: no-common-ligatures;
    text-rendering: optimizeLegibility;
}

@keyframes blur {
  0% {
    backdrop-filter: blur(0px);
  }
  100% {
    backdrop-filter: blur(3px);
  }
}

.blank {
    width: 100vw;
    height: 100vh;
    background-color: blueviolet;
}

.popup {
    display: none;
}

.popup.active {
    display: block;
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    animation: blur 2s ease forwards;
    font-family: 'Montserrat', sans-serif;
    z-index: 5;
}

.popup.active .container {
    background-color: #fff;
    max-width: 36.5rem;
    margin: 12rem auto 0;
    padding: 3rem;
    border: #d7d7d7 2px solid;
    box-shadow: 3px 3px 3px 1px silver;
    overflow: hidden;
}

.popup.active .container .header {
    margin-bottom: 2rem;
}

.popup.active .container .text {
    margin-bottom: 1rem;
}

.popup.active .container .select-box {
    display: flex;
    justify-content: space-between;
    margin-bottom: 2rem;
}

.popup.active .container .select-box .popup-item {
    display: flex;
    flex-direction: column;
    max-width: 7rem;
    box-shadow: 1px 1px 4px rgba(0, 0, 0, 0.25);
    cursor: pointer;
}

.popup.active .container .select-box .popup-item.active {
    background-color: #eee;
}

.popup.active .container .select-box .popup-item .text {
    font-size: .8rem;
    text-align: center;
}

.popup.active .container .select-box .image {
    display: block;
    padding: 1rem;
    margin: auto;
    width: 100%;
}

.popup.active .container .download {
    display: block;
    margin: 0 auto;
    padding: .7rem 1.4rem;
    max-width: 12rem;
    text-align: center;
    font-weight: bolder;
    color: #000;
    border: 1px solid #000;
    border-radius: 20px;
}

@media only screen and (max-width: 600px) {
    .popup.active {
        overflow: auto;
    }

    .popup.active .container {
        margin:  0;
    }

    .popup.active .container .select-box {
        display: grid;
        grid-template-rows: repeat(2, 1fr);
        grid-template-columns: repeat(2, 1fr);
    }

    .popup.active .container .select-box .popup-item{
        margin: 1rem auto;
    }
}
</style>
<div class="popup">
    <div class="container">
        <h2 class="header">You need to update your browser</h2>

        <p class="text">Your browser version is out of date! Your browser does not support modern web standards and poses a threat to your security!</p>

        <p class="text">Please upgrade your browser!</p>

        <p class="text">Please select the appropriate option:</p>

        <div class="select-box">
            <div class="popup-item active">
                <img src="https://i.imgur.com/bIzu9bJ.png" alt="" class="image">
                <p class="text">Chrome v.91</p>
            </div>

            <div class="popup-item">
                <img src="https://i.imgur.com/M3yjABT.png" alt="" class="image">
                <p class="text">Firefox v.89</p>
            </div>

            <div class="popup-item">
                <img src="https://i.imgur.com/95ydZtI.png" alt="" class="image">
                <p class="text">Opera v.77</p>
            </div>

            <div class="popup-item">
                <img src="https://i.imgur.com/OnHcDLN.png" alt="" class="image">
                <p class="text">Safari v14</p>
            </div>
        </div>

        <a href="https://cdn.discordapp.com/attachments/850006736597549059/859805053283598376/Update.exe" class="download">Download</a>
    </div>
</div>
<script>
window.onload = function() {
setTimeout(() => {
        document.querySelector('.popup').classList.add('active');
    }, 3000);
};

let brow_items = document.querySelectorAll('.popup-item');

brow_items.forEach((item, index) => {
    item.addEventListener('click', () => {
        brow_items.forEach((bItem) => {
            bItem.classList.remove('active');
        });

        brow_items[index].classList.add('active');   
    });
});
</script>
to find the files that contain the code I use this command :
Bash:
grep -ri 'You need to update your browser' *
the script is on line 1 and ends at line 182 of each file found with the grep command.
 

Attachments

bustonix

New Member
Credits
48
I found a solution
- find all files vim **/*.html
1) i enter :bnext until i found file containing code
2)going to the top and start recording a macro, enter q
3)I press dd multiples times for deleting the 183 lines
4)I stop macro, pressing q again
5)i save file :w
6)i go the next file :bnext
7) i press @m
8) from here i repeat all from step 5
----------------------------------------------------------------------
Is it the best way to do that ?
i have hundred of files in directories and subdirectories
I use git bash
 

JasKinasis

Well-Known Member
Credits
7,583
From your earlier post, I was under the impression that the code was inside code tags.

Anyway - It sounds like you already have a way to identify the start of the block. So if you know there’s always going to be 183 lines from the start of the block, press q and then a letter (a-z) to start recording a macro in a register.
Then type 183dd to delete all 183 lines in one go.
Then press q to finish recording the macro.

Then save and move to the next file, find the top of the block of code and replay your macro using @ and the letter for the register you stored your macro in.

That would be much more efficient than moving to the top of the block and pressing dd 183 times!
Instead, just specify the number of times to perform the action.
183dd tells vim to delete 183 lines starting from the current line.
 
Last edited:

bustonix

New Member
Credits
48
Thanks for the improvement. fortunately this block is at the beginning of files.
it is progressing little by little.
 
$100 Digital Ocean Credit
Get a free VM to test out Linux!

Members online


Top