Browse Source

reenable chrome support.

Jason Schwarzenberger 2 months ago
parent
commit
edb669ba98
2 changed files with 34 additions and 13 deletions
  1. 33 12
      inject.js
  2. 1 1
      manifest.json

+ 33 - 12
inject.js

@@ -1,22 +1,43 @@
 (function() {
-  const MAX_ITERATIONS = document.querySelector('html').innerHTML.length;
-  const loopParse = (json, iteration) => {
+  const loopParse = (json, iterations) => {
     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) {
+      let column = 0;
+      let m = e.message.match(/position (\d+) ?/);
+      if (m) {
+        column = Number(m[1]);
+      } else {
+        m = e.message.match(/column (\d+) ?/);
+        column = Number(m[1]) - 1;
+      }
+      const left = json.substring(0, column).replace(/,$/, '');
+      const right = json.substring(column + 1);
+      if (iterations <= 0) {
         return null;
       }
-      return loopParse(left + right, iteration + 1);
+      return loopParse(left + right, iterations - 1);
+    }
+  };
+  const ldParse = text => {
+    const input = text.replace(/[\r\n]/g, '');
+    const schemaInput = input.replace(/[\ ]/g, '');
+    const schema = loopParse(schemaInput, schemaInput.length + 1);
+    const json = loopParse(input, input.length + 1);
+    if (!json) {
+      return schema;
+    }
+    if (!schema) {
+      return json;
     }
+    return {
+      ...json,
+      '@context': schema['@context'],
+      '@type': schema['@type']
+    };
   };
-  const ld = [].map
-    .call(document.querySelectorAll('script[type="application/ld+json"]'), e => {
-      return loopParse(e.innerText.replace(/[\r\n\ ]/g, ''), 0);
-    })
+  const ld = Array.from(document.querySelectorAll('script[type="application/ld+json"]'))
+    .map(e => ldParse(e.textContent))
     .find(l => {
       if (!l) {
         return false;
@@ -39,7 +60,7 @@
 
   displayed.style.marginBottom = '75px';
   displayed.removeAttribute('id');
-  displayed.classList.replace('premium-content', 'content');
+  displayed.classList.replace('premium-content', 'full-content');
   Array.from(displayed.querySelectorAll(selector)).forEach(e => {
     e.classList.remove(selector.substring(1));
     e.style = '';

+ 1 - 1
manifest.json

@@ -1,7 +1,7 @@
 {
   "manifest_version": 2,
   "name": "NZHerald Bypass",
-  "version": "1.1.6",
+  "version": "1.1.7",
   "description": "Improves the NZHerald experience",
   "content_scripts": [
     {