Browse Source

Use string literals for injected code

simov 8 years ago
parent
commit
58b5cf3aa5
1 changed files with 18 additions and 13 deletions
  1. 18 13
      background/background.js

+ 18 - 13
background/background.js

@@ -89,13 +89,15 @@ chrome.storage.sync.get((res) => {
 
 function inject (id) {
   chrome.tabs.executeScript(id, {
-    code: [
-      'document.querySelector("pre").style.visibility = "hidden"',
-      'var theme = "' + state.theme + '"',
-      'var raw = ' + state.raw,
-      'var content = ' + JSON.stringify(state.content),
-      'var compiler = "' + state.compiler + '"'
-    ].join(';'), runAt: 'document_start'})
+    code: `
+      document.querySelector('pre').style.visibility = 'hidden'
+      var theme = '${state.theme}'
+      var raw = ${state.raw}
+      var content = ${JSON.stringify(state.content)}
+      var compiler = '${state.compiler}'
+    `,
+    runAt: 'document_start'
+  })
 
   chrome.tabs.insertCSS(id, {file: 'css/content.css', runAt: 'document_start'})
   chrome.tabs.insertCSS(id, {file: 'vendor/prism.css', runAt: 'document_start'})
@@ -111,15 +113,17 @@ function inject (id) {
 chrome.tabs.onUpdated.addListener((id, info, tab) => {
   if (info.status === 'loading') {
     chrome.tabs.executeScript(id, {
-      code: 'JSON.stringify({' +
-        'location: window.location,' +
-        'contentType: document.contentType,' +
-        'loaded: !!window.state' +
-      '})',
+      code: `
+        JSON.stringify({
+          location: window.location,
+          contentType: document.contentType,
+          loaded: !!window.state
+        })
+      `,
       runAt: 'document_start'
     }, (res) => {
       if (chrome.runtime.lastError) {
-        // Origin not allowed
+        // origin not allowed
         return
       }
 
@@ -132,6 +136,7 @@ chrome.tabs.onUpdated.addListener((id, info, tab) => {
       }
 
       if (win.loaded) {
+        // anchor
         return
       }