【问题标题】:Joomla Plugin inject Javascript snippet into all pagesJoomla 插件将 Javascript 片段注入所有页面
【发布时间】:2018-12-08 17:47:23
【问题描述】:

我正在尝试编写一个 Joomla 插件,该插件将一个小的 javascript 代码注入所有页面以分发给其他人。但我读过的所有内容都说我应该使用$doc = JFactory::getDocument(); 来完成它,但我无法让它工作。

这是我正在尝试使用的代码,Joomla 说一切正常,但是一旦激活,如果我转到我网站的主页,我的 javascript sn-p 就不会被添加到页面的源代码中。

当前代码:

bwai.xml

<?xml version="1.0" encoding="utf-8"?>
<extension version="3.0" type="plugin" group="system" method="upgrade">
    <name>Beacon Web Analytics Intergration for Joomla</name>
    <creationDate>June 2018</creationDate>
    <author>Beaconsoft Limited</author>
    <authorEmail>contact@thisisbeaon.com</authorEmail>
    <authorUrl>https://www.thisisbeacon.com</authorUrl>
    <copyright>Beaconsoft Limited 2018</copyright>
    <license>BSD new</license>
    <version>1.0.0</version>
    <description>This plugin enables the Beacon Web Analytics Intelligence to run on your site, this information will not be saved unless you have an account for Beacon.</description>

    <files folder="site">
        <filename plugin="bwai">index.php</filename>
        <filename>index.html</filename>
    </files>
    <administration>
    </administration>
</extension>

index.php

<?php
defined('_JEXEC') or die('No direct access');
die("hello");
jimport('joomla.plugin.plugin');

class plgSystemCustomhead extends JPlugin{
    function onBeforeCompileHead() {
        die("hello hello");
        /*if (JFactory::getApplication()->isAdmin()){
            return true;
        }*/
        $doc = JFactory::getDocument();
        $doc->addScriptDeclaration('
            (function(a, d, w){
                var h= d.getElementsByTagName(a[0])[0]; var s= d.createElement(a[1]);
                s.setAttribute("type", a[2]); s.setAttribute("src", a[3]); s.setAttribute(a[4], true); s.setAttribute(a[5], a[6]); h.appendChild(s);
            })(["head", "script", "text/javascript", "//tracker.thisisbeacon.com/tracker/", "async", "rel", "preload"], document, window);

        ', 'text/javascript'); 
    }
}
?>

我想做的只是有一个人们可以安装的小插件,它将我们的跟踪 javascript sn-p 代码添加到 Joomla 网站前端的所有页面。

将exit('hello hello'); 添加到函数顶部我也尝试将die("hello"); 添加到文件顶部,在它不执行的已定义或死亡语句下,但Joomla 将扩展报告为活动,如您所见更多

【问题讨论】:

  • 您确定插件已启用、正在执行并且正在调用钩子吗?尝试在 onBeforeCompileHead 函数中添加exit('executing...') 以确保...
  • 好吧,Joomla 报告它已启用,但它似乎不起作用,因为退出调用似乎不起作用。 (更新问题以显示这一点)

标签: php plugins joomla joomla-extensions


【解决方案1】:

将你的 index.php 重命名为 bwai.php,并在 xml 中写入

<filename plugin="bwai">bwai.php</filename>

那么你的类名应该与插件名匹配,并且是 PlgSystemBwai

【讨论】:

  • 将在我拿到带有代码的机器后立即进行测试:D
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多