What is the most reliable way to hide / spoof the referrer in JavaScript?

前端 未结 10 1621
挽巷
挽巷 2020-11-28 03:13

Normally, the referrer is traceable through:

  • JavaScript\'s document.referrer
  • The request headers, e.g. PHP\'s $_SERVER[\'HTTP_REFER
10条回答
  •  -上瘾入骨i
    2020-11-28 03:35

    What you're asking for cannot be done in Firefox.

    The current context menu implementation always passes the current document as a referrer:

    // Open linked-to URL in a new window.
    openLink: function () {
        var doc = this.target.ownerDocument;
        urlSecurityCheck(this.linkURL, doc.nodePrincipal);
        openLinkIn(this.linkURL, "window", {
            charset: doc.characterSet,
            referrerURI: doc.documentURIObject // <----------------
        });
    },
    
    // Open linked-to URL in a new tab.
    openLinkInTab: function () {
        var doc = this.target.ownerDocument;
        urlSecurityCheck(this.linkURL, doc.nodePrincipal);
        openLinkIn(this.linkURL, "tab", {
            charset: doc.characterSet,
            referrerURI: doc.documentURIObject // <----------------
        });
    },
    
    // open URL in current tab
    openLinkInCurrent: function () {
        var doc = this.target.ownerDocument;
        urlSecurityCheck(this.linkURL, doc.nodePrincipal);
        openLinkIn(this.linkURL, "current", {
            charset: doc.characterSet,
            referrerURI: doc.documentURIObject // <----------------
        });
    }, 
    

    Obviously, userscripts are not allowed to change the context menu implementation, so the only way out is a browser extension.

    (Or, which would be a pretty poor hack, disable the context menu by calling preventDefault() on the contextmenu event, and use your own custom context menu)

提交回复
热议问题