Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OSOE-476: Extract inline javascript block into file, and register for usage in Lombiq.HelpfulExtensions #114

Merged
merged 13 commits into from
Feb 18, 2023
17 changes: 17 additions & 0 deletions Lombiq.HelpfulExtensions/Assets/Scripts/target-blank.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function targetBlank() {
DemeSzabolcs marked this conversation as resolved.
Show resolved Hide resolved
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
11 changes: 10 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 @@ -49,6 +51,13 @@

<ItemGroup Condition="'$(NuGetBuild)' == 'true'">
<PackageReference Include="Lombiq.HelpfulLibraries.OrchardCore" Version="5.1.1" />
DemeSzabolcs marked this conversation as resolved.
Show resolved Hide resolved
<PackageReference Include="Lombiq.NodeJs.Extensions" Version="1.1.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Condition="'$(NuGetBuild)' == 'true'" Include="..\..\..\Utilities\Lombiq.NodeJs.Extensions\Lombiq.NodeJs.Extensions\Lombiq.NodeJs.Extensions.csproj" />
</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"
}
},
Comment on lines +9 to +14
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole configuration is superfluous, if you're willing to accept the default path wwwroot/js for JS files.

"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"
}
Comment on lines +15 to +22
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When used as part of OSOCE, this whole block should not be here.

}