123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- window.codebeautifyContentScript = (() => {
- let __importScript = (filename) => {
- let url = filename;
- if (location.protocol === 'chrome-extension:' || chrome.runtime && chrome.runtime.getURL) {
- url = chrome.runtime.getURL('code-beautify/' + filename);
- }
- // 使用chrome.runtime.sendMessage向background请求加载脚本
- chrome.runtime.sendMessage({
- type: 'fh-dynamic-any-thing',
- thing: 'load-local-script',
- script: url
- }, (scriptContent) => {
- if (!scriptContent) {
- console.error('Failed to load script:', filename);
- return;
- }
-
- // 如果有evalCore则使用它
- if (window.evalCore && window.evalCore.getEvalInstance) {
- try {
- window.evalCore.getEvalInstance(window)(scriptContent);
- } catch(e) {
- console.error('Failed to evaluate script using evalCore:', e);
- }
- } else {
- // 创建一个函数来执行脚本
- try {
- // 使用Function构造函数创建一个函数,并在当前窗口上下文中执行
- // 这比动态创建script元素更安全,因为它不涉及DOM操作
- const executeScript = new Function(scriptContent);
- executeScript.call(window);
- } catch(e) {
- console.error('Failed to execute script:', filename, e);
- }
- }
- });
- };
- __importScript('beautify.js');
- __importScript('beautify-css.js');
- let highlightWebWorker = () => {
- // TODO ...
- // __importScript('../static/vendor/highlight/highlight.js');
- self.onmessage = (event) => {
- // const result = self.hljs.highlightAuto(event.data);
- // postMessage(result.value);
- postMessage(event.data);
- };
- };
- let formattedCodes = '';
- let cssInjected = false;
- // **************************************************************
- /**
- * 代码美化
- */
- let format = (fileType, source, callback) => {
- let beauty = txtResult => {
- let code = document.getElementsByTagName('pre')[0];
- formattedCodes = txtResult;
- code.textContent = txtResult;
- code.classList.add('language-' + fileType.toLowerCase());
- document.querySelector('html').classList.add('jf-cb');
- // 用webwork的方式来进行格式化,效率更高
- let worker = new Worker(URL.createObjectURL(new Blob(["(" + highlightWebWorker.toString() + ")()"], {type: 'text/javascript'})));
- worker.onmessage = (event) => {
- code.innerHTML = "<ol><li><span>" + event.data
- .replace(/</gm,'<').replace(/>/gm,'>')
- .replace(/\n/gm, '</span></li><li><span>') + '</span></li></ol>';
- callback && callback();
- };
- worker.postMessage(txtResult);
- };
- switch (fileType) {
- case 'javascript':
- let opts = {
- brace_style: "collapse",
- break_chained_methods: false,
- indent_char: " ",
- indent_scripts: "keep",
- indent_size: "4",
- keep_array_indentation: true,
- preserve_newlines: true,
- space_after_anon_function: true,
- space_before_conditional: true,
- unescape_strings: false,
- wrap_line_length: "120"
- };
- beauty(js_beautify(source, opts));
- break;
- case 'css':
- css_beautify(source, {}, resp => beauty(resp));
- break;
- }
- };
- /**
- * 检测
- * @returns {boolean}
- */
- window._codebutifydetect_ = (fileType) => {
- if (!document.getElementsByTagName('pre')[0]) {
- return;
- }
- let source = document.getElementsByTagName('pre')[0].textContent;
- // 提前注入css
- if(!cssInjected) {
- chrome.runtime.sendMessage({
- type: 'fh-dynamic-any-thing',
- thing:'inject-content-css',
- tool: 'code-beautify'
- });
- }
- $(document.body).addClass('show-tipsbar');
- let tipsBar = $('<div id="fehelper_tips">' +
- '<span class="desc">FeHelper检测到这可能是<i>' + fileType + '</i>代码,<span class="ask">是否进行美化处理?</span></span>' +
- '<a class="encoding">有乱码?点击修正!</a>' +
- '<button class="yes">代码美化</button>' +
- '<button class="no">放弃!</button>' +
- '<button class="copy hide">复制美化过的代码</button>' +
- '<button class="close"><span></span></button>' +
- '<a class="forbid">彻底关闭这个功能!>></a>' +
- '</div>').prependTo('body');
- tipsBar.find('button.yes').click((evt) => {
- tipsBar.find('button.yes,button.no').hide();
- let elAsk = tipsBar.find('span.ask').text('正在努力美化,请稍候...');
- format(fileType, source, () => {
- elAsk.text('已为您美化完毕!');
- $(document.body).removeClass('show-tipsbar').addClass('show-beautified');
- });
- });
- tipsBar.find('a.forbid').click((evt) => {
- evt.preventDefault();
- if (confirm('一旦彻底关闭,不可恢复,请确认?')) {
- chrome.runtime.sendMessage({
- type: 'fh-dynamic-any-thing',
- thing: 'close-beautify'
- }, () => {
- alert('已关闭,如果要恢复,请在FeHelper「设置页」重新安装「代码美化工具」!');
- });
- }
- });
- tipsBar.find('button.no,button.close').click((evt) => {
- $(document.body).removeClass('show-tipsbar').removeClass('show-beautified');
- tipsBar.remove();
- });
- tipsBar.find('button.copy').click((evt) => {
- _copyToClipboard(formattedCodes);
- });
- tipsBar.find('a.encoding').click((evt) => {
- evt.preventDefault();
- fetch(location.href).then(res => res.text()).then(text => {
- source = text;
- if ($(document.body).hasClass('show-beautified')) {
- tipsBar.find('button.yes').trigger('click');
- } else {
- $('#fehelper_tips+pre').text(text);
- }
- });
- });
- };
- /**
- * chrome 下复制到剪贴板
- * @param text
- */
- let _copyToClipboard = function (text) {
- let input = document.createElement('textarea');
- input.style.position = 'fixed';
- input.style.opacity = 0;
- input.value = text;
- document.body.appendChild(input);
- input.select();
- document.execCommand('Copy');
- document.body.removeChild(input);
- alert('代码复制成功,随处粘贴可用!')
- };
- return function () {
- let ext = location.pathname.substring(location.pathname.lastIndexOf(".") + 1).toLowerCase();
- let fileType = ({'js': 'javascript', 'css': 'css'})[ext];
- let contentType = document.contentType.toLowerCase();
- if (!fileType) {
- if (/\/javascript$/.test(contentType)) {
- fileType = 'javascript';
- } else if (/\/css$/.test(contentType)) {
- fileType = 'css';
- }
- } else if (contentType === 'text/html') {
- fileType = undefined;
- }
- if (['javascript', 'css'].includes(fileType)) {
- chrome.runtime.sendMessage({
- type: 'fh-dynamic-any-thing',
- thing: 'code-beautify',
- params: { fileType, tabId: window.__FH_TAB_ID__ || null }
- });
- }
- };
- })();
|