-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
112 changed files
with
6,124 additions
and
1,249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#use_frameworks! | ||
source 'https://github.com/CocoaPods/Specs.git' | ||
target 'Stay' do | ||
pod 'InterAppCommunication' | ||
pod 'SDWebImage' | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// | ||
// MatchPattern.h | ||
// Stay Extension | ||
// | ||
// Created by ris on 2022/5/25. | ||
// | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface MatchPattern : NSObject | ||
|
||
- (instancetype)initWithPattern:(NSString *)pattern; | ||
- (BOOL)doMatch:(NSString *)urlString; | ||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
// | ||
// MatchPattern.m | ||
// Stay Extension | ||
// | ||
// Created by ris on 2022/5/25. | ||
// | ||
|
||
#import "MatchPattern.h" | ||
|
||
@interface MatchPattern() | ||
|
||
@property (nonatomic, assign) BOOL all; | ||
@property (nonatomic, strong) NSString *protocol; | ||
@property (nonatomic, assign) BOOL pass; | ||
@property (nonatomic, strong) NSRegularExpression *hostExpr; | ||
@property (nonatomic, strong) NSRegularExpression *pathExpr; | ||
|
||
//Const members. | ||
@property (nonatomic, strong) NSArray<NSString *> *validProtocols; | ||
@property (nonatomic, strong) NSRegularExpression *REG_PARTS; | ||
@property (nonatomic, strong) NSRegularExpression *REG_HOST; | ||
@property (nonatomic, strong) NSRegularExpression *tldRegExp; | ||
|
||
@end | ||
|
||
@implementation MatchPattern | ||
|
||
- (instancetype)initWithPattern:(NSString *)pattern{ | ||
if (self = [super init]){ | ||
if ([pattern isEqualToString:@"<all_urls>"]){ | ||
self.all = YES; | ||
self.protocol = @"all_urls"; | ||
} | ||
|
||
do{ | ||
NSArray<NSTextCheckingResult *> *results = [self.REG_PARTS matchesInString:pattern | ||
options:0 | ||
range:NSMakeRange(0, pattern.length)]; | ||
|
||
if (results.count == 0){ | ||
NSLog(@"Pattern (%@) is not vailed",pattern); | ||
break; | ||
} | ||
|
||
NSTextCheckingResult *result = results.firstObject; | ||
NSInteger n = result.numberOfRanges; | ||
|
||
if (n < 4){ | ||
NSLog(@"Pattern (%@) is not vailed",pattern); | ||
break; | ||
} | ||
|
||
self.protocol = [pattern substringWithRange:[result rangeAtIndex:1]]; | ||
NSString *host = [pattern substringWithRange:[result rangeAtIndex:2]]; | ||
NSString *path = [pattern substringWithRange:[result rangeAtIndex:3]]; | ||
|
||
if (![self.protocol isEqualToString:@"*:"] && ![self.validProtocols containsObject:self.protocol]){ | ||
NSLog(@"@match: Invalid protocol (%@) specified.",self.protocol); | ||
break; | ||
} | ||
|
||
results = [self.REG_HOST matchesInString:host options:0 range:NSMakeRange(0, host.length)]; | ||
if (results.count == 0){ | ||
NSLog(@"@match: Invalid host (%@) specified.",host); | ||
break; | ||
} | ||
|
||
if (![[path substringWithRange:NSMakeRange(0, 1)] isEqualToString:@"/"]){ | ||
NSLog(@"@match: Invalid path (%@) specified.",path); | ||
break; | ||
} | ||
|
||
if (host.length > 0){ | ||
NSString *expr = [[[host stringByReplacingOccurrencesOfString:@"." withString:@"\\."] | ||
stringByReplacingOccurrencesOfString:@"*" withString:@".*"] | ||
stringByReplacingOccurrencesOfString:@"*." withString:@"*\\."]; | ||
self.hostExpr = [[NSRegularExpression alloc] initWithPattern:[NSString stringWithFormat:@"^%@$",expr] options:0 error:nil]; | ||
|
||
} | ||
|
||
//TLD missed. | ||
NSMutableString *builder = [[NSMutableString alloc] initWithString:@"^"]; | ||
for (int i = 0; i < path.length; i++){ | ||
unichar c = [path characterAtIndex:i]; | ||
switch(c){ | ||
case '*' : [builder appendString:@".*"]; | ||
break; | ||
case '.' : | ||
case '?' : | ||
case '^' : | ||
case '$' : | ||
case '+' : | ||
case '{' : | ||
case '}' : | ||
case '[' : | ||
case ']' : | ||
case '|' : | ||
case '(' : | ||
case ')' : | ||
case '\\' : [builder appendFormat:@"\\%C",c]; | ||
break; | ||
case ' ' : | ||
break; | ||
default : [builder appendFormat:@"%C",c]; | ||
break; | ||
} | ||
} | ||
|
||
[builder appendString:@"$"]; | ||
self.pathExpr = [[NSRegularExpression alloc] initWithPattern:builder options:0 error:nil]; | ||
self.pass = YES; | ||
|
||
}while(0); | ||
} | ||
|
||
return self; | ||
} | ||
|
||
- (NSArray<NSString *> *)validProtocols{ | ||
return @[@"http:", @"https:"]; | ||
} | ||
|
||
- (NSRegularExpression *)REG_PARTS{ | ||
if (nil == _REG_PARTS){ | ||
_REG_PARTS = [[NSRegularExpression alloc] initWithPattern:@"^([a-z*]+:|\\*:)\\/\\/([^\\/]+)?(\\/.*)$" options:0 error:nil]; | ||
} | ||
return _REG_PARTS; | ||
} | ||
|
||
- (NSRegularExpression *)REG_HOST{ | ||
if (nil == _REG_HOST){ | ||
_REG_HOST = [[NSRegularExpression alloc] initWithPattern:@"^(?:\\*\\.)?[^*\\/]+$|^\\*$|^$" options:0 error:nil]; | ||
} | ||
return _REG_HOST; | ||
} | ||
|
||
- (NSRegularExpression *)tldRegExp{ | ||
if (nil == _tldRegExp){ | ||
_tldRegExp = [[NSRegularExpression alloc] initWithPattern:@"^([^:]+:\\/\\/[^\\/]+)\\.tld(\\/.*)?$" options:0 error:nil]; | ||
} | ||
return _tldRegExp; | ||
} | ||
|
||
- (BOOL)doMatch:(NSString *)urlString{ | ||
if(!self.pass) return NO; | ||
NSURL *url = [NSURL URLWithString:urlString]; | ||
if (!url) return NO; | ||
if (self.all) return YES; | ||
|
||
return [self.hostExpr matchesInString:url.host options:0 range:NSMakeRange(0, url.host.length)].count > 0 | ||
&& [self.pathExpr matchesInString:url.path options:0 range:NSMakeRange(0, url.path.length)].count > 0; | ||
} | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
|
||
|
||
console.log("test checkUserscript") | ||
let userscriptText = document.body.textContent; | ||
|
||
let meta = extractMeta(userscriptText).match(/.+/g); | ||
const is_dark = () => { | ||
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches; | ||
} | ||
if(meta){ | ||
let url = encodeURIComponent(window.location.href); | ||
let stayImg = browser.runtime.getURL("images/icon-256.png"); | ||
let bg = "background: #fff;"; | ||
let fontColor = "color: #000000;" | ||
if (is_dark()) { | ||
bg = "background: #000;"; | ||
fontColor = "color: #F3F3F3;" | ||
} | ||
let schemeUrl = "stay://x-callback-url/install?scriptURL="+url; | ||
let popToastTemp = [ | ||
'<div id="popToast" style="width: 270px;height: 57px;transform: translateX(-50%);border-radius: 16px; ' + bg + ' position: fixed; bottom: 10;left: 50%;box-shadow: 0 12px 32px rgba(0, 0, 0, .1), 0 2px 6px rgba(0, 0, 0, .08);display: flex;flex-direction: row;">', | ||
'<a id="popImg" href="' + schemeUrl +'" style="text-decoration: none;width: 75px;display: flex;flex-direction: row;align-items:center;justify-content: center;justify-items: center;"><img src=' + stayImg +' style="width: 46px;height: 46px;"></img></a>', | ||
'<a id="popInstall" href="' + schemeUrl +'" style="font-family:Helvetica Neue;text-decoration: none;display: flex;flex-direction: column;justify-content: center;justify-items: center;align-items:center;line-height:23px;">', | ||
'<div style="font-size: 17px; color: #B620E0;font-weight:700;">Tap to install</div>', | ||
'<div style="font-size: 13px; ' + fontColor +' ">Stay 2 - Local scrip manager</div>', | ||
'</a>', | ||
'</div>' | ||
]; | ||
let temp = popToastTemp.join(""); | ||
let tempDom = document.createElement("div"); | ||
tempDom.innerHTML = temp; | ||
document.body.appendChild(tempDom); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.