Skip to main content

NZHerald Bypass

Bypasses the new NZHerald Premium paywall using Javascript to peek at the LD+JSON Schema to figure out which CSS selector is being used to hide content.

gist link

Attachment Type Size
NZHerald Bypass.md text/markdown 1.5KiB
inject.js text/javascript 1.6KiB
manifest.json application/json 372.0B
style.css text/css 110.0B

NZHerald Bypass.md – text/markdown, 1.5KiB

NZHerald Bypass

Bypasses the new NZHerald Premium paywall using Javascript to peek at the LD+JSON Schema to figure out which CSS selector is being used to hide content.

I made this mostly for myself.

Repository

Primary source repository is located on my gogs instance, https://git.1j.nz/firefox-extensions/nzherald-bypass.

Firefox add-on page: https://addons.mozilla.org/en-US/firefox/addon/nzherald-bypass/

License

MIT License

Copyright (c) 2019 Jason

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


inject.js – text/javascript, 1.6KiB

(function() {
  const MAX_ITERATIONS = document.querySelector('html').innerHTML.length;
  const loopParse = (json, iteration) => {
    try {
      return JSON.parse(json);
    } catch (e) {
      const [m, column] = e.message.match(/column (\d+) ?/);
      const left = json.substring(0, Number(column) - 1).replace(/,$/, '');
      const right = json.substring(Number(column));
      if (iteration >= MAX_ITERATIONS) {
        return null;
      }
      return loopParse(left + right, iteration + 1);
    }
  };
  const ld = [].map
    .call(document.querySelectorAll('script[type="application/ld+json"]'), e => {
      return loopParse(e.innerText.replace(/[\r\n\ ]/g, ''), 0);
    })
    .find(l => {
      if (!l) {
        return false;
      }
      const isSchema = l['@context'] === 'http://schema.org';
      const isArticle = l['@type'] === 'Article';
      const hasPart = l.hasOwnProperty('hasPart');
      return isSchema && isArticle && hasPart;
    });

  const content = document.querySelector('.premium-content');

  if (!ld || !content) {
    return;
  }

  const selector = ld.hasPart.cssSelector;
  const inserted = content.parentElement.insertBefore(content.cloneNode(true), content);
  const [displayed, hidden] = [content, inserted];

  displayed.style.marginBottom = '75px';
  displayed.removeAttribute('id');
  displayed.classList.replace('premium-content', 'content');
  Array.from(displayed.querySelectorAll(selector)).forEach(e => {
    e.classList.remove(selector.substring(1));
    e.style = '';
  });

  hidden.style.opacity = 0.1;
  hidden.style.height = '1px';
  hidden.style.overflow = 'hidden';
})();


manifest.json – application/json, 372.0B

{
  "manifest_version": 2,
  "name": "NZHerald Bypass",
  "version": "1.1.6",
  "description": "Improves the NZHerald experience",
  "content_scripts": [
    {
      "js": [
        "inject.js"
      ],
      "css": [
        "style.css"
      ],
      "matches": [
        "https://www.nzherald.co.nz/*"
      ]
    }
  ],
  "icons": {
    "128": "img/icon-128.png"
  }
}

style.css – text/css, 110.0B

.premium-sub,
.article-offer {
  display: none !important;
}

.ellipsis:after {
  content: None !important;
}