超星 - 修复活动页面顶栏

因为使用不安全的 HTTP 协议加载跨域的顶栏脚本,超星课程的活动页面(比如各种签到页面)的顶栏会而加载不出来;即使使用 HTTPS 协议加载顶栏脚本,又会因为跨域脚本不能使用不安全的 document.writeln() 方法而毫无作用。本脚本修复了此问题。

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name        超星 - 修复活动页面顶栏
// @description 因为使用不安全的 HTTP 协议加载跨域的顶栏脚本,超星课程的活动页面(比如各种签到页面)的顶栏会而加载不出来;即使使用 HTTPS 协议加载顶栏脚本,又会因为跨域脚本不能使用不安全的 document.writeln() 方法而毫无作用。本脚本修复了此问题。
// @namespace   UnKnown
// @author      UnKnown
// @icon        https://imgsrc.baidu.com/forum/pic/item/6a63f6246b600c33c3d714d61c4c510fd9f9a106.jpg
// @version     1.0
// @match       https://mobilelearn.chaoxing.com/widget/*
// @grant       none
// @inject-into content
// @run-at      document-end
// ==/UserScript==

"use strict";

/*
<div id="header">
	<!-- <iframe src="/widget/pcpick/main/header" width="100%" scrolling="no" frameborder="0" height="44px"></iframe> -->
	<script id="pcPassPort" type="text/javascript" charset="utf-8" xml:space="preserve" src="http://www.fanya.chaoxing.com/passport/allHead.shtml"></script>
</div>
*/

const header = document.getElementById("header");

header !== null &&
header.querySelector(
	':scope > script[src="http://www.fanya.chaoxing.com/passport/allHead.shtml"]'
) !== null &&
fetch(
	"https://fanya.chaoxing.com/passport/allHead.shtml", {
		method      : "GET",
		mode        : "cors",
		credentials : "include"
	}
).then(
	response => response.text()
).then(

	text => text
		.match( /(?<=document\.writeln\(')[^']+(?='\);)/g )
		.filter( line => !/^\s+$/.test(line) )
		.join("\n")

).then(

	html => html.replace(
		/http:\/\/photo\.chaoxing\.com\//, "https://photo.chaoxing.com/"
	) + "<style>.zt_u_abs:hover > .zt_u_bg { display: block; }</style>"

).then(

	// 不执行内联脚本
	html => header.innerHTML = html

	// 执行内联脚本,无必要勿用;请自行了解和判断安全风险
	/* html => {
		header.innerHTML = "";
		header.appendChild(
			document.createRange().createContextualFragment(html)
		);
	} */

);