Skip to content

Commit

Permalink
Merge pull request #114 from Lombiq/issue/OSOE-476
Browse files Browse the repository at this point in the history
OSOE-476: Extract inline javascript block into file, and register for usage in Lombiq.HelpfulExtensions
  • Loading branch information
DemeSzabolcs authored Feb 18, 2023
2 parents 9b02b5b + 8842243 commit c19e06b
Show file tree
Hide file tree
Showing 8 changed files with 1,382 additions and 28 deletions.
20 changes: 20 additions & 0 deletions Lombiq.HelpfulExtensions/Assets/Scripts/target-blank.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(function initTargetBlank() {
function targetBlank() {
const links = document.querySelectorAll('a');
const currentHostname = window.location.hostname;

for (let i = 0; i < links.length; i++) {
if (!links[i].href.match(/^mailto:/) &&
(links[i].hostname !== currentHostname &&
(!links[i].href.match(/^javascript:/i)))) {
links[i].setAttribute('target', '_blank');
}
}
}
window.addEventListener(
'load',
() => {
window.setTimeout(targetBlank, 100);
},
false);
})();
6 changes: 6 additions & 0 deletions Lombiq.HelpfulExtensions/Constants/ResourceNames.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Lombiq.HelpfulExtensions.Constants;

public static class ResourceNames
{
public const string TargetBlank = nameof(TargetBlank);
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
using Microsoft.AspNetCore.Html;
using Lombiq.HelpfulExtensions.Constants;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using OrchardCore.ResourceManagement;
using System.Threading.Tasks;

namespace Lombiq.HelpfulExtensions.Extensions.TargetBlank.Filters;

public class TargetBlankFilter : IAsyncResultFilter
{
private readonly IResourceManager _resourceManager;

public TargetBlankFilter(
IResourceManager resourceManager) => _resourceManager = resourceManager;
public TargetBlankFilter(IResourceManager resourceManager) => _resourceManager = resourceManager;

public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultExecutionDelegate next)
{
Expand All @@ -21,28 +21,7 @@ public async Task OnResultExecutionAsync(ResultExecutingContext context, ResultE
return;
}

const string script = @"
<script>
function targetBlank() {
const x=document.querySelectorAll('a');
for(let i=0;i<x.length;i++) {
if(!x[i].href.match(/^mailto:/)&&(x[i].hostname!==location.hostname&&(!x[i].href.match('javascript:'))))
{
x[i].setAttribute('target','_blank');
}
}
}
window.addEventListener(
'load',
function() {
window.setTimeout(targetBlank,100)
},
false);
</script>";

// Until Node Extensions is ready for use, this solution is here to replace the usage of Gulp. When Node
// Extensions is ready, this script should be replaced with a JavaScript file.
_resourceManager.RegisterFootScript(new HtmlString(script));
_resourceManager.RegisterResource("script", ResourceNames.TargetBlank).AtFoot();

await next();
}
Expand Down
9 changes: 7 additions & 2 deletions Lombiq.HelpfulExtensions/Extensions/TargetBlank/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
using Lombiq.HelpfulExtensions.Extensions.TargetBlank.Filters;
using Lombiq.HelpfulExtensions.Extensions.TargetBlank.Filters;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OrchardCore.Modules;
using OrchardCore.ResourceManagement;
using System;

namespace Lombiq.HelpfulExtensions.Extensions.TargetBlank;

[Feature(FeatureIds.TargetBlank)]
public class Startup : StartupBase
{
public override void ConfigureServices(IServiceCollection services) =>
public override void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IConfigureOptions<ResourceManagementOptions>, ResourceManagementOptionsConfiguration>();
services.Configure<MvcOptions>(options => options.Filters.Add(typeof(TargetBlankFilter)));
}

public override void Configure(IApplicationBuilder app, IEndpointRouteBuilder routes, IServiceProvider serviceProvider)
{
Expand Down
8 changes: 7 additions & 1 deletion Lombiq.HelpfulExtensions/Lombiq.HelpfulExtensions.csproj
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">


<Import Condition="'$(NuGetBuild)' != 'true'" Project="..\..\..\Utilities\Lombiq.NodeJs.Extensions\Lombiq.NodeJs.Extensions\Lombiq.NodeJs.Extensions.props" />

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AddRazorSupportForMvc>true</AddRazorSupportForMvc>
Expand Down Expand Up @@ -45,10 +47,14 @@

<ItemGroup Condition="'$(NuGetBuild)' != 'true'">
<ProjectReference Include="..\..\..\Libraries\Lombiq.HelpfulLibraries\Lombiq.HelpfulLibraries.OrchardCore\Lombiq.HelpfulLibraries.OrchardCore.csproj" />
<ProjectReference Include="..\..\..\Utilities\Lombiq.NodeJs.Extensions\Lombiq.NodeJs.Extensions\Lombiq.NodeJs.Extensions.csproj" />
</ItemGroup>

<ItemGroup Condition="'$(NuGetBuild)' == 'true'">
<PackageReference Include="Lombiq.HelpfulLibraries.OrchardCore" Version="5.1.1" />
<PackageReference Include="Lombiq.NodeJs.Extensions" Version="1.1.0" />
</ItemGroup>

<Import Condition="'$(NuGetBuild)' != 'true'" Project="..\..\..\Utilities\Lombiq.NodeJs.Extensions\Lombiq.NodeJs.Extensions\Lombiq.NodeJs.Extensions.targets" />

</Project>
19 changes: 19 additions & 0 deletions Lombiq.HelpfulExtensions/ResourceManagementOptionsConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Microsoft.Extensions.Options;
using OrchardCore.ResourceManagement;
using static Lombiq.HelpfulExtensions.Constants.ResourceNames;

namespace Lombiq.HelpfulExtensions;

public class ResourceManagementOptionsConfiguration : IConfigureOptions<ResourceManagementOptions>
{
private const string WwwRoot = "~/Lombiq.HelpfulExtensions/";
private static readonly ResourceManifest _manifest = new();

static ResourceManagementOptionsConfiguration() =>
_manifest
.DefineScript(TargetBlank)
.SetUrl(WwwRoot + "scripts/target-blank.min.js", WwwRoot + "scripts/target-blank.js")
.SetVersion("1.0.0");

public void Configure(ResourceManagementOptions options) => options.ResourceManifests.Add(_manifest);
}
23 changes: 23 additions & 0 deletions Lombiq.HelpfulExtensions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"private": true,
"scripts": {
"build:assets": "npm explore nodejs-extensions -- pnpm build:assets",
"build:scripts": "npm explore nodejs-extensions -- pnpm build:scripts",
"clean:scripts": "npm explore nodejs-extensions -- pnpm clean:scripts",
"watch:scripts": "npm explore nodejs-extensions -- pnpm watch:scripts"
},
"nodejsExtensions": {
"scripts": {
"source": "Assets/Scripts",
"target": "wwwroot/scripts"
}
},
"devDependencies": {
"eslint": "^8.25.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-only-warn": "^1.0.3",
"eslint-plugin-promise": "^6.1.0"
}
}
Loading

0 comments on commit c19e06b

Please sign in to comment.