Browse Source

Auto check injectability and select the right way to inject script

Gerald 9 years ago
parent
commit
ef56db996b
4 changed files with 28 additions and 20 deletions
  1. 1 1
      src/common.js
  2. 12 6
      src/injected.js
  3. 2 0
      src/options/templates/tab-settings.html
  4. 13 13
      src/options/views/tab-settings.js

+ 1 - 1
src/common.js

@@ -10,7 +10,7 @@ _.options = function () {
     exportValues: true,
     closeAfterInstall: false,
     trackLocalFile: false,
-    injectMode: 0,
+    // injectMode: 0,
     autoReload: false,
     dropbox: {},
     dropboxEnabled: false,

+ 12 - 6
src/injected.js

@@ -180,6 +180,12 @@ var comm = {
     comm.did = comm.vmid + destId;
     document.addEventListener(comm.sid, comm['handle' + srcId].bind(comm), false);
     comm.load = comm.checkLoad = function(){};
+    // check whether the page is injectable via <script>, whether limited by CSP
+    try {
+      comm.injectable = (0, eval)('true');
+    } catch (e) {
+      comm.injectable = false;
+    }
   },
   post: function(data) {
     var e = new CustomEvent(this.did, {detail: data});
@@ -528,12 +534,7 @@ var comm = {
       code.push('}.call(this);');
       code = code.join('\n');
       var name = script.custom.name || script.meta.name || script.id;
-      if (data.injectMode == 1) {
-        // advanced injection
-        var id = comm.getUniqId();
-        comm.ainject[id] = [name, wrapper];
-        comm.post({cmd: 'Inject', data: [id, code]});
-      } else {
+      if (comm.injectable) {
         // normal injection
         try {
           var func = new Function('g', code);
@@ -542,6 +543,11 @@ var comm = {
           return;
         }
         comm.runCode(name, func, wrapper);
+      } else {
+        // advanced injection
+        var id = comm.getUniqId();
+        comm.ainject[id] = [name, wrapper];
+        comm.post({cmd: 'Inject', data: [id, code]});
       }
     }
     function run(list) {

+ 2 - 0
src/options/templates/tab-settings.html

@@ -12,6 +12,7 @@
     <input type=checkbox data-check=autoReload <%= it.autoReload ? 'checked' : '' %>>
     <span data-i18n=labelAutoReloadCurrentTab></span>
   </label>
+  <!--
   <label class="line">
     <span data-i18n=labelInjectMode></span>
     <select id=sInjectMode>
@@ -20,6 +21,7 @@
     </select>
     <span></span>
   </label>
+  -->
   <fieldset class=title>
     <legend data-i18n=labelDataImport></legend>
     <button id=bImport data-i18n=buttonImportData></button>

+ 13 - 13
src/options/views/tab-settings.js

@@ -34,7 +34,7 @@ var SettingsTab = BaseView.extend({
   name: 'settings',
   events: {
     'change [data-check]': 'updateCheckbox',
-    'change #sInjectMode': 'updateInjectMode',
+    // 'change #sInjectMode': 'updateInjectMode',
     'change #cUpdate': 'updateAutoUpdate',
     'click #bSelect': 'toggleSelection',
     'click #bImport': 'importFile',
@@ -56,24 +56,24 @@ var SettingsTab = BaseView.extend({
       var serviceView = new SyncServiceView({model: service});
       syncServices.append(serviceView.$el);
     });
-    this.$('#sInjectMode').val(options.injectMode);
-    this.updateInjectHint();
+    // this.$('#sInjectMode').val(options.injectMode);
+    // this.updateInjectHint();
     this.exportList = new ExportList;
   },
   updateCheckbox: _.updateCheckbox,
   updateAutoUpdate: function (e) {
     _.sendMessage({cmd: 'AutoUpdate'});
   },
-  updateInjectHint: function () {
-    this.$('#sInjectMode+span').text([
-      _.i18n('hintInjectModeNormal'),
-      _.i18n('hintInjectModeAdvanced'),
-    ][this.$('#sInjectMode').val()]);
-  },
-  updateInjectMode: function (e) {
-    _.options.set('injectMode', e.target.value);
-    this.updateInjectHint();
-  },
+  // updateInjectHint: function () {
+  //   this.$('#sInjectMode+span').text([
+  //     _.i18n('hintInjectModeNormal'),
+  //     _.i18n('hintInjectModeAdvanced'),
+  //   ][this.$('#sInjectMode').val()]);
+  // },
+  // updateInjectMode: function (e) {
+  //   _.options.set('injectMode', e.target.value);
+  //   this.updateInjectHint();
+  // },
   toggleSelection: function () {
     this.exportList.toggleAll();
   },