mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2025-08-29 15:06:26 +02:00
Renamed server roles by osi they work on
This commit is contained in:
24
roles/srv-web-7-7-inj-iframe/README.md
Normal file
24
roles/srv-web-7-7-inj-iframe/README.md
Normal file
@@ -0,0 +1,24 @@
|
||||
|
||||
# 🌐 iFrame Notifier for Nginx
|
||||
|
||||
This Ansible role injects a small JavaScript snippet into your HTML responses that enables parent pages to get notified whenever the iframe’s location changes and forces external links to open in a new tab.
|
||||
|
||||
---
|
||||
|
||||
## Features
|
||||
|
||||
- **Location Change Notification**
|
||||
Uses `postMessage` to inform the parent window of any URL changes inside the iframe (including pushState/popState events) for seamless SPA support.
|
||||
|
||||
- **External Link Handling**
|
||||
Automatically sets `target="_blank"` and `rel="noopener"` on links pointing outside your primary domain to improve security and user experience.
|
||||
|
||||
- **Easy CSP Integration**
|
||||
Calculates a CSP hash for the injected script so you can safely allow it via your Content Security Policy.
|
||||
|
||||
---
|
||||
|
||||
## Author
|
||||
|
||||
Developed by **Kevin Veen-Birkenbach**
|
||||
[https://www.veen.world](https://www.veen.world) 🎉
|
28
roles/srv-web-7-7-inj-iframe/meta/main.yml
Normal file
28
roles/srv-web-7-7-inj-iframe/meta/main.yml
Normal file
@@ -0,0 +1,28 @@
|
||||
|
||||
---
|
||||
galaxy_info:
|
||||
author: "Kevin Veen-Birkenbach"
|
||||
description: "Injects a JS snippet into HTML to notify parent windows of iframe location changes and force external links to new tabs."
|
||||
company: |
|
||||
Kevin Veen-Birkenbach
|
||||
Consulting & Coaching Solutions
|
||||
https://www.veen.world
|
||||
license: "CyMaIS NonCommercial License (CNCL)"
|
||||
repository: https://s.veen.world/cymais
|
||||
issue_tracker_url: https://s.veen.world/cymaisissues
|
||||
documentation: https://s.veen.world/cymais
|
||||
license_url: "https://s.veen.world/cncl"
|
||||
min_ansible_version: "2.9"
|
||||
platforms:
|
||||
- name: Archlinux
|
||||
versions:
|
||||
- rolling
|
||||
galaxy_tags:
|
||||
- nginx
|
||||
- iframe
|
||||
- javascript
|
||||
- csp
|
||||
- security
|
||||
- postMessage
|
||||
dependencies:
|
||||
- srv-web-7-4-core
|
12
roles/srv-web-7-7-inj-iframe/tasks/main.yml
Normal file
12
roles/srv-web-7-7-inj-iframe/tasks/main.yml
Normal file
@@ -0,0 +1,12 @@
|
||||
- name: "Load iFrame handler JS template for '{{ application_id }}'"
|
||||
set_fact:
|
||||
iframe_code: "{{ lookup('template','iframe-handler.js.j2') }}"
|
||||
|
||||
- name: "Collapse iFrame code into one-liner for '{{ application_id }}'"
|
||||
set_fact:
|
||||
iframe_code_one_liner: "{{ iframe_code | to_one_liner }}"
|
||||
|
||||
- name: "Append iFrame CSP hash for '{{ application_id }}'"
|
||||
set_fact:
|
||||
applications: "{{ applications | append_csp_hash(application_id, iframe_code_one_liner) }}"
|
||||
changed_when: false
|
1
roles/srv-web-7-7-inj-iframe/templates/head_sub.j2
Normal file
1
roles/srv-web-7-7-inj-iframe/templates/head_sub.j2
Normal file
@@ -0,0 +1 @@
|
||||
<script>{{ iframe_code_one_liner | replace("'", "\\'") }}</script>
|
46
roles/srv-web-7-7-inj-iframe/templates/iframe-handler.js.j2
Normal file
46
roles/srv-web-7-7-inj-iframe/templates/iframe-handler.js.j2
Normal file
@@ -0,0 +1,46 @@
|
||||
(function() {
|
||||
var primary = "{{ primary_domain }}";
|
||||
var allowedOrigin = "https://{{ domains | get_domain('portfolio') }}";
|
||||
|
||||
function notifyParent() {
|
||||
try {
|
||||
window.parent.postMessage({
|
||||
type: "iframeLocationChange",
|
||||
href: window.location.href
|
||||
}, allowedOrigin);
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
function forceExternalLinks() {
|
||||
Array.prototype.forEach.call(document.querySelectorAll("a[href]"), function(a) {
|
||||
try {
|
||||
var url = new URL(a.href, location);
|
||||
if (!url.hostname.endsWith(primary)) {
|
||||
a.target = "_blank";
|
||||
a.rel = "noopener";
|
||||
}
|
||||
} catch (e) {}
|
||||
});
|
||||
}
|
||||
|
||||
window.addEventListener("load", function() {
|
||||
notifyParent();
|
||||
forceExternalLinks();
|
||||
});
|
||||
window.addEventListener("popstate", function() {
|
||||
notifyParent();
|
||||
forceExternalLinks();
|
||||
});
|
||||
|
||||
// SPA support
|
||||
var _pushState = history.pushState;
|
||||
history.pushState = function() {
|
||||
_pushState.apply(history, arguments);
|
||||
notifyParent();
|
||||
forceExternalLinks();
|
||||
};
|
||||
})();
|
||||
|
||||
{% if enable_debug | bool %}
|
||||
console.log("[iframe-sync] Sender for iframe messages is active.");
|
||||
{% endif %}
|
Reference in New Issue
Block a user