Skip to content

Commit

Permalink
adiciona possibilidade de remover e renovar o script que já está na tela
Browse files Browse the repository at this point in the history
  • Loading branch information
HenriqueVilela committed Sep 29, 2022
1 parent f5c3fe9 commit 7244a96
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
12 changes: 10 additions & 2 deletions front/src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { Component } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
export class AppComponent implements OnInit {
title = 'front';
constructor(
public router: Router
) {}

ngOnInit(): void {
this.router.navigate(['/main']);
}
}
2 changes: 0 additions & 2 deletions front/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { CustomScriptInjectComponent } from './components/custom-script-inject/custom-script-inject.component';
import { MainPageComponent } from './components/main-page/main-page.component';
import { SubPageComponent } from './components/sub-page/sub-page.component';
import { MatButtonModule } from '@angular/material/button';
Expand All @@ -13,7 +12,6 @@ import { MatButtonModule } from '@angular/material/button';
@NgModule({
declarations: [
AppComponent,
CustomScriptInjectComponent,
MainPageComponent,
SubPageComponent
],
Expand Down
10 changes: 3 additions & 7 deletions front/src/app/components/main-page/main-page.component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Component, OnInit, Input, AfterViewInit, Renderer2 } from '@angular/core';
import { Component, OnInit} from '@angular/core';
import { InjectCustomScriptService } from '../../services/custom-script/inject-custom-script.service'

@Component({
selector: 'main-page',
templateUrl: './main-page.component.html',
styleUrls: ['./main-page.component.scss']
})
export class MainPageComponent implements OnInit, AfterViewInit {
export class MainPageComponent implements OnInit {


script = '<script>console.log(\'script da pagina principal executando\');</script>';
Expand All @@ -17,11 +17,7 @@ export class MainPageComponent implements OnInit, AfterViewInit {

ngOnInit(): void {
console.log("Você entrou na pagina principal");
this.injectCustomScriptService.setScriptToHeader(this.script)
}

ngAfterViewInit(): void {

this.injectCustomScriptService.setScriptToHeader(this.script, true)
}

}
8 changes: 2 additions & 6 deletions front/src/app/components/sub-page/sub-page.component.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Component, OnInit, Input, AfterViewInit } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import { InjectCustomScriptService } from 'src/app/services/custom-script/inject-custom-script.service';

@Component({
selector: 'sub-page',
templateUrl: './sub-page.component.html',
styleUrls: ['./sub-page.component.scss']
})
export class SubPageComponent implements OnInit, AfterViewInit {
export class SubPageComponent implements OnInit {

script = `<script>console.log(\'script da pagina secundaria executando\');</script>`;

Expand All @@ -19,8 +19,4 @@ export class SubPageComponent implements OnInit, AfterViewInit {
this.injectCustomScriptService.setScriptToBody(this.script)
}

ngAfterViewInit(): void {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ export class InjectCustomScriptService {
constructor(
) { }

public setScriptToBody(data: string): void {
console.log(document.getElementsByClassName("script_personalizado_body").length);
if (document.getElementsByClassName("script_personalizado").length === 0) {
public setScriptToBody(data: string, $replaceSript: boolean = false): void {
if ($replaceSript) {
this.removeOldScript("body");
}
if (document.getElementsByClassName("script_personalizado_body").length === 0) {
$(data).toArray().forEach(element => {
if (element.tagName === "SCRIPT" || element.tagName === "NOSCRIPT") {
element.classList.add("script_personalizado_body");
Expand All @@ -22,7 +24,10 @@ export class InjectCustomScriptService {
}
}

public setScriptToHeader(data: string): void {
public setScriptToHeader(data: string, $replaceSript: boolean = false): void {
if ($replaceSript) {
this.removeOldScript("header");
}
if (document.getElementsByClassName("script_personalizado_header").length === 0) {
$(data).toArray().forEach(element => {
if (element.tagName === "SCRIPT" || element.tagName === "NOSCRIPT") {
Expand All @@ -33,4 +38,11 @@ export class InjectCustomScriptService {
}
}

private removeOldScript($location: string) {
const elements = document.getElementsByClassName("script_personalizado_" + $location);
while(elements.length > 0) {
elements[0].remove();
}
}

}

0 comments on commit 7244a96

Please sign in to comment.