當前位置:首頁 » 操作系統 » js獲取頁面源碼

js獲取頁面源碼

發布時間: 2023-05-15 22:51:56

㈠ Android 在WebView中通過javascript獲取網頁源碼,並在TextView或者在EditText中顯示問題

應該是public void showSource(String html) {} 運行在非UI線程裡面, 你嘗試在這個方法裡面使用handler.sendMessage() 然後在handler的handlerMessage 方法中更新TextView中的內容試試

㈡ Android 在WebView中通過javascript獲取網頁源碼,並在TextView或者在EditText中顯示問題

webview js之間的交互,項目中馬上用到。

JS調用java代碼效果圖


index.html代碼:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd";><html><head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript" language="javascript"> var share = JSON.stringify({"title": "sinodata",
"desc": "ios",
"shareUrl": "http://www.sinodata.com.cn"
});

function sendInfoToJava(){
window.AndroidWebView.showInfoFromJs(share);
}

<!--在android代碼中調用此方法-->
function showInfoFromJava(msg){
alert("showInfoFromJava:"+msg);
} </script></head><body la><div id='b'> <input onclick="sendInfoToJava()" type="button" value="sendInfoToJava"/></div></body></html>
布局代碼:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.chenjifang.webview.MainActivity"> <Button android:id="@+id/test_btn" android:text="代碼中調用web js代碼傳遞參數" android:layout_width="match_parent" android:layout_height="wrap_content" /> <EditText android:id="@+id/test_edt" android:layout_width="match_parent" android:layout_height="wrap_content" /><WebView android:id="@+id/test_webview" android:layout_width="match_parent" android:layout_height="400dp"></WebView></LinearLayout>
java代碼:

public class MainActivity extends AppCompatActivity {private WebView mWebView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mWebView = (WebView) findViewById(R.id.test_webview); //設置WebView支持JavaScript mWebView.getSettings().setJavaScriptEnabled(true); mWebView.loadUrl("file:///android_asset/index.html"); mWebView.addJavascriptInterface(new JsInterface(this), "AndroidWebView"); //添加客戶端支持 mWebView.setWebChromeClient(new WebChromeClient()); findViewById(R.id.test_btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {
sendInfoToJs(); }
}); } private class JsInterface { private Context mContext; public JsInterface(Context context) { this.mContext = context; } //在js中調用window.AndroidWebView.showInfoFromJs(name),便會觸發此方法。 @JavascriptInterface public void showInfoFromJs(String share) {
Toast.makeText(mContext, share, Toast.LENGTH_SHORT).show(); }
} //在java中調用js代碼 public void sendInfoToJs() {
String msg = ((EditText)findViewById(R.id.test_edt)).getText().toString(); //調用js中的函數:showInfoFromJava(msg) mWebView.loadUrl("javascript:showInfoFromJava('" + msg + "')"); }
總結下,java代碼中要設置webview對javascript的支持,addJavascriptInterface(new JsInterface(this), "AndroidWebView");//這句代碼中的第二個參數是在js訪問方法的地址。
window.AndroidWebView.showInfoFromJs(share);

㈢ 如何獲取JS執行過後的網頁源代碼

需要獲取網頁中的javascript執行後生成的完整的網頁源碼(通常使用的右鍵-查看源代碼是看不到js執行後的內容的,用firefox的firebug看到的代碼就是js執行後的代碼),從中提取一些有用的數據。

㈣ js寫個獲取源碼的函數

js文件中:
function getHtml(id)
{
var obj=document.all(id)

return obj.innerHTML;

}

在調用的頁面head頭部增加<script src="js.js" type="text/javascript"></script>
然後在頁面需要的地方調用函數 getHtml 就可以了

㈤ 如何通過JavaScript或者jQuery非同步實現獲取遠程網頁源碼,例如按下按鈕就顯示某網站的源

js和jquery無法直接獲取遠程網站的原碼納鋒,因為ajax無法跨域,如果你想引用,直接用iframe不就鉛桐得了。如果想獲取源碼,用js訪問php,讓php去干這活,然槐茄坦後返回給頁面
方法有了,自己動手

㈥ js讀取遠程網頁源碼

代碼如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>遠程網頁源代碼讀取</title>
<style type="text/css">
/* 頁面字體樣式 */
body, td, input, textarea {
font-family:Arial;
font-size:12px;
}
</style>
<script type="text/javascript">
//用於創建XMLHttpRequest對象
function createXmlHttp() {
//根據window.XMLHttpRequest對象是否存在使用不同的創建方式
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest(); //FireFox、Opera等瀏覽器支持的創建方式
} else {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");//IE瀏覽器支持的創建方式
}
}
//直接通過XMLHttpRequest對象獲取遠程網頁源代碼
function getSource() {
var url = document.getElementById("url").value; //獲取目標地址信息
//地址為空時提示用戶輸入
if (url == "") {
alert("請輸入網頁地址 。");
return;
}
document.getElementById("source").value = "正在載入……"; //提示正在載入
createXmlHttp(); //創建XMLHttpRequest對象
xmlHttp.onreadystatechange = writeSource; //設置回調函數
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}
//將遠程網頁源代碼寫入頁面文字區域
function writeSource() {
if (xmlHttp.readyState == 4) {
document.getElementById("source").value = xmlHttp.responseText;
}
}
</script>
</head>
<body>
<h1>遠程網頁源代碼讀取</h1>
<div>
地址:<input type="text" id="url">
<input type="button" onclick="getSource()" value="獲取源碼">
</div>
<textarea rows="10" cols="80" id="source"></textarea>
</body>
</html>

熱點內容
c語言報告三 發布:2025-05-15 05:10:37 瀏覽:843
09壓縮餅干 發布:2025-05-15 05:05:58 瀏覽:279
迭代法編程c 發布:2025-05-15 04:58:01 瀏覽:815
用什麼dns伺服器地址快 發布:2025-05-15 04:52:59 瀏覽:27
手機端so反編譯 發布:2025-05-15 04:50:55 瀏覽:610
linuxlamp安裝 發布:2025-05-15 04:50:45 瀏覽:578
sqlplus緩存區怎麼設置 發布:2025-05-15 04:50:44 瀏覽:858
shell腳本環境變數 發布:2025-05-15 04:45:18 瀏覽:693
安卓nba2k18什麼時候出 發布:2025-05-15 04:38:42 瀏覽:393
王者安卓轉蘋果為什麼顯示失敗 發布:2025-05-15 04:35:49 瀏覽:18