mirror of
https://github.com/servo/servo.git
synced 2025-07-24 15:50:21 +01:00
Allow setting userscripts directly without the need of files (#35388)
* Allow settings userscripts through preferences Signed-off-by: Tony <legendmastertony@gmail.com> * mach fmt instead of cargo fmt Signed-off-by: Tony <legendmastertony@gmail.com> * Fix pref loading not working for array values Signed-off-by: Tony <legendmastertony@gmail.com> * Use pref! in userscripts instead Signed-off-by: Tony <legendmastertony@gmail.com> * Implement the model jdm suggested - Remove userscripts from all places and move it to servoshell - Add in `UserContentManager` struct and passing it through `Servo::new` all the way down to script thread Signed-off-by: Tony <legendmastertony@gmail.com> * Apply suggestions from code review and format Signed-off-by: Tony <legendmastertony@gmail.com> * Revert unrelated change Signed-off-by: Tony <legendmastertony@gmail.com> --------- Signed-off-by: Tony <legendmastertony@gmail.com> Signed-off-by: Tony <68118705+Legend-Master@users.noreply.github.com>
This commit is contained in:
parent
53a2e61fec
commit
5a76906d64
16 changed files with 143 additions and 51 deletions
|
@ -2,9 +2,6 @@
|
|||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
use std::fs::{File, read_dir};
|
||||
use std::io::Read;
|
||||
use std::path::PathBuf;
|
||||
use std::rc::Rc;
|
||||
|
||||
use js::jsval::UndefinedValue;
|
||||
|
@ -19,37 +16,24 @@ use crate::script_runtime::CanGc;
|
|||
|
||||
pub(crate) fn load_script(head: &HTMLHeadElement) {
|
||||
let doc = head.owner_document();
|
||||
let path_str = match doc.window().get_userscripts_path() {
|
||||
Some(p) => p,
|
||||
None => return,
|
||||
};
|
||||
let userscripts = doc.window().userscripts().to_owned();
|
||||
if userscripts.is_empty() {
|
||||
return;
|
||||
}
|
||||
let window = Trusted::new(doc.window());
|
||||
doc.add_delayed_task(task!(UserScriptExecute: move || {
|
||||
let win = window.root();
|
||||
let cx = win.get_cx();
|
||||
rooted!(in(*cx) let mut rval = UndefinedValue());
|
||||
|
||||
let path = PathBuf::from(&path_str);
|
||||
let mut files = read_dir(path)
|
||||
.expect("Bad path passed to --userscripts")
|
||||
.filter_map(|e| e.ok())
|
||||
.map(|e| e.path())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
files.sort();
|
||||
|
||||
for file in files {
|
||||
let mut f = File::open(&file).unwrap();
|
||||
let mut contents = vec![];
|
||||
f.read_to_end(&mut contents).unwrap();
|
||||
for user_script in userscripts {
|
||||
let script_text = SourceCode::Text(
|
||||
Rc::new(DOMString::from_string(String::from_utf8_lossy(&contents).to_string()))
|
||||
Rc::new(DOMString::from_string(user_script.script))
|
||||
);
|
||||
|
||||
let global_scope = win.as_global_scope();
|
||||
global_scope.evaluate_script_on_global_with_result(
|
||||
&script_text,
|
||||
&file.to_string_lossy(),
|
||||
&user_script.source_file.map(|path| path.to_string_lossy().to_string()).unwrap_or_default(),
|
||||
rval.handle_mut(),
|
||||
1,
|
||||
ScriptFetchOptions::default_classic_script(global_scope),
|
||||
|
|
|
@ -26,6 +26,7 @@ use crossbeam_channel::{Sender, unbounded};
|
|||
use cssparser::{Parser, ParserInput, SourceLocation};
|
||||
use devtools_traits::{ScriptToDevtoolsControlMsg, TimelineMarker, TimelineMarkerType};
|
||||
use dom_struct::dom_struct;
|
||||
use embedder_traits::user_content_manager::{UserContentManager, UserScript};
|
||||
use embedder_traits::{
|
||||
AlertResponse, ConfirmResponse, EmbedderMsg, PromptResponse, SimpleDialog, Theme,
|
||||
WebDriverJSError, WebDriverJSResult,
|
||||
|
@ -373,10 +374,9 @@ pub(crate) struct Window {
|
|||
/// Unminify Css.
|
||||
unminify_css: bool,
|
||||
|
||||
/// Where to load userscripts from, if any. An empty string will load from
|
||||
/// the resources/user-agent-js directory, and if the option isn't passed userscripts
|
||||
/// won't be loaded.
|
||||
userscripts_path: Option<String>,
|
||||
/// User content manager
|
||||
#[no_trace]
|
||||
user_content_manager: UserContentManager,
|
||||
|
||||
/// Window's GL context from application
|
||||
#[ignore_malloc_size_of = "defined in script_thread"]
|
||||
|
@ -624,8 +624,8 @@ impl Window {
|
|||
&self.compositor_api
|
||||
}
|
||||
|
||||
pub(crate) fn get_userscripts_path(&self) -> Option<String> {
|
||||
self.userscripts_path.clone()
|
||||
pub(crate) fn userscripts(&self) -> &[UserScript] {
|
||||
self.user_content_manager.scripts()
|
||||
}
|
||||
|
||||
pub(crate) fn get_player_context(&self) -> WindowGLContext {
|
||||
|
@ -2797,7 +2797,7 @@ impl Window {
|
|||
unminify_js: bool,
|
||||
unminify_css: bool,
|
||||
local_script_source: Option<String>,
|
||||
userscripts_path: Option<String>,
|
||||
user_content_manager: UserContentManager,
|
||||
user_agent: Cow<'static, str>,
|
||||
player_context: WindowGLContext,
|
||||
#[cfg(feature = "webgpu")] gpu_id_hub: Arc<IdentityHub>,
|
||||
|
@ -2884,7 +2884,7 @@ impl Window {
|
|||
has_sent_idle_message: Cell::new(false),
|
||||
relayout_event,
|
||||
unminify_css,
|
||||
userscripts_path,
|
||||
user_content_manager,
|
||||
player_context,
|
||||
throttled: Cell::new(false),
|
||||
layout_marker: DomRefCell::new(Rc::new(Cell::new(true))),
|
||||
|
|
|
@ -43,6 +43,7 @@ use devtools_traits::{
|
|||
CSSError, DevtoolScriptControlMsg, DevtoolsPageInfo, NavigationState,
|
||||
ScriptToDevtoolsControlMsg, WorkerId,
|
||||
};
|
||||
use embedder_traits::user_content_manager::UserContentManager;
|
||||
use embedder_traits::{
|
||||
EmbedderMsg, InputEvent, MediaSessionActionType, Theme, WebDriverScriptCommand,
|
||||
};
|
||||
|
@ -303,10 +304,9 @@ pub struct ScriptThread {
|
|||
/// Unminify Css.
|
||||
unminify_css: bool,
|
||||
|
||||
/// Where to load userscripts from, if any. An empty string will load from
|
||||
/// the resources/user-agent-js directory, and if the option isn't passed userscripts
|
||||
/// won't be loaded
|
||||
userscripts_path: Option<String>,
|
||||
/// User content manager
|
||||
#[no_trace]
|
||||
user_content_manager: UserContentManager,
|
||||
|
||||
/// An optional string allowing the user agent to be set for testing.
|
||||
user_agent: Cow<'static, str>,
|
||||
|
@ -938,8 +938,8 @@ impl ScriptThread {
|
|||
unminify_js: opts.unminify_js,
|
||||
local_script_source: opts.local_script_source.clone(),
|
||||
unminify_css: opts.unminify_css,
|
||||
userscripts_path: opts.userscripts.clone(),
|
||||
user_agent,
|
||||
user_content_manager: state.user_content_manager,
|
||||
player_context: state.player_context,
|
||||
node_ids: Default::default(),
|
||||
is_user_interacting: Cell::new(false),
|
||||
|
@ -3096,7 +3096,7 @@ impl ScriptThread {
|
|||
self.unminify_js,
|
||||
self.unminify_css,
|
||||
self.local_script_source.clone(),
|
||||
self.userscripts_path.clone(),
|
||||
self.user_content_manager.clone(),
|
||||
self.user_agent.clone(),
|
||||
self.player_context.clone(),
|
||||
#[cfg(feature = "webgpu")]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue