Skip to content

Commit

Permalink
encontrado forma legal de fazer a incerção do script
Browse files Browse the repository at this point in the history
  • Loading branch information
HenriqueVilela committed Sep 28, 2022
1 parent 11fe000 commit 8754157
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 41 deletions.
19 changes: 7 additions & 12 deletions front/src/app/components/main-page/main-page.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,16 @@ import { InjectCustomScriptService } from '../../services/custom-script/inject-c
export class MainPageComponent implements OnInit, AfterViewInit {


constructor(
injectCustomScriptService: InjectCustomScriptService,
renderer2: Renderer2
) {
injectCustomScriptService.setScriptToHeader(renderer2, this.script)
}
script = '<script>console.log(\'script da pagina secundaria executando\');</script>';

script = `
<script>
console.log('script da pagina principal executando');
</script>
`;
constructor(
public injectCustomScriptService: InjectCustomScriptService
) {}

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

ngAfterViewInit(): void {
Expand Down
28 changes: 23 additions & 5 deletions front/src/app/components/sub-page/sub-page.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, OnInit, Input, AfterViewInit, Renderer2 } from '@angular/core';
import { Component, OnInit, Input, AfterViewInit } from '@angular/core';
import { InjectCustomScriptService } from 'src/app/services/custom-script/inject-custom-script.service';

@Component({
Expand All @@ -8,17 +8,35 @@ import { InjectCustomScriptService } from 'src/app/services/custom-script/inject
})
export class SubPageComponent implements OnInit, AfterViewInit {

script = '<script>console.log(\'script da pagina secundaria executando\');</script>';
script = `
<!-- Meta Pixel Code -->
<script>
!function(f,b,e,v,n,t,s)
{if(f.fbq)return;n=f.fbq=function(){n.callMethod?
n.callMethod.apply(n,arguments):n.queue.push(arguments)};
if(!f._fbq)f._fbq=n;n.push=n;n.loaded=!0;n.version='2.0';
n.queue=[];t=b.createElement(e);t.async=!0;
t.src=v;s=b.getElementsByTagName(e)[0];
s.parentNode.insertBefore(t,s)}(window, document,'script',
'https://connect.facebook.net/en_US/fbevents.js');
fbq('init', '457526166340322');
fbq('track', 'PageView');
</script>
<noscript><img height="1" width="1" style="display:none"
src="https://www.facebook.com/tr?id=457526166340322&ev=PageView&noscript=1"
/></noscript>
<!-- End Meta Pixel Code -->
`;

constructor(
public injectCustomScriptService: InjectCustomScriptService,
public renderer2: Renderer2
public injectCustomScriptService: InjectCustomScriptService
) {}

ngOnInit(): void {
console.log("Você entrou na pagina secundaria");
console.log('garai',this.script);
this.injectCustomScriptService.setScriptToHeader(this.renderer2, this.script)
// this.injectCustomScriptService.setScriptToHeader(this.script)
this.injectCustomScriptService.setScriptToBody(this.script)
}

ngAfterViewInit(): void {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DOCUMENT } from '@angular/common';
import { Inject, Injectable, Renderer2 } from '@angular/core';
import { Inject, Injectable } from '@angular/core';
import * as $ from 'jquery';

@Injectable({
Expand All @@ -8,33 +8,29 @@ import * as $ from 'jquery';
export class InjectCustomScriptService {

constructor(
@Inject(DOCUMENT) private _document: Document
) { }


/**
* Set JSON-LD Microdata on the Document Body.
*
* @param renderer2 The Angular Renderer
* @param data The data for the JSON-LD script
* @returns Void
*/
public setScriptToBody(renderer2: Renderer2, data: string): void {
let script = JSON.stringify(data);
const scriptElements = $.parseHTML(script)
scriptElements.forEach(scriptElement => {
console.log('randon', scriptElement);
renderer2.appendChild(this._document.head, scriptElement);
});
public setScriptToBody(data: string): void {
console.log(document.getElementsByClassName("script_personalizado").length);
if (document.getElementsByClassName("script_personalizado").length === 0) {
$(data).toArray().forEach(element => {
if (element.tagName === "SCRIPT" || element.tagName === "NOSCRIPT") {
element.classList.add("script_personalizado");
$("body").append(element);
}
});
}
}

public setScriptToHeader(renderer2: Renderer2, data: string): void {
let script = JSON.stringify(data);
const scriptElements = $.parseHTML(script)
scriptElements.forEach(scriptElement => {
console.log('randon', scriptElement);
renderer2.appendChild(this._document.head, scriptElement);
});
public setScriptToHeader(data: string): void {
if (document.getElementsByClassName("script_personalizado").length === 0) {
$(data).toArray().forEach(element => {
if (element.tagName === "SCRIPT" || element.tagName === "NOSCRIPT") {
element.classList.add("script_personalizado");
$("head").append(element);
}
});
}
}

}

0 comments on commit 8754157

Please sign in to comment.