Skip to content

Commit

Permalink
Changes to be committed:
Browse files Browse the repository at this point in the history
	new file:   joomla.asset.json
	new file:   js/functions.js
	modified:   pankybrowsertitle.php
	modified:   pankybrowsertitle.xml
  • Loading branch information
pnkr committed Apr 4, 2023
1 parent 57a734b commit 3303361
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 51 deletions.
21 changes: 21 additions & 0 deletions joomla.asset.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "https://developer.joomla.org/schemas/json-schema/web_assets.json",
"name": "pankybrowsertitle",
"version": "1.0.0",
"description": "Browser Tab Title Change plugin",
"license": "GPL-3.0-or-later",
"assets": [
{
"name": "pankybrowsertitle",
"description": "Browser title change JavaScript",
"type": "script",
"uri": "plg_system_pankybrowsertitle/functions.js",
"dependencies": [
"core"
],
"attributes": {
"defer": true
}
}
]
}
51 changes: 51 additions & 0 deletions js/functions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
window.onload = function() {

var pageTitle = document.title;
var attentionMessage = document.title;
var meta = document.querySelector('meta[name="browserTabMsg"]');

if (meta) {
var attentionMessage = meta.getAttribute('content');
}

var blinkEvent = null;
var currentDomain = window.location.hostname;

document.addEventListener('visibilitychange', function(e) {
var isPageActive = !document.hidden;

if (!isPageActive) {
blink();
} else {
document.title = pageTitle;
clearInterval(blinkEvent);
}
});

function blink() {
blinkEvent = setInterval(function() {
if (document.title === attentionMessage) {
document.title = pageTitle;
} else {
document.title = attentionMessage;
}
}, 100);
}

window.addEventListener('blur', function() {
// Save the current domain name when the user switches to a new tab
currentDomain = window.location.hostname;
});

window.addEventListener('focus', function() {
// Check if the user switched to a new site when the tab regained focus
if (window.location.hostname !== currentDomain) {
document.title = attentionMessage;
blink();
} else {
document.title = pageTitle;
clearInterval(blinkEvent);
}
});
};

53 changes: 5 additions & 48 deletions pankybrowsertitle.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,62 +30,19 @@ public function onAfterDispatch()

$document = Joomla\CMS\Factory::getApplication()->getDocument();

$document->setMetaData('browserTabMsg', $this->params->get('browsermessage'));


if (!($document instanceof Joomla\CMS\Document\HtmlDocument))
{
return;
}

/** @var Joomla\CMS\WebAsset\WebAssetManager $wa */
$wa = Factory::getApplication()->getDocument()->getWebAssetManager();
// Load inline asset
$script = "
window.onload = function() {
$wa->getRegistry()->addExtensionRegistryFile('plg_system_pankybrowsertitle');

var pageTitle = document.title;
var attentionMessage = '" . $this->params->get('browsermessage') . "';
var blinkEvent = null;
var currentDomain = window.location.hostname;
document.addEventListener('visibilitychange', function(e) {
var isPageActive = !document.hidden;
if (!isPageActive) {
blink();
} else {
document.title = pageTitle;
clearInterval(blinkEvent);
}
});
function blink() {
blinkEvent = setInterval(function() {
if (document.title === attentionMessage) {
document.title = pageTitle;
} else {
document.title = attentionMessage;
}
}, 100);
}
window.addEventListener('blur', function() {
// Save the current domain name when the user switches to a new tab
currentDomain = window.location.hostname;
});
window.addEventListener('focus', function() {
// Check if the user switched to a new site when the tab regained focus
if (window.location.hostname !== currentDomain) {
document.title = attentionMessage;
blink();
} else {
document.title = pageTitle;
clearInterval(blinkEvent);
}
});
};
";
$wa->useScript('pankybrowsertitle','script');

$wa->addInlineScript($script, ['name' => 'pankybrowsertitle.asset']);
}
}
9 changes: 6 additions & 3 deletions pankybrowsertitle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@
<author>Panayiotis Kiriakopoulos</author>
<creationDate>April 2023</creationDate>
<copyright>(C) 2023 Panayiotis Kiriakopoulos</copyright>
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
<license>GNU General Public License version 3 or later; see LICENSE.txt</license>
<authorEmail>kiriakopoulos_p@hotmail.com</authorEmail>
<authorUrl>https://github.com/pnkr</authorUrl>
<version>1.0.0</version>
<version>1.0.1</version>
<description>PLG_SYSTEM_PANKYBROWSERTITLE_XML_DESCRIPTION</description>
<files>
<filename plugin="pankybrowsertitle">pankybrowsertitle.php</filename>
</files>
<media folder="media/plg_system_pankybrowsertitle" destination="plg_system_pankybrowsertitle">
<folder>js</folder>
<filename>joomla.asset.json</filename>
</media>
<languages>
<language tag="en-GB">language/en-GB/plg_system_pankybrowsertitle.ini</language>
<language tag="en-GB">language/en-GB/plg_system_pankybrowsertitle.sys.ini</language>
Expand All @@ -22,7 +26,6 @@
<config>
<fields name="params">
<fieldset name="options" label="PLG_SYSTEM_PANKYBROWSERTITLE_MESSAGETEXT">

<field name="browsermessage" type="text" label="PLG_SYSTEM_PANKYBROWSERTITLE_MESSAGETEXT" description="PLG_SYSTEM_PANKYBROWSERTITLE_MESSAGEDESC" />
</fieldset>
</fields>
Expand Down

0 comments on commit 3303361

Please sign in to comment.