Re: stripped down webview
D'oh, I just discoverd that the website I'd like to display has a mobile version, which is already basic. I'll just display that instead in my webView. This only needs some minor tweaks to remove one or two divs, and saves me a lot of headaches. - Koen. On Sun, Aug 19, 2012 at 10:00 AM, Koen van der Drift koenvanderdr...@gmail.com wrote: So for future reference, I figured out how to add my local javascript. This is what I do: - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame { if (frame == [sender mainFrame]) { NSError *error = nil; // get the DOMDocument DOMDocument* domDocument = [frame DOMDocument]; DOMElement* headElement= (DOMElement*)[[domDocument getElementsByTagName: @head] item: 0]; // get the reference.js file NSString *path = [[NSBundle mainBundle] pathForResource: @test ofType: @js]; NSString *js = [NSString stringWithContentsOfFile: path encoding: NSUTF8StringEncoding error: error]; if (js headElement error = nil) { // create a script element DOMElement* scriptElement = [domDocument createElement: @script]; [scriptElement setAttribute: @type value: @text/javascript]; // add the default css to it DOMText *jsText = [domDocument createTextNode: js]; [scriptElement appendChild: jsText]; // add it to head [headElement appendChild: scriptElement]; } } } My test.js file looks like this: function test() { document.write(pThis is a test./p); } test(); And all my webview shows is This is a test. It even picks up the css for the p element I declared in my custom css class! I didn't have to use stringByEvaluatingJavaScriptFromString, although it definitely was useful for debugging purposes. Next, changing the content by stripping out some divs. - Koen. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: stripped down webview
On 18 Aug 2012, at 22:48, Koen van der Drift koenvanderdr...@gmail.com wrote: Is there a similar way to inject an external javascript.js file into my webView? I think I can use that to strip out the divs I don't want to show. You could use the resource load delegate to substitute in a custom CSS file in place of the one the page specifies. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: stripped down webview
So for future reference, I figured out how to add my local javascript. This is what I do: - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame { if (frame == [sender mainFrame]) { NSError *error = nil; // get the DOMDocument DOMDocument* domDocument = [frame DOMDocument]; DOMElement* headElement= (DOMElement*)[[domDocument getElementsByTagName: @head] item: 0]; // get the reference.js file NSString *path = [[NSBundle mainBundle] pathForResource: @test ofType: @js]; NSString *js = [NSString stringWithContentsOfFile: path encoding: NSUTF8StringEncoding error: error]; if (js headElement error = nil) { // create a script element DOMElement* scriptElement = [domDocument createElement: @script]; [scriptElement setAttribute: @type value: @text/javascript]; // add the default css to it DOMText *jsText = [domDocument createTextNode: js]; [scriptElement appendChild: jsText]; // add it to head [headElement appendChild: scriptElement]; } } } My test.js file looks like this: function test() { document.write(pThis is a test./p); } test(); And all my webview shows is This is a test. It even picks up the css for the p element I declared in my custom css class! I didn't have to use stringByEvaluatingJavaScriptFromString, although it definitely was useful for debugging purposes. Next, changing the content by stripping out some divs. - Koen. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
stripped down webview
In my OS X app I show some webpages in a WebView, but I'd like to show them stripped down, remove clutter, e.g. as is done in Evernote and other apps. Any suggestions where to start? Maybe use a local css file? - Koen. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: stripped down webview
On Aug 18, 2012, at 7:24 AM, Koen van der Drift koenvanderdr...@gmail.com wrote: In my OS X app I show some webpages in a WebView, but I'd like to show them stripped down, remove clutter, e.g. as is done in Evernote and other apps. Any suggestions where to start? Maybe use a local css file? Answering to myself... Alright, I found some clues on SO, and am now able to replace the css file with my own by tweaking the DOMDocument. The only place I could think of to do this is in - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame But since the webview loads asynchronously, I first see the page with the original css for a second or two, and then it changes to the page using my local css file only after the whole page has loaded. Is there another place where I could hook into so the page shows correctly immediately? Next is to strip the DOM form all elements I don't need :) - Koen. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: stripped down webview
How about asynchronously downloading the page HTML into a string, doing a text replace of the name of the CSS file to your local one, then passing the modified HTML to the web view? Sent from my iPhone On Aug 18, 2012, at 6:52 AM, Koen van der Drift koenvanderdr...@gmail.com wrote: On Aug 18, 2012, at 7:24 AM, Koen van der Drift koenvanderdr...@gmail.com wrote: In my OS X app I show some webpages in a WebView, but I'd like to show them stripped down, remove clutter, e.g. as is done in Evernote and other apps. Any suggestions where to start? Maybe use a local css file? Answering to myself... Alright, I found some clues on SO, and am now able to replace the css file with my own by tweaking the DOMDocument. The only place I could think of to do this is in - (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame But since the webview loads asynchronously, I first see the page with the original css for a second or two, and then it changes to the page using my local css file only after the whole page has loaded. Is there another place where I could hook into so the page shows correctly immediately? Next is to strip the DOM form all elements I don't need :) - Koen. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/punster%40mac.com This email sent to puns...@mac.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: stripped down webview
On Aug 18, 2012, at 8:26 AM, Steve Christensen puns...@mac.com wrote: How about asynchronously downloading the page HTML into a string, doing a text replace of the name of the CSS file to your local one, then passing the modified HTML to the web view? Doesn't work if there are style rules directly in the HTML, which is not uncommon. Also, parsing HTML is kind of a minefield given the sort of malformed tag-soup that's common on teh interwebz. —Jens ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: stripped down webview
On Aug 18, 2012, at 4:24 AM, Koen van der Drift koenvanderdr...@gmail.com wrote: In my OS X app I show some webpages in a WebView, but I'd like to show them stripped down, remove clutter, e.g. as is done in Evernote and other apps. Any suggestions where to start? Maybe use a local css file? This is a good start. I believe user stylesheet is the term of art for a local override stylesheet. From a quick Google, the relevant APIs are on the WebPreferences class: -setUserStyleSheetEnabled: and -setUserStyleSheetLocation:. You could start with one of the common reset stylesheets and customize it from there. --Kyle Sluder ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: stripped down webview
On Aug 18, 2012, at 1:40 PM, Kyle Sluder k...@ksluder.com wrote: This is a good start. I believe user stylesheet is the term of art for a local override stylesheet. From a quick Google, the relevant APIs are on the WebPreferences class: -setUserStyleSheetEnabled: and -setUserStyleSheetLocation:. You could start with one of the common reset stylesheets and customize it from there. I tried that, but the settings in my css file are not used. WebPreferences *prefs = [WebPreferences standardPreferences]; [prefs setJavaScriptEnabled: YES]; [prefs setCacheModel: WebCacheModelDocumentBrowser]; [prefs setPlugInsEnabled: YES]; NSString *path = [[NSBundle mainBundle] pathForResource: @default ofType: @css]; NSURL *css = [NSURL URLWithString: path]; [prefs setUserStyleSheetEnabled: YES]; [prefs setUserStyleSheetLocation: css]; [self.webView setPreferences: prefs]; So I am now hacking in the DOM, and this way I can add my stylesheet: NSError *error = nil; DOMDocument* domDocument = [webView mainFrameDocument]; DOMElement* styleElement = [domDocument createElement: @style]; [styleElement setAttribute: @type value: @text/css]; NSString *path = [[NSBundle mainBundle] pathForResource: @default ofType: @css]; NSString *css = [NSString stringWithContentsOfFile: path encoding: NSUTF8StringEncoding error: error]; DOMText *cssText = [domDocument createTextNode: css]; [styleElement appendChild: cssText]; DOMElement* headElement= (DOMElement*)[[domDocument getElementsByTagName: @head] item:0]; [headElement appendChild:styleElement]; This way the css gets inserted into the DOM, but I still have the delay. I am open for any suggestions/improvements :) - Koen. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: stripped down webview
User stylesheets should work just fine, so you should try to figure out what's going wrong with that. Perhaps don't use standardPreferences; use [self.webView setPreferencesIdentifier:@mySpecialPreferences] to generate a new preferences object for your WebView, then [self.webView preferences] to get a preferences object to customize. If that still doesn't work, why don't you just hide your WebView and show a spinner while it's loading, and then after it loads and you've injected your stylesheets, show it? -Matt On Aug 18, 2012, at 11:29 AM, Koen van der Drift koenvanderdr...@gmail.com wrote: On Aug 18, 2012, at 1:40 PM, Kyle Sluder k...@ksluder.com wrote: This is a good start. I believe user stylesheet is the term of art for a local override stylesheet. From a quick Google, the relevant APIs are on the WebPreferences class: -setUserStyleSheetEnabled: and -setUserStyleSheetLocation:. You could start with one of the common reset stylesheets and customize it from there. I tried that, but the settings in my css file are not used. WebPreferences *prefs = [WebPreferences standardPreferences]; [prefs setJavaScriptEnabled: YES]; [prefs setCacheModel: WebCacheModelDocumentBrowser]; [prefs setPlugInsEnabled: YES]; NSString *path = [[NSBundle mainBundle] pathForResource: @default ofType: @css]; NSURL *css = [NSURL URLWithString: path]; [prefs setUserStyleSheetEnabled: YES]; [prefs setUserStyleSheetLocation: css]; [self.webView setPreferences: prefs]; So I am now hacking in the DOM, and this way I can add my stylesheet: NSError *error = nil; DOMDocument* domDocument = [webView mainFrameDocument]; DOMElement* styleElement = [domDocument createElement: @style]; [styleElement setAttribute: @type value: @text/css]; NSString *path = [[NSBundle mainBundle] pathForResource: @default ofType: @css]; NSString *css = [NSString stringWithContentsOfFile: path encoding: NSUTF8StringEncoding error: error]; DOMText *cssText = [domDocument createTextNode: css]; [styleElement appendChild: cssText]; DOMElement* headElement= (DOMElement*)[[domDocument getElementsByTagName: @head] item:0]; [headElement appendChild:styleElement]; This way the css gets inserted into the DOM, but I still have the delay. I am open for any suggestions/improvements :) - Koen. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/matt%40mattpatenaude.com This email sent to m...@mattpatenaude.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: stripped down webview
On Aug 18, 2012, at 2:46 PM, Matt Patenaude m...@mattpatenaude.com wrote: User stylesheets should work just fine, so you should try to figure out what's going wrong with that. Perhaps don't use standardPreferences; use [self.webView setPreferencesIdentifier:@mySpecialPreferences] to generate a new preferences object for your WebView, then [self.webView preferences] to get a preferences object to customize. Nope, still doen't work, the custom css is not used. Unless I did something wrong: [self.webView setPreferencesIdentifier: @mySpecialPreferences]; WebPreferences *prefs = [self.webView preferences]; [prefs setJavaScriptEnabled: YES]; [prefs setCacheModel: WebCacheModelDocumentBrowser]; [prefs setPlugInsEnabled: YES]; NSString *path = [[NSBundle mainBundle] pathForResource: @default ofType: @css]; NSURL *cssUrl = [NSURL URLWithString: path]; [prefs setUserStyleSheetEnabled: YES]; [prefs setUserStyleSheetLocation: cssUrl]; [self.webView setPreferences: prefs]; If that still doesn't work, why don't you just hide your WebView and show a spinner while it's loading, and then after it loads and you've injected your stylesheets, show it? The delay is a separate issue, but this works great to prevent it. Thanks! - Koen. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: stripped down webview
Ah! Your problem is URLWithString:. You want fileURLWithPath:. :) -Matt Sent from my iPhone On Aug 18, 2012, at 12:12 PM, Koen van der Drift koenvanderdr...@gmail.com wrote: On Aug 18, 2012, at 2:46 PM, Matt Patenaude m...@mattpatenaude.com wrote: User stylesheets should work just fine, so you should try to figure out what's going wrong with that. Perhaps don't use standardPreferences; use [self.webView setPreferencesIdentifier:@mySpecialPreferences] to generate a new preferences object for your WebView, then [self.webView preferences] to get a preferences object to customize. Nope, still doen't work, the custom css is not used. Unless I did something wrong: [self.webView setPreferencesIdentifier: @mySpecialPreferences]; WebPreferences *prefs = [self.webView preferences]; [prefs setJavaScriptEnabled: YES]; [prefs setCacheModel: WebCacheModelDocumentBrowser]; [prefs setPlugInsEnabled: YES]; NSString *path = [[NSBundle mainBundle] pathForResource: @default ofType: @css]; NSURL *cssUrl = [NSURL URLWithString: path]; [prefs setUserStyleSheetEnabled: YES]; [prefs setUserStyleSheetLocation: cssUrl]; [self.webView setPreferences: prefs]; If that still doesn't work, why don't you just hide your WebView and show a spinner while it's loading, and then after it loads and you've injected your stylesheets, show it? The delay is a separate issue, but this works great to prevent it. Thanks! - Koen. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: stripped down webview
On Aug 18, 2012, at 3:26 PM, Matt Patenaude m...@mattpatenaude.com wrote: Ah! Your problem is URLWithString:. You want fileURLWithPath:. :) Well spotted, thanks! - Koen. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: stripped down webview
Is there a similar way to inject an external javascript.js file into my webView? I think I can use that to strip out the divs I don't want to show. On Aug 18, 2012, at 3:32 PM, Koen van der Drift koenvanderdr...@gmail.com wrote: On Aug 18, 2012, at 3:26 PM, Matt Patenaude m...@mattpatenaude.com wrote: Ah! Your problem is URLWithString:. You want fileURLWithPath:. :) Well spotted, thanks! - Koen. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: stripped down webview
You can use the DOM: dynamically create a Script tag then allow it to load more stuff by abusing -stringByEvaluatingJavaScriptFromString: . On Aug 18, 2012, at 2:51 PM, Koen van der Drift koenvanderdr...@gmail.com wrote: Is there a similar way to inject an external javascript.js file into my webView? I think I can use that to strip out the divs I don't want to show. On Aug 18, 2012, at 3:32 PM, Koen van der Drift koenvanderdr...@gmail.com wrote: On Aug 18, 2012, at 3:26 PM, Matt Patenaude m...@mattpatenaude.com wrote: Ah! Your problem is URLWithString:. You want fileURLWithPath:. :) Well spotted, thanks! - Koen. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/ev%40monoceroi.com This email sent to e...@monoceroi.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: stripped down webview
On Aug 18, 2012, at 11:29 AM, Koen van der Drift koenvanderdr...@gmail.com wrote: This is a good start. I believe user stylesheet is the term of art for a local override stylesheet. From a quick Google, the relevant APIs are on the WebPreferences class: -setUserStyleSheetEnabled: and -setUserStyleSheetLocation:. You could start with one of the common reset stylesheets and customize it from there. I tried that, but the settings in my css file are not used. That will establish default values, but values in page stylesheets will still override them. If you want your values to override those on the page, add the !important modifier to them. —Jens ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com