NSPasteboard,如何获取html内容的URL

时间:2017-07-21 20:49:25

标签: html macos url clipboard

我的应用程序接受从剪贴板粘贴的HTML或从浏览器拖动的HTML。

我能够成功获取HTML(片段):

export class Foo {
    constructor(name: string) {}
}

export class MyComponent implements OnInit {

    myArray = [];        

    ngOnInit() {
        this.service.get().then((data) => {
           //This won't work: 
           this.myArray.push(this.doSomething(data));

           //This will work:
           this.myArray = [ this.doSomething(data) ];
        }
    } 

    doSomething(data: any): Foo {
        return new Foo("hello");
    }
}

<select multiple name="myArray" ngModel [(ngModel)]="myArray" >
    <option *ngFor="let a of ..." [ngValue]="...">...</option>  
</select>

但不知道如何获取源文档的URL。

试过这个:

  NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];

  NSString* nstext = [pasteboard stringForType:NSPasteboardTypeHTML];

但没有运气 - 报告 NSLog(@"url: %@", [NSURL URLFromPasteboard:pasteboard]);

原则上可行吗?

以防万一,在Windows上,我可以从CF_HTML剪贴板格式的某个字段中获取该URL。

1 个答案:

答案 0 :(得分:1)

这对我有用。为简洁起见,省略了错误检查等:

NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSData *data = [pasteboard dataForType:@"Apple Web Archive pasteboard type"];
NSDictionary *plist = [NSPropertyListSerialization propertyListWithData:data
                                                                options:NSPropertyListImmutable
                                                                 format:NULL
                                                                  error:nil];

NSDictionary *values = [plist objectForKey:@"WebMainResource"];
NSString *url = [values objectForKey:@"WebResourceURL"];