最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to insert a note node in the DOM at the top of any web page? - Stack Overflow

programmeradmin0浏览0评论

I try to add a text box if the REQUEST_URI have an entry note like this:

By example: /?note=TEST don't produce any new node from my code.

// ==UserScript==
// @name         Simple note from QUERY_STRING
// @namespace    /
// @version      1.0
// @description  Affiche la valeur de 'note' de la query string de l'URL.
// @author       Mévatlavé
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function addNoteHeader(noteValue) {
        const header = document.createElement('div');
        header.className = 'query-string-notifier';
        header.textContent = noteValue;
        header.style.backgroundColor = 'yellow';
        header.style.color = 'black';
        header.style.fontSize = '24px';
        header.style.fontWeight = 'bold';
        header.style.textAlign = 'center';
        header.style.padding = '10px';
        header.style.position = 'fixed';
        header.style.top = '0';
        header.style.left = '0';
        header.style.right = '0';
        header.style.zIndex = '1000';

        document.body.insertBefore(header, document.body.firstChild);
    }

    const urlParams = new URLSearchParams(window.location.search);
    const noteValue = urlParams.get('note');

    if (noteValue) {
        addNoteHeader(noteValue);
    }
})();

It works on some sites, but not others. What's the cause?
Is my issue is CSP or DOM loading related?
How to fix or workaround to work on any website?

I try to add a text box if the REQUEST_URI have an entry note like this:

By example: https://stackoverflow/?note=TEST don't produce any new node from my code.

// ==UserScript==
// @name         Simple note from QUERY_STRING
// @namespace    http://tampermonkey/
// @version      1.0
// @description  Affiche la valeur de 'note' de la query string de l'URL.
// @author       Mévatlavé
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    function addNoteHeader(noteValue) {
        const header = document.createElement('div');
        header.className = 'query-string-notifier';
        header.textContent = noteValue;
        header.style.backgroundColor = 'yellow';
        header.style.color = 'black';
        header.style.fontSize = '24px';
        header.style.fontWeight = 'bold';
        header.style.textAlign = 'center';
        header.style.padding = '10px';
        header.style.position = 'fixed';
        header.style.top = '0';
        header.style.left = '0';
        header.style.right = '0';
        header.style.zIndex = '1000';

        document.body.insertBefore(header, document.body.firstChild);
    }

    const urlParams = new URLSearchParams(window.location.search);
    const noteValue = urlParams.get('note');

    if (noteValue) {
        addNoteHeader(noteValue);
    }
})();

It works on some sites, but not others. What's the cause?
Is my issue is CSP or DOM loading related?
How to fix or workaround to work on any website?

Share Improve this question edited Mar 4 at 2:50 Mévatlavé Kraspek asked Mar 4 at 2:33 Mévatlavé KraspekMévatlavé Kraspek 6914 silver badges21 bronze badges 14
  • Why <div>? There is an element for this purpose, <header>. Why you don't use CSS? Create CSS property for the selector header, or something like that. – Sergey A Kryukov Commented Mar 4 at 2:44
  • What is your URL? Are you sure you setup right value for note? – Sergey A Kryukov Commented Mar 4 at 2:47
  • 1 @SergeyAKryukov That type of content isn’t a header. Maybe an aside. – Quentin Commented Mar 4 at 2:48
  • 1 @SergeyAKryukov Div is better than something with the wrong semantics. – Quentin Commented Mar 4 at 2:50
  • 1 Maybe try document.body.insertAdjacentElement('afterbegin', header). Some website uses JavaScript to generate dom element, that when your script runs, the <body> element could be empty. – Hao Wu Commented Mar 4 at 3:00
 |  Show 9 more comments

1 Answer 1

Reset to default 0

Instead of trying to add an element on the top of the page, my experience is that using a side box is more reliable and works on most web sites including SO:

        const sidebar = document.createElement('div');
        sidebar.className = 'note-sidebar';
        sidebar.style.position = 'fixed';
        sidebar.style.top = '30%'; // Positionne la note dans le premier tiers de la fenêtre
        sidebar.style.right = '0';
        sidebar.style.zIndex = '9999';
        sidebar.style.backgroundColor = 'yellow';
        sidebar.style.padding = '5px'; // Réduit le padding
        sidebar.style.fontSize = '16px'; // Réduit la taille de la police
        sidebar.style.color = 'black';
        sidebar.style.textAlign = 'center';
        sidebar.style.width = '150px'; // Largeur de la boîte dépliée
        sidebar.style.minHeight = '30px'; // Hauteur minimale pour le replié
        sidebar.style.transition = 'width 0.3s'; // Transition pour l'animation de largeur
发布评论

评论列表(0)

  1. 暂无评论