config method
Add web seo mata config method which remove any javascript
code with the same id metaSEOScripts
and replace if exists with
needed one before the end of the body of the html web file automatically.
This method should be run before any meta seo method to run the package correctly
Implement the interface
Implementation
@override
config() {
/// Define the ScriptElement
ScriptElement script = ScriptElement();
/// Define the id of the ScriptElement
script.id = 'metaSEOScripts';
/// Define the javascript code of the ScriptElement
script.innerHtml = """
function seoNameJS(name, content) {
if(document.querySelector("[name='"+name+"']") !== null) {
document.querySelector("[name='"+name+"']").remove();
}
var meta = document.createElement('meta');
meta.setAttribute('name', name);
meta.setAttribute('content', content);
document.getElementsByTagName('head')[0].appendChild(meta);
}
function seoPropertyJS(property, content) {
if(document.querySelector("[property='"+property+"']") !== null) {
document.querySelector("[property='"+property+"']").remove();
}
var meta = document.createElement('meta');
meta.setAttribute('property', property);
meta.setAttribute('content', content);
document.getElementsByTagName('head')[0].appendChild(meta);
}
function seoAttributeJS(key, val) {
if(document.querySelector("[name='"+key+"']") !== null) {
document.querySelector("[name='"+key+"']").remove();
}
var meta = document.createElement('meta');
meta.setAttribute(key, val);
document.getElementsByTagName('head')[0].appendChild(meta);
}
function seoOpenGraphJS(property, content) {
if(document.querySelector("[property='"+property+"']") !== null) {
document.querySelector("[property='"+property+"']").remove();
}
var meta = document.createElement('meta');
meta.setAttribute('property', property);
meta.setAttribute('content', content);
meta.setAttribute('data-rh', 'true');
document.getElementsByTagName('head')[0].appendChild(meta);
}
function seoRobotsJS(name, content) {
var meta = document.createElement('meta');
meta.setAttribute('name', name);
meta.setAttribute('content', content);
document.getElementsByTagName('head')[0].appendChild(meta);
}
""";
/// Make loop in html file body to check of any node with the same id
for (int i = 0; i < document.body!.children.length; i++) {
/// Check if the id of the package is exists in the html document
if (document.body!.children[i].id == 'metaSEOScripts') {
/// Remove any node with the same id of the javascript functions
document.body!.children[i].remove();
/// Then break the loop after deleting
break;
}
}
/// Add new or replace the javascript needed functions to the end
/// of the body of the html document
document.body!.insertAdjacentElement('beforeEnd', script);
}