| 5455 | } |
| 5456 | |
| 5457 | addToHpRules(instead) { |
| 5458 | try { |
| 5459 | if (this.curSiteRule.isScript) return; |
| 5460 | if (!this.hpRules) this.hpRules = []; |
| 5461 | let url = this.curSiteRule && this.curSiteRule.url, self = this; |
| 5462 | let href = location.href.slice(0, 500); |
| 5463 | if (instead) { |
| 5464 | this.hpRules = this.hpRules.filter(item => { |
| 5465 | return item && !(new RegExp(item.url, "i").test(href) && self.ruleMatch(item)); |
| 5466 | }); |
| 5467 | } |
| 5468 | let matchedRules = this.hpRules.filter(rule => JSON.stringify(rule) != JSON.stringify(self.curSiteRule) && new RegExp(rule.url, "i").test(href) && self.ruleMatch(rule)); |
| 5469 | if (url) matchedRules.unshift(this.curSiteRule); |
| 5470 | matchedRules.sort((a, b) => { |
| 5471 | if ((a.include || a.exclude) && (!b.include && !b.exclude)) { |
| 5472 | return -1; |
| 5473 | } else if ((b.include || b.exclude) && (!a.include && !a.exclude)) { |
| 5474 | return 1; |
| 5475 | } else { |
| 5476 | if ((a.nextLink || a.pageElement) && (!b.nextLink && !b.pageElement)) { |
| 5477 | return -1; |
| 5478 | } else if ((b.nextLink || b.pageElement) && (!a.nextLink && !a.pageElement)) { |
| 5479 | return 1; |
| 5480 | } else if (a.from === b.from) { |
| 5481 | if (a.url.length > b.url.length) { |
| 5482 | return -1; |
| 5483 | } else if (a.url.length < b.url.length) { |
| 5484 | return 1; |
| 5485 | } else { |
| 5486 | return 0; |
| 5487 | } |
| 5488 | } else { |
| 5489 | return 0; |
| 5490 | } |
| 5491 | } |
| 5492 | }); |
| 5493 | this.hpRules = this.hpRules.filter(item => { |
| 5494 | return item && !matchedRules.find(rule => item.url == rule.url && JSON.stringify(item) == JSON.stringify(rule)); |
| 5495 | }); |
| 5496 | if (instead) { |
| 5497 | if (url) { |
| 5498 | this.hpRules.unshift(this.curSiteRule); |
| 5499 | matchedRules = []; |
| 5500 | } |
| 5501 | } else { |
| 5502 | this.hpRules = matchedRules.concat(this.hpRules); |
| 5503 | } |
| 5504 | if (matchedRules && matchedRules.length) this.curSiteRule = matchedRules[0]; |
| 5505 | if (this.hpRules.length > 30) { |
| 5506 | this.hpRules.pop(); |
| 5507 | } |
| 5508 | if (!rulesData.sort) rulesData.sort = [1]; |
| 5509 | this.hpRules.sort((a, b) => { |
| 5510 | let aSort = -1, bSort = -1; |
| 5511 | for (let s = 0; s < rulesData.sort.length; s++) { |
| 5512 | if (a.from == rulesData.sort[s]) aSort = s; |
| 5513 | if (b.from == rulesData.sort[s]) bSort = s; |
| 5514 | if (aSort > -1 && bSort > -1) break; |