Skip to content

Instantly share code, notes, and snippets.

@ozero
Last active May 21, 2016 03:35
Show Gist options
  • Save ozero/4ccc663dc25720b2b944fb1291db8b6a to your computer and use it in GitHub Desktop.
Save ozero/4ccc663dc25720b2b944fb1291db8b6a to your computer and use it in GitHub Desktop.
How to inject userscript (IITC) into UWP WebView
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<WebView x:Name="webView1" HorizontalAlignment="Center" Height="640" VerticalAlignment="Top"
Source="https://www.ingress.com/intel/?vp=m" Width="360"
DOMContentLoaded="webView1_DOMContentLoaded" />
</Grid>
</Page>
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
// 空白ページのアイテム テンプレートについては、http://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 を参照してください
namespace App1
{
/// <summary>
/// それ自体で使用できる空白ページまたはフレーム内に移動できる空白ページ。
/// </summary>
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
}
private async void webView1_DOMContentLoaded(WebView sender, WebViewDOMContentLoadedEventArgs args)
{
if (System.Text.RegularExpressions.Regex.IsMatch(
webView1.Source.ToString(),
@"ingress.com",
System.Text.RegularExpressions.RegexOptions.ECMAScript))
{
System.Diagnostics.Debug.WriteLine("Site:intel, Continue to inject");
}else {
System.Diagnostics.Debug.WriteLine("Site:others: "+ webView1.Source.ToString());
return;
}
var js = "u = ["
+ "'%R%total-conversion-build.user.js',"
+ "'%R%plugins/portal-highlighter-high-level.user.js',"
+ "'%R%plugins/player-tracker.user.js',"
+ "'%R%plugins/basemap-openstreetmap.user.js',"
+ "'%R%plugins/bookmarks-by-zaso.user.js',"
+ "'%R%plugins/sync.user.js',"
+ "'%R%plugins/draw-tools.user.js',"
+ "'%R%plugins/cross_link.user.js',"
+ "'%R%plugins/show-less-portals-zoomed-out.user.js',"
+ "'%R%plugins/score-cycle-times.user.js',"
+ "];"
+ "for(i in u){"
+ "s=document.createElement('script');"
+ "s.type='text/javascript';"
+ "s.src=u[i].replace('%R%','https://secure.jonatkins.com/iitc/release/');"
+ "document.getElementsByTagName('head')[0].appendChild(s);"
+ "};"
+"(function(){return 'IITC Loaded!';})();";
string result = await webView1.InvokeScriptAsync("eval", new string[] { js });
System.Diagnostics.Debug.WriteLine(result);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment