Browse Source

Implement rule list exporting. Fix #99.

FelisCatus 11 years ago
parent
commit
8e56c4f883

+ 6 - 0
omega-i18n/en/messages.json

@@ -268,6 +268,12 @@
   "options_deleteProfile": {
     "message": "Delete"
   },
+  "options_profileExportRuleList": {
+    "message": "Publish rule list"
+  },
+  "options_profileExportRuleListHelp": {
+    "message": "Export Switch Rules as text format for publishing."
+  },
   "options_profileExportPac": {
     "message": "Export PAC"
   },

+ 6 - 0
omega-i18n/zh_CN/messages.json

@@ -268,6 +268,12 @@
   "options_deleteProfile": {
     "message": "删除"
   },
+  "options_profileExportRuleList": {
+    "message": "发布规则列表"
+  },
+  "options_profileExportRuleListHelp": {
+    "message": "将切换规则导出为文本格式以便发布。"
+  },
   "options_profileExportPac": {
     "message": "导出PAC"
   },

+ 6 - 0
omega-i18n/zh_HK/messages.json

@@ -268,6 +268,12 @@
   "options_deleteProfile": {
     "message": "刪除"
   },
+  "options_profileExportRuleList": {
+    "message": "釋出規則列表"
+  },
+  "options_profileExportRuleListHelp": {
+    "message": "將切換規則匯出為文字格式以便釋出。"
+  },
   "options_profileExportPac": {
     "message": "導出PAC"
   },

+ 6 - 0
omega-i18n/zh_TW/messages.json

@@ -268,6 +268,12 @@
   "options_deleteProfile": {
     "message": "刪除"
   },
+  "options_profileExportRuleList": {
+    "message": "釋出規則列表"
+  },
+  "options_profileExportRuleListHelp": {
+    "message": "將切換規則匯出為文字格式以便釋出。"
+  },
   "options_profileExportPac": {
     "message": "匯出PAC"
   },

+ 19 - 2
omega-pac/src/rule_list.coffee

@@ -1,4 +1,5 @@
 Buffer = require('buffer').Buffer
+Conditions = require('./conditions')
 
 strStartsWith = (str, prefix) ->
   str.substr(0, prefix.length) == prefix
@@ -52,6 +53,23 @@ module.exports = exports =
       # Exclusive rules have higher priority, so they come first.
       return exclusive_rules.concat normal_rules
   'Switchy':
+    conditionFromLegacyWildcard: (pattern) ->
+      if pattern[0] == '@'
+        pattern = pattern.substring(1)
+      else
+        if pattern.indexOf('://') <= 0 and pattern[0] != '*'
+          pattern = '*' + pattern
+        if pattern[pattern.length - 1] != '*'
+          pattern += '*'
+
+      host = Conditions.urlWildcard2HostWildcard(pattern)
+      if host
+        conditionType: 'HostWildcardCondition'
+        pattern: host
+      else
+        conditionType: 'UrlWildcardCondition'
+        pattern: pattern
+
     parse: (text, matchProfileName, defaultProfileName) ->
       text = text.trim()
       normal_rules = []
@@ -79,8 +97,7 @@ module.exports = exports =
           line = line.substring(1)
         cond = switch section
           when 'WILDCARD'
-            conditionType: 'UrlWildcardCondition'
-            pattern: line
+            exports['Switchy'].conditionFromLegacyWildcard(line)
           when 'REGEXP'
             conditionType: 'UrlRegexCondition'
             pattern: line

+ 1 - 15
omega-target-chromium-extension/src/upgrade.coffee

@@ -158,21 +158,7 @@ module.exports = (oldOptions, i18n) ->
         switch rule['patternType']
           when 'wildcard'
             pattern = rule['urlPattern']
-            if pattern[0] == '@'
-              pattern = pattern.substring(1)
-            else
-              if pattern.indexOf('://') <= 0 and pattern[0] != '*'
-                pattern = '*' + pattern
-              if pattern[pattern.length - 1] != '*'
-                pattern += '*'
-
-            host = OmegaPac.Conditions.urlWildcard2HostWildcard(pattern)
-            if host
-              conditionType: 'HostWildcardCondition'
-              pattern: host
-            else
-              conditionType: 'UrlWildcardCondition'
-              pattern: pattern
+            OmegaPac.RuleList['Switchy'].conditionFromLegacyWildcard(pattern)
           else
             conditionType: 'UrlRegexCondition'
             pattern: rule['urlPattern']

+ 4 - 0
omega-web/src/omega/controllers/profile.coffee

@@ -83,6 +83,10 @@ angular.module('omega').controller 'ProfileCtrl', ($scope, $stateParams,
         revisionChanged = true
     this.$watch expression, onChange, true
 
+  $scope.exportRuleList = null
+  $scope.setExportRuleListHandler = (exportRuleList) ->
+    $scope.exportRuleList = exportRuleList
+
   unwatch = $scope.$watch (-> $scope.options?['+' + name]), (profile) ->
     if not profile
       if $scope.options

+ 34 - 0
omega-web/src/omega/controllers/switch_profile.coffee

@@ -40,6 +40,38 @@ angular.module('omega').controller 'SwitchProfileCtrl', ($scope, $location,
     }
   ]
 
+  exportRuleList = ->
+    wildcardRules = ''
+    regexpRules = ''
+    for rule in $scope.profile.rules
+      i = ''
+      if rule.profileName == 'direct'
+        i = '!'
+      switch rule.condition.conditionType
+        when 'HostWildcardCondition'
+          wildcardRules += i + '@*://' + rule.condition.pattern + '/*' + '\r\n'
+        when 'UrlWildcardCondition'
+          wildcardRules += i + '@' + rule.condition.pattern + '\r\n'
+        when 'UrlRegexCondition'
+          regexpRules += i + rule.condition.pattern + '\r\n'
+
+    text = """
+      ; Summary: Proxy Switchy! Exported Rule List
+      ; Date: #{new Date().toLocaleDateString()}
+      ; Website: http://bit.ly/proxyswitchy
+
+      #BEGIN
+
+      [wildcard]
+      #{wildcardRules}
+      [regexp]
+      #{regexpRules}
+      #END
+    """
+    blob = new Blob [text], {type: "text/plain;charset=utf-8"}
+    fileName = $scope.profile.name.replace(/\W+/g, '_')
+    saveAs(blob, "SwitchyRules_#{fileName}.ssrl")
+
   expandGroups = (groups) ->
     result = []
     for group in groups
@@ -82,8 +114,10 @@ angular.module('omega').controller 'SwitchProfileCtrl', ($scope, $location,
       $scope.showConditionTypes = $scope.hasConditionTypes
     if $scope.showConditionTypes == 0
       $scope.conditionTypes = basicConditionTypesExpanded
+      $scope.setExportRuleListHandler(exportRuleList)
     else
       $scope.conditionTypes = advancedConditionTypesExpanded
+      $scope.setExportRuleListHandler(null)
       if not $scope.options["-showConditionTypes"]?
         $scope.options["-showConditionTypes"] = $scope.showConditionTypes
       unwatchRules?()

+ 5 - 0
omega-web/src/partials/profile.jade

@@ -1,5 +1,10 @@
 .page-header
   .profile-actions
+    button.btn.btn-default(ng-show='exportRuleList' ng-click='exportRuleList(profile.name)' title="{{'options_profileExportRuleListHelp' | tr}}")
+      span.glyphicon.glyphicon-list
+      = ' '
+      | {{'options_profileExportRuleList' | tr}}
+    = ' '
     button.btn.btn-default(ng-show='scriptable' ng-click='exportScript(profile.name)' title="{{'options_exportPacFileHelp' | tr}}")
       span.glyphicon.glyphicon-download
       = ' '