From 7244a9627c47f4f9bd6fc766416467e56e5e3dbb Mon Sep 17 00:00:00 2001 From: HenriqueVilela Date: Thu, 29 Sep 2022 12:09:13 -0300 Subject: [PATCH] =?UTF-8?q?adiciona=20possibilidade=20de=20remover=20e=20r?= =?UTF-8?q?enovar=20o=20script=20que=20j=C3=A1=20est=C3=A1=20na=20tela?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- front/src/app/app.component.ts | 12 +++++++++-- front/src/app/app.module.ts | 2 -- .../main-page/main-page.component.ts | 10 +++------- .../components/sub-page/sub-page.component.ts | 8 ++------ .../inject-custom-script.service.ts | 20 +++++++++++++++---- 5 files changed, 31 insertions(+), 21 deletions(-) diff --git a/front/src/app/app.component.ts b/front/src/app/app.component.ts index 317c226..92cc9d9 100644 --- a/front/src/app/app.component.ts +++ b/front/src/app/app.component.ts @@ -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']); + } } diff --git a/front/src/app/app.module.ts b/front/src/app/app.module.ts index 6be21a1..7b8966e 100644 --- a/front/src/app/app.module.ts +++ b/front/src/app/app.module.ts @@ -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'; @@ -13,7 +12,6 @@ import { MatButtonModule } from '@angular/material/button'; @NgModule({ declarations: [ AppComponent, - CustomScriptInjectComponent, MainPageComponent, SubPageComponent ], diff --git a/front/src/app/components/main-page/main-page.component.ts b/front/src/app/components/main-page/main-page.component.ts index b86f2ac..895063f 100644 --- a/front/src/app/components/main-page/main-page.component.ts +++ b/front/src/app/components/main-page/main-page.component.ts @@ -1,4 +1,4 @@ -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({ @@ -6,7 +6,7 @@ import { InjectCustomScriptService } from '../../services/custom-script/inject-c templateUrl: './main-page.component.html', styleUrls: ['./main-page.component.scss'] }) -export class MainPageComponent implements OnInit, AfterViewInit { +export class MainPageComponent implements OnInit { script = ''; @@ -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) } } diff --git a/front/src/app/components/sub-page/sub-page.component.ts b/front/src/app/components/sub-page/sub-page.component.ts index a456759..9d3306c 100644 --- a/front/src/app/components/sub-page/sub-page.component.ts +++ b/front/src/app/components/sub-page/sub-page.component.ts @@ -1,4 +1,4 @@ -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({ @@ -6,7 +6,7 @@ import { InjectCustomScriptService } from 'src/app/services/custom-script/inject templateUrl: './sub-page.component.html', styleUrls: ['./sub-page.component.scss'] }) -export class SubPageComponent implements OnInit, AfterViewInit { +export class SubPageComponent implements OnInit { script = ``; @@ -19,8 +19,4 @@ export class SubPageComponent implements OnInit, AfterViewInit { this.injectCustomScriptService.setScriptToBody(this.script) } - ngAfterViewInit(): void { - - } - } diff --git a/front/src/app/services/custom-script/inject-custom-script.service.ts b/front/src/app/services/custom-script/inject-custom-script.service.ts index f16a45b..49985f2 100644 --- a/front/src/app/services/custom-script/inject-custom-script.service.ts +++ b/front/src/app/services/custom-script/inject-custom-script.service.ts @@ -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"); @@ -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") { @@ -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(); + } + } + }